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

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'companies',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'gen_random_uuid()' },
          { name: 'name', type: 'varchar', length: '200' },
          { name: 'type', type: 'varchar', length: '32' },
          { name: 'status', type: 'varchar', length: '32', default: "'pending'" },
          { name: 'tin', type: 'varchar', length: '10', isUnique: true },
          { name: 'vat_number', type: 'varchar', length: '50', isNullable: true },
          { name: 'business_license', type: 'varchar', length: '100', isNullable: true },
          { name: 'fan_number', type: 'varchar', length: '16', isNullable: true },
          { name: 'country', type: 'varchar', length: '32', default: "'Ethiopia'" },
          { name: 'address', type: 'text', isNullable: true },
          { name: 'phone', type: 'varchar', length: '20', isNullable: true },
          { name: 'email', type: 'varchar', length: '150', isNullable: true },
          { name: 'website', type: 'varchar', length: '200', isNullable: true },
          { name: 'attributes', type: 'jsonb', isNullable: true },
          { name: 'created_at', type: 'timestamptz', default: 'now()' },
          { name: 'updated_at', type: 'timestamptz', default: 'now()' },
          { name: 'deleted_at', type: 'timestamptz', isNullable: true },
        ],
      }),
      true,
    );

    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'external_profiles',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'gen_random_uuid()' },
          { name: 'user_id', type: 'uuid' },
          { name: 'company_id', type: 'uuid' },
          { name: 'first_name', type: 'varchar', length: '100' },
          { name: 'last_name', type: 'varchar', length: '100' },
          { name: 'email', type: 'varchar', length: '150', isUnique: true },
          { name: 'phone', type: 'varchar', length: '20', isNullable: true },
          { name: 'national_id', type: 'varchar', length: '50', isNullable: true },
          { name: 'job_title', type: 'varchar', length: '100', isNullable: true },
          { name: 'is_primary_contact', type: 'boolean', default: false },
          { name: 'created_at', type: 'timestamptz', default: 'now()' },
          { name: 'updated_at', type: 'timestamptz', default: 'now()' },
          { name: 'deleted_at', type: 'timestamptz', isNullable: true },
        ],
        foreignKeys: [
          {
            columnNames: ['company_id'],
            referencedTableName: 'companies',
            referencedSchema: 'freight',
            referencedColumnNames: ['id'],
          },
        ],
      }),
      true,
    );

    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'ff_clients',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'gen_random_uuid()' },
          { name: 'forwarder_company_id', type: 'uuid' },
          { name: 'client_company_id', type: 'uuid' },
          { name: 'relationship_type', type: 'varchar', length: '32', default: "'managed_account'" },
          { name: 'can_book_on_behalf', type: 'boolean', default: true },
          { name: 'can_view_documents', type: 'boolean', default: true },
          { name: 'created_at', type: 'timestamptz', default: 'now()' },
          { name: 'updated_at', type: 'timestamptz', default: 'now()' },
          { name: 'deleted_at', type: 'timestamptz', isNullable: true },
        ],
        foreignKeys: [
          {
            columnNames: ['forwarder_company_id'],
            referencedTableName: 'companies',
            referencedSchema: 'freight',
            referencedColumnNames: ['id'],
          },
          {
            columnNames: ['client_company_id'],
            referencedTableName: 'companies',
            referencedSchema: 'freight',
            referencedColumnNames: ['id'],
          },
        ],
      }),
      true,
    );

    await queryRunner.createIndex('freight.companies', new TableIndex({ columnNames: ['tin'] }));
    await queryRunner.createIndex('freight.companies', new TableIndex({ columnNames: ['type'] }));
    await queryRunner.createIndex('freight.external_profiles', new TableIndex({ columnNames: ['user_id'] }));
    await queryRunner.createIndex('freight.external_profiles', new TableIndex({ columnNames: ['company_id'] }));
    await queryRunner.createIndex('freight.ff_clients', new TableIndex({ columnNames: ['forwarder_company_id'] }));
    await queryRunner.createIndex('freight.ff_clients', new TableIndex({ columnNames: ['client_company_id'] }));

    await queryRunner.createUniqueConstraint('freight.ff_clients', new TableUnique({
      columnNames: ['forwarder_company_id', 'client_company_id'],
    }));
  }

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