import {
  MigrationInterface,
  QueryRunner,
  Table,
  TableIndex,
  TableForeignKey,
} from "typeorm";

export class CreateCompanyProfiles1752000000000 implements MigrationInterface {
  name = "CreateCompanyProfiles1752000000000";

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        schema: "freight",
        name: "company_profiles",
        columns: [
          {
            name: "id",
            type: "uuid",
            isPrimary: true,
            generationStrategy: "uuid",
            default: "gen_random_uuid()",
          },
          { name: "company_id", type: "uuid" },
          { name: "type", type: "varchar", length: "32" },
          { name: "reference", type: "varchar", length: "20", isUnique: true },
          {
            name: "status",
            type: "varchar",
            length: "32",
            default: "'active'",
          },
          {
            name: "business_license",
            type: "varchar",
            length: "100",
            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.createForeignKey(
      "freight.company_profiles",
      new TableForeignKey({
        columnNames: ["company_id"],
        referencedTableName: "companies",
        referencedSchema: "freight",
        referencedColumnNames: ["id"],
      }),
    );

    await queryRunner.createIndex(
      "freight.company_profiles",
      new TableIndex({ columnNames: ["company_id"] }),
    );
    await queryRunner.createIndex(
      "freight.company_profiles",
      new TableIndex({ columnNames: ["type"] }),
    );

    await queryRunner.query(
      `CREATE SEQUENCE IF NOT EXISTS freight.seq_company_profile_ex START WITH 1`,
    );
    await queryRunner.query(
      `CREATE SEQUENCE IF NOT EXISTS freight.seq_company_profile_im START WITH 1`,
    );
    await queryRunner.query(
      `CREATE SEQUENCE IF NOT EXISTS freight.seq_company_profile_ffe START WITH 1`,
    );
    await queryRunner.query(
      `CREATE SEQUENCE IF NOT EXISTS freight.seq_company_profile_fwj START WITH 1`,
    );
    await queryRunner.query(
      `CREATE SEQUENCE IF NOT EXISTS freight.seq_company_profile_tr START WITH 1`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable("freight.company_profiles");
    await queryRunner.query(
      `DROP SEQUENCE IF EXISTS freight.seq_company_profile_ex`,
    );
    await queryRunner.query(
      `DROP SEQUENCE IF EXISTS freight.seq_company_profile_im`,
    );
    await queryRunner.query(
      `DROP SEQUENCE IF EXISTS freight.seq_company_profile_ffe`,
    );
    await queryRunner.query(
      `DROP SEQUENCE IF EXISTS freight.seq_company_profile_fwj`,
    );
    await queryRunner.query(
      `DROP SEQUENCE IF EXISTS freight.seq_company_profile_tr`,
    );
  }
}
