import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS freight.files (
        id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
        resource_id UUID NOT NULL,
        resource VARCHAR(100) NOT NULL,
        code VARCHAR(100) NOT NULL,
        name VARCHAR(500) NOT NULL,
        url TEXT NOT NULL,
        size INTEGER NOT NULL,
        mime_type VARCHAR(255) NOT NULL,
        created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        deleted_at TIMESTAMPTZ
      );
    `);

    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS "IDX_freight_files_resource"
        ON freight.files (resource_id, resource);
    `);

    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS "IDX_freight_files_resource_code"
        ON freight.files (resource_id, resource, code);
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE IF EXISTS freight.files CASCADE`);
  }
}
