import { describe, expect, it } from 'vitest';
import { defaultAdminTaskQuery, parseAppRoute, pathForAdminSection, pathForAdminTaskQuery, pathForApiDocSection } from './routing';

describe('API documentation routes', () => {
  it.each([
    ['openApiOverview', '/docs/open-api'],
    ['compatApiOverview', '/docs/compatible-api'],
    ['compatGemini', '/docs/compatible-api/gemini'],
    ['compatKling', '/docs/compatible-api/kling'],
    ['compatVolces', '/docs/compatible-api/volces'],
    ['compatServerMain', '/docs/compatible-api/server-main'],
    ['guideBaseUrl', '/docs/auth'],
    ['guideWebhook', '/docs/webhook'],
    ['guideErrors', '/docs/errors'],
    ['guideTesting', '/docs/testing'],
    ['responses', '/docs/responses'],
    ['videoGeneration', '/docs/videos/generations'],
    ['asyncMode', '/docs/async'],
    ['taskRetrieve', '/docs/tasks/retrieve'],
  ] as const)('maps %s to a stable documentation URL', (section, path) => {
    expect(pathForApiDocSection(section)).toBe(path);
    expect(parseAppRoute(path).apiDocSection).toBe(section);
  });

  it.each([
    ['/docs/playground', 'guideTesting'],
    ['/docs/api/responses', 'responses'],
    ['/docs/api/videos', 'videoGeneration'],
    ['/docs/api/tasks', 'taskRetrieve'],
  ] as const)('keeps the legacy documentation URL %s working', (path, section) => {
    expect(parseAppRoute(path).apiDocSection).toBe(section);
  });

  it('uses the common open API catalog as the documentation landing page', () => {
    expect(parseAppRoute('/docs').apiDocSection).toBe('openApiOverview');
  });
});

describe('admin task routes', () => {
  it('round-trips the complete admin task query through the URL', () => {
    const query = {
      ...defaultAdminTaskQuery(),
      query: 'request-123',
      tenantId: 'tenant-id',
      userId: 'user-id',
      userGroupId: 'group-id',
      status: 'failed',
      platformId: 'platform-id',
      model: 'model-a',
      modelType: 'image_generate',
      runMode: 'production',
      billingStatus: 'settled',
      apiKey: 'ops-key',
      createdFrom: '2026-07-01T00:00:00Z',
      createdTo: '2026-07-25T00:00:00Z',
      page: 3,
      pageSize: 50,
    };
    const path = pathForAdminTaskQuery(query);
    const parsed = parseAppRoute(path);
    expect(parsed.adminSection).toBe('tasks');
    expect(parsed.adminTaskQuery).toEqual(query);
  });

  it('uses stable defaults for the admin task page', () => {
    expect(parseAppRoute('/admin/tasks').adminTaskQuery).toEqual(defaultAdminTaskQuery());
  });
});

describe('admin billing settlement route', () => {
  it('maps billing settlements to an independent admin page', () => {
    expect(pathForAdminSection('billingSettlements')).toBe('/admin/billing-settlements');
    expect(parseAppRoute('/admin/billing-settlements').adminSection).toBe('billingSettlements');
  });
});

describe('legacy admin access rule route', () => {
  it('opens user groups because model permissions are configured from the group row', () => {
    expect(parseAppRoute('/admin/access-rules').adminSection).toBe('userGroups');
  });
});
