import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`CREATE SCHEMA IF NOT EXISTS freight`);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS freight.work_orders (
        id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        vehicle_id UUID NOT NULL,
        title VARCHAR NOT NULL,
        description TEXT,
        status VARCHAR NOT NULL DEFAULT 'OPEN',
        priority VARCHAR NOT NULL DEFAULT 'MEDIUM',
        assigned_to VARCHAR,
        opened_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        closed_at TIMESTAMPTZ,
        labor_cost NUMERIC(14, 2),
        parts_cost NUMERIC(14, 2),
        created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        deleted_at TIMESTAMPTZ
      );
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS freight.parts (
        id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        name VARCHAR NOT NULL,
        sku VARCHAR,
        category VARCHAR,
        quantity_in_stock INT NOT NULL DEFAULT 0,
        reorder_level INT NOT NULL DEFAULT 0,
        unit_cost NUMERIC(14, 2),
        location VARCHAR,
        created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        deleted_at TIMESTAMPTZ
      );
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS freight.warranties (
        id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        vehicle_id UUID NOT NULL,
        component VARCHAR NOT NULL,
        provider VARCHAR,
        start_date DATE,
        expiry_date DATE NOT NULL,
        coverage_notes TEXT,
        created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        deleted_at TIMESTAMPTZ
      );
    `);

    await queryRunner.query(
      `CREATE INDEX IF NOT EXISTS "IDX_work_orders_vehicle_id_status" ON freight.work_orders (vehicle_id, status)`,
    );
    await queryRunner.query(
      `CREATE INDEX IF NOT EXISTS "IDX_parts_category" ON freight.parts (category)`,
    );
    await queryRunner.query(
      `CREATE INDEX IF NOT EXISTS "IDX_warranties_vehicle_id_expiry_date" ON freight.warranties (vehicle_id, expiry_date)`,
    );

    await queryRunner.query(`
      DO $$ BEGIN
        ALTER TABLE freight.work_orders
          ADD CONSTRAINT "FK_work_orders_vehicle_id"
          FOREIGN KEY (vehicle_id) REFERENCES freight.vehicles(id) ON DELETE CASCADE;
      EXCEPTION WHEN duplicate_object THEN NULL;
      END $$;
    `);

    await queryRunner.query(`
      DO $$ BEGIN
        ALTER TABLE freight.warranties
          ADD CONSTRAINT "FK_warranties_vehicle_id"
          FOREIGN KEY (vehicle_id) REFERENCES freight.vehicles(id) ON DELETE CASCADE;
      EXCEPTION WHEN duplicate_object THEN NULL;
      END $$;
    `);
  }

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