/**
 * Migration: Create Carrier Profiles Table
 *
 * This table stores carrier (transporteur) profile information
 * Linked to users and organizations for authentication and management
 */

import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Create carrier_profiles table
    await queryRunner.query(`
      CREATE TABLE "carrier_profiles" (
        "id" UUID NOT NULL DEFAULT uuid_generate_v4(),
        "user_id" UUID NOT NULL,
        "organization_id" UUID NOT NULL,

        -- Informations professionnelles
        "company_name" VARCHAR(255) NOT NULL,
        "company_registration" VARCHAR(100) NULL,
        "vat_number" VARCHAR(50) NULL,

        -- Contact
        "phone" VARCHAR(50) NULL,
        "website" VARCHAR(255) NULL,

        -- Adresse
        "street_address" TEXT NULL,
        "city" VARCHAR(100) NULL,
        "postal_code" VARCHAR(20) NULL,
        "country" CHAR(2) NULL,

        -- Statistiques
        "total_bookings_accepted" INTEGER NOT NULL DEFAULT 0,
        "total_bookings_rejected" INTEGER NOT NULL DEFAULT 0,
        "acceptance_rate" DECIMAL(5,2) NOT NULL DEFAULT 0.00,
        "total_revenue_usd" DECIMAL(15,2) NOT NULL DEFAULT 0.00,
        "total_revenue_eur" DECIMAL(15,2) NOT NULL DEFAULT 0.00,

        -- Préférences
        "preferred_currency" VARCHAR(3) NOT NULL DEFAULT 'USD',
        "notification_email" VARCHAR(255) NULL,
        "auto_accept_enabled" BOOLEAN NOT NULL DEFAULT FALSE,

        -- Métadonnées
        "is_verified" BOOLEAN NOT NULL DEFAULT FALSE,
        "is_active" BOOLEAN NOT NULL DEFAULT TRUE,
        "last_login_at" TIMESTAMP NULL,
        "created_at" TIMESTAMP NOT NULL DEFAULT NOW(),
        "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(),

        CONSTRAINT "pk_carrier_profiles" PRIMARY KEY ("id"),
        CONSTRAINT "uq_carrier_profiles_user_id" UNIQUE ("user_id"),
        CONSTRAINT "fk_carrier_profiles_user" FOREIGN KEY ("user_id")
          REFERENCES "users"("id") ON DELETE CASCADE,
        CONSTRAINT "fk_carrier_profiles_organization" FOREIGN KEY ("organization_id")
          REFERENCES "organizations"("id") ON DELETE CASCADE,
        CONSTRAINT "chk_carrier_profiles_acceptance_rate"
          CHECK ("acceptance_rate" >= 0 AND "acceptance_rate" <= 100),
        CONSTRAINT "chk_carrier_profiles_revenue_usd"
          CHECK ("total_revenue_usd" >= 0),
        CONSTRAINT "chk_carrier_profiles_revenue_eur"
          CHECK ("total_revenue_eur" >= 0)
      )
    `);

    // Create indexes
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_profiles_user_id" ON "carrier_profiles" ("user_id")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_profiles_org_id" ON "carrier_profiles" ("organization_id")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_profiles_company_name" ON "carrier_profiles" ("company_name")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_profiles_is_active" ON "carrier_profiles" ("is_active")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_profiles_is_verified" ON "carrier_profiles" ("is_verified")
    `);

    // Add comments
    await queryRunner.query(`
      COMMENT ON TABLE "carrier_profiles" IS 'Carrier (transporteur) profiles for B2B portal'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "carrier_profiles"."acceptance_rate" IS 'Percentage of accepted bookings (0-100)'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "carrier_profiles"."auto_accept_enabled" IS 'Automatically accept compatible bookings'
    `);
  }

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