import { MigrationInterface, QueryRunner, Table, TableIndex } from 'typeorm';

export class CreateFacilitiesTable1750000000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'facilities',
        columns: [
          {
            name: 'id',
            type: 'uuid',
            isPrimary: true,
            generationStrategy: 'uuid',
            default: 'gen_random_uuid()',
          },
          {
            name: 'code',
            type: 'varchar',
            length: '40',
            isUnique: true,
          },
          {
            name: 'name',
            type: 'varchar',
            length: '160',
          },
          {
            name: 'description',
            type: 'text',
            isNullable: true,
          },
          {
            name: 'facility_type',
            type: 'varchar',
            length: '32',
          },
          {
            name: 'facility_status',
            type: 'varchar',
            length: '32',
            default: "'ACTIVE'",
          },
          {
            name: 'location_name',
            type: 'varchar',
            length: '200',
            isNullable: true,
          },
          {
            name: 'country',
            type: 'varchar',
            length: '100',
            isNullable: true,
          },
          {
            name: 'city',
            type: 'varchar',
            length: '100',
            isNullable: true,
          },
          {
            name: 'address',
            type: 'text',
            isNullable: true,
          },
          {
            name: 'latitude',
            type: 'numeric',
            precision: 10,
            scale: 8,
            isNullable: true,
          },
          {
            name: 'longitude',
            type: 'numeric',
            precision: 11,
            scale: 8,
            isNullable: true,
          },
          {
            name: 'capacity',
            type: 'numeric',
            precision: 14,
            scale: 3,
            isNullable: true,
          },
          {
            name: 'is_active',
            type: 'boolean',
            default: true,
          },
          {
            name: 'notes',
            type: 'text',
            isNullable: true,
          },
          {
            name: 'created_at',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
          },
          {
            name: 'updated_at',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
          },
          {
            name: 'deleted_at',
            type: 'timestamp',
            isNullable: true,
          },
        ],
      }),
    );

    await queryRunner.createIndex(
      'freight.facilities',
      new TableIndex({
        name: 'idx_facilities_code',
        columnNames: ['code'],
        isUnique: true,
      }),
    );

    await queryRunner.createIndex(
      'freight.facilities',
      new TableIndex({
        name: 'idx_facilities_status',
        columnNames: ['facility_status'],
      }),
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable('freight.facilities');
  }
}
