import { renderToStaticMarkup } from 'react-dom/server';
import { describe, expect, it, vi } from 'vitest';
import type { IntegrationPlatform, PlatformModel } from '@easyai-ai-gateway/contracts';
import { AccessPermissionEditor, ineffectiveRuleCleanupBatches } from './AccessPermissionEditor';

describe('AccessPermissionEditor API Key diagnostics', () => {
  it('shows retained ineffective rules and only the scope-pruned model capabilities', () => {
    const platform = {
      id: 'platform-1',
      provider: 'openai',
      platformKey: 'scope-test',
      name: 'Scope Test',
      authType: 'bearer',
      status: 'enabled',
      priority: 0,
      effectivePriority: 0,
      defaultPricingMode: 'inherit',
      defaultDiscountFactor: 1,
      createdAt: '2026-08-03T00:00:00Z',
      updatedAt: '2026-08-03T00:00:00Z',
    } satisfies IntegrationPlatform;
    const model = {
      id: 'model-1',
      platformId: platform.id,
      modelName: 'multi-model',
      modelType: ['image_generate'],
      displayName: 'Multi Model',
      pricingMode: 'inherit',
      rateLimitPolicyMode: 'inherit',
      enabled: true,
      createdAt: '2026-08-03T00:00:00Z',
      updatedAt: '2026-08-03T00:00:00Z',
    } satisfies PlatformModel;

    const html = renderToStaticMarkup(
      <AccessPermissionEditor
        accessRules={[]}
        platformModels={[model]}
        platforms={[platform]}
        ruleDiagnostics={[{
          ruleId: 'rule-1',
          resourceType: 'platform_model',
          resourceId: 'text-model',
          resourceName: 'Text Model',
          effect: 'allow',
          effective: false,
          reason: 'scope_not_allowed',
        }]}
        state="ready"
        subjectId="key-1"
        subjectType="api_key"
        onBatchAccessRules={vi.fn()}
      />,
    );

    expect(html).toContain('1 条规则当前不生效');
    expect(html).toContain('未配置白名单');
    expect(html).toContain('继承上级可用范围');
    expect(html).toContain('允许使用（白名单）');
    expect(html).toContain('清空允许');
    expect(html).toContain('Text Model');
    expect(html).toContain('不在当前 API Key 的能力范围内');
    expect(html).toContain('移除规则');
    expect(html).toContain('一键清理');
    expect(html).toContain('image_generate');
    expect(html).not.toContain('text_generate');
  });

  it('shows the restrictive whitelist state when the current subject has an allow', () => {
    const html = renderToStaticMarkup(
      <AccessPermissionEditor
        accessRules={[{
          id: 'rule-allow',
          subjectType: 'api_key',
          subjectId: 'key-1',
          resourceType: 'platform_model',
          resourceId: 'model-1',
          effect: 'allow',
          priority: 100,
          minPermissionLevel: 0,
          conditions: {},
          metadata: {},
          status: 'active',
          createdAt: '2026-08-03T00:00:00Z',
          updatedAt: '2026-08-03T00:00:00Z',
        }]}
        platformModels={[]}
        platforms={[]}
        state="ready"
        subjectId="key-1"
        subjectType="api_key"
        onBatchAccessRules={vi.fn()}
      />,
    );

    expect(html).toContain('已配置白名单');
    expect(html).toContain('仅允许所选资源');
    expect(html).not.toContain('专属使用');
  });

  it('groups one-click cleanup by effect and deduplicates retained diagnostics', () => {
    const batches = ineffectiveRuleCleanupBatches('api_key', 'key-1', [
      { ruleId: 'allow-1', resourceType: 'platform_model', resourceId: 'model-1', effect: 'allow', effective: false, reason: 'scope_not_allowed' },
      { ruleId: 'allow-duplicate', resourceType: 'platform_model', resourceId: 'model-1', effect: 'allow', effective: false, reason: 'scope_not_allowed' },
      { ruleId: 'deny-1', resourceType: 'platform', resourceId: 'platform-1', effect: 'deny', effective: false, reason: 'resource_unavailable' },
      { ruleId: 'effective', resourceType: 'platform_model', resourceId: 'model-2', effect: 'allow', effective: true },
    ]);
    expect(batches).toHaveLength(2);
    expect(batches[0]).toMatchObject({ effect: 'allow', deleteResources: [{ resourceType: 'platform_model', resourceId: 'model-1' }] });
    expect(batches[1]).toMatchObject({ effect: 'deny', deleteResources: [{ resourceType: 'platform', resourceId: 'platform-1' }] });
  });
});
