// tests/api/07-known-issues.ts
// Tests for Known Implementation Issues (EXPECTED TO FAIL)

import { api, log, runTest, createTestImage, testContext, exitWithTestResults } from './utils';
import { endpoints } from './config';

async function main() {
  log.section('KNOWN ISSUES TESTS (Expected to Fail)');
  log.warning('These tests document known bugs and missing features');

  // Issue #1: Project aliases on flow images
  await runTest('ISSUE: Project alias on flow image', async () => {
    const gen = await createTestImage('Image in flow with project alias', {
      flowAlias: '@flow-test',
      alias: '@project-test', // Project alias on flow image
    });

    // Try to resolve the project alias
    const result = await api(`${endpoints.images}/resolve/@project-test`);

    if (!result.data.data || result.data.data.imageId !== gen.outputImageId) {
      throw new Error('Project alias on flow image should work but does not');
    }

    log.detail('Project alias resolved', '✓');
    log.detail('Image ID', gen.outputImageId);
  });

  // Issue #2: Flow cascade delete - non-aliased images
  await runTest('ISSUE: Flow delete cascades non-aliased images', async () => {
    // Create flow with mixed images
    const genWithoutAlias = await createTestImage('No alias', {
      flowAlias: '@issue-flow',
    });
    const flowId = genWithoutAlias.flowId;

    // Add another image with project alias
    const genWithAlias = await createTestImage('With alias', {
      flowId: flowId,
      alias: '@protected-image',
    });

    // Delete flow
    await api(`${endpoints.flows}/${flowId}`, {
      method: 'DELETE',
    });

    // Check if non-aliased image was deleted
    try {
      await api(`${endpoints.images}/${genWithoutAlias.outputImageId}`, {
        expectError: true,
      });
      log.detail('Non-aliased image deleted', '✓');
    } catch (error: any) {
      if (error.message.includes('expectError')) {
        throw new Error('Non-aliased image should be deleted but still exists');
      }
    }
  });

  // Issue #3: Flow cascade delete - aliased images protected
  await runTest('ISSUE: Flow delete preserves aliased images', async () => {
    // Create flow
    const gen = await createTestImage('Protected image', {
      flowAlias: '@test-flow-2',
      alias: '@keep-this',
    });
    const flowId = gen.flowId;

    // Delete flow
    await api(`${endpoints.flows}/${flowId}`, {
      method: 'DELETE',
    });

    // Aliased image should exist but flowId should be null
    const image = await api(`${endpoints.images}/${gen.outputImageId}`);

    if (image.data.data.flowId !== null) {
      throw new Error('Aliased image should have flowId=null after flow deletion');
    }

    log.detail('Aliased image preserved', '✓');
    log.detail('flowId set to null', '✓');
  });

  // Issue #4: Flow cascade delete - generations
  await runTest('ISSUE: Flow delete cascades generations', async () => {
    // Create flow with generation
    const gen = await createTestImage('Test gen', {
      flowAlias: '@gen-flow',
    });
    const flowId = gen.flowId;
    const genId = gen.id;

    // Delete flow
    await api(`${endpoints.flows}/${flowId}`, {
      method: 'DELETE',
    });

    // Generation should be deleted
    try {
      await api(`${endpoints.generations}/${genId}`, {
        expectError: true,
      });
      log.detail('Generation deleted', '✓');
    } catch (error: any) {
      if (error.message.includes('expectError')) {
        throw new Error('Generation should be deleted but still exists');
      }
    }
  });

  log.section('KNOWN ISSUES TESTS COMPLETED');
  log.warning('Failures above are EXPECTED and document bugs to fix');
}

main()
  .then(() => exitWithTestResults())
  .catch((error) => {
    console.error('Unexpected error:', error);
    process.exit(1);
  });
