/**
 * Migration: Create Carrier Activities Table
 *
 * This table logs all actions performed by carriers (transporteurs)
 * Including: login, booking acceptance/rejection, document downloads, profile updates
 */

import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Create ENUM type for activity types
    await queryRunner.query(`
      CREATE TYPE "carrier_activity_type" AS ENUM (
        'BOOKING_ACCEPTED',
        'BOOKING_REJECTED',
        'DOCUMENT_DOWNLOADED',
        'PROFILE_UPDATED',
        'LOGIN',
        'PASSWORD_CHANGED'
      )
    `);

    // Create carrier_activities table
    await queryRunner.query(`
      CREATE TABLE "carrier_activities" (
        "id" UUID NOT NULL DEFAULT uuid_generate_v4(),
        "carrier_id" UUID NOT NULL,
        "booking_id" UUID NULL,

        "activity_type" carrier_activity_type NOT NULL,
        "description" TEXT NULL,
        "metadata" JSONB NULL,

        "ip_address" VARCHAR(45) NULL,
        "user_agent" TEXT NULL,

        "created_at" TIMESTAMP NOT NULL DEFAULT NOW(),

        CONSTRAINT "pk_carrier_activities" PRIMARY KEY ("id"),
        CONSTRAINT "fk_carrier_activities_carrier" FOREIGN KEY ("carrier_id")
          REFERENCES "carrier_profiles"("id") ON DELETE CASCADE,
        CONSTRAINT "fk_carrier_activities_booking" FOREIGN KEY ("booking_id")
          REFERENCES "csv_bookings"("id") ON DELETE SET NULL
      )
    `);

    // Create indexes for performance
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_activities_carrier_id" ON "carrier_activities" ("carrier_id")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_activities_booking_id" ON "carrier_activities" ("booking_id")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_activities_type" ON "carrier_activities" ("activity_type")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_activities_created_at" ON "carrier_activities" ("created_at" DESC)
    `);

    // Composite index for common queries
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_activities_carrier_created"
      ON "carrier_activities" ("carrier_id", "created_at" DESC)
    `);

    // GIN index for JSONB metadata search
    await queryRunner.query(`
      CREATE INDEX "idx_carrier_activities_metadata"
      ON "carrier_activities" USING GIN ("metadata")
    `);

    // Add comments
    await queryRunner.query(`
      COMMENT ON TABLE "carrier_activities" IS 'Audit log of all carrier actions'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "carrier_activities"."activity_type" IS 'Type of activity performed'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "carrier_activities"."metadata" IS 'Additional context data (JSON)'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "carrier_activities"."ip_address" IS 'IP address of the carrier (IPv4 or IPv6)'
    `);
  }

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