import { MigrationInterface, QueryRunner } from 'typeorm';

export class SeasonDateToMMDD1775556000000 implements MigrationInterface {
  name = 'SeasonDateToMMDD1775556000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Step 1: Change column type from date to varchar, converting existing values inline
    await queryRunner.query(`
      ALTER TABLE seasons
      ALTER COLUMN start_date TYPE character varying(5)
      USING CASE WHEN start_date IS NOT NULL THEN TO_CHAR(start_date, 'MM-DD') ELSE NULL END
    `);
    await queryRunner.query(`
      ALTER TABLE seasons
      ALTER COLUMN end_date TYPE character varying(5)
      USING CASE WHEN end_date IS NOT NULL THEN TO_CHAR(end_date, 'MM-DD') ELSE NULL END
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Revert to date type — use dummy year 2000 to reconstruct
    await queryRunner.query(`
      ALTER TABLE seasons
      ALTER COLUMN start_date TYPE date
      USING CASE WHEN start_date IS NOT NULL THEN ('2000-' || start_date)::date ELSE NULL END
    `);
    await queryRunner.query(`
      ALTER TABLE seasons
      ALTER COLUMN end_date TYPE date
      USING CASE WHEN end_date IS NOT NULL THEN ('2000-' || end_date)::date ELSE NULL END
    `);
  }
}
