#!/usr/bin/env ts-node
/**
 * Test script for media reference migration
 * Creates a fixture with old URL formats and tests the migration
 */

import { MediaReferenceMigration } from './migrate-media-refs';

// Mock data with old URL formats
const mockDriverData = [
  { id: 'driver-1', oldUrl: '/api/avatar/driver-1' },
  { id: 'driver-2', oldUrl: '/api/avatar/driver-2' },
  { id: 'driver-3', oldUrl: '/images/avatars/male-default-avatar.jpg' },
  { id: 'driver-4', oldUrl: 'https://external.com/avatar.jpg' },
  { id: 'driver-5', oldUrl: '' }, // Empty
  { id: 'driver-6', oldUrl: null }, // Null
];

const mockTeamData = [
  { id: 'team-1', oldUrl: '/api/media/teams/team-1/logo' },
  { id: 'team-2', oldUrl: '/api/teams/team-2/logo' },
  { id: 'team-3', oldUrl: 'https://example.com/logo.png' },
];

const mockLeagueData = [
  { id: 'league-1', oldUrl: '/api/media/leagues/league-1/logo' },
  { id: 'league-2', oldUrl: null },
];

async function testMigration() {
  console.log('=== Testing Media Reference Migration ===\n');

  const migration = new MediaReferenceMigration(true); // Dry run mode

  console.log('Testing URL parsing logic...\n');

  // Test driver avatars
  console.log('Driver Avatar Tests:');
  for (const driver of mockDriverData) {
    const ref = (migration as any).parseOldUrl(driver.oldUrl);
    const result = ref ? ref.toJSON() : null;
    console.log(`  ${driver.id}: "${driver.oldUrl}" -> ${JSON.stringify(result)}`);
  }

  console.log('\nTeam Logo Tests:');
  for (const team of mockTeamData) {
    const ref = (migration as any).parseOldUrl(team.oldUrl);
    const result = ref ? ref.toJSON() : null;
    console.log(`  ${team.id}: "${team.oldUrl}" -> ${JSON.stringify(result)}`);
  }

  console.log('\nLeague Logo Tests:');
  for (const league of mockLeagueData) {
    const ref = (migration as any).parseOldUrl(league.oldUrl);
    const result = ref ? ref.toJSON() : null;
    console.log(`  ${league.id}: "${league.oldUrl}" -> ${JSON.stringify(result)}`);
  }

  // Test deterministic avatar selection
  console.log('\nDeterministic Avatar Variant Tests:');
  const testIds = ['driver-1', 'driver-2', 'driver-3', 'driver-4', 'driver-5'];
  for (const id of testIds) {
    const variant = (migration as any).getDeterministicAvatarVariant(id);
    console.log(`  ${id} -> ${variant}`);
  }

  console.log('\n✅ All tests completed successfully!');
  console.log('\nTo execute the actual migration:');
  console.log('  npm run migrate:media:exec');
  console.log('\nFor dry run (no changes):');
  console.log('  npm run migrate:media:test');
}

// Run tests
testMigration().catch(console.error);