import { MigrationInterface, QueryRunner } from "typeorm";

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    const fuelPurchasesExists = await queryRunner.query(
      `SELECT 1 FROM information_schema.tables WHERE table_schema = 'freight' AND table_name = 'fuel_purchases';`,
    );

    if (!fuelPurchasesExists.length) {
      await queryRunner.query(`
        CREATE TABLE freight.fuel_purchases (
          id uuid NOT NULL DEFAULT gen_random_uuid(),
          vehicle_id uuid NOT NULL,
          purchase_date timestamptz NOT NULL,
          liters numeric(10, 2) NOT NULL,
          cost_per_liter numeric(10, 2) NOT NULL,
          total_cost numeric(14, 2) NOT NULL,
          fuel_station varchar(255) NULL,
          payment_method varchar(50) DEFAULT 'CASH',
          odometer_reading numeric(10, 2) NULL,
          driver_id uuid NULL,
          receipt_number varchar(255) NULL,
          notes text NULL,
          created_at timestamptz NOT NULL DEFAULT now(),
          updated_at timestamptz NOT NULL DEFAULT now(),
          deleted_at timestamptz NULL,
          CONSTRAINT pk_fuel_purchases PRIMARY KEY (id),
          CONSTRAINT fk_fuel_purchases_vehicle FOREIGN KEY (vehicle_id)
            REFERENCES freight.vehicles (id) ON DELETE CASCADE
        );
      `);

      await queryRunner.query(
        `CREATE INDEX idx_fuel_purchases_vehicle ON freight.fuel_purchases (vehicle_id);`,
      );
      await queryRunner.query(
        `CREATE INDEX idx_fuel_purchases_date ON freight.fuel_purchases (purchase_date);`,
      );
    }

    const fuelConsumptionExists = await queryRunner.query(
      `SELECT 1 FROM information_schema.tables WHERE table_schema = 'freight' AND table_name = 'fuel_consumption';`,
    );

    if (!fuelConsumptionExists.length) {
      await queryRunner.query(`
        CREATE TABLE freight.fuel_consumption (
          id uuid NOT NULL DEFAULT gen_random_uuid(),
          vehicle_id uuid NOT NULL,
          month date NOT NULL,
          total_liters numeric(10, 2) NOT NULL,
          total_cost numeric(14, 2) NOT NULL,
          total_distance_km numeric(10, 2) NOT NULL,
          fuel_efficiency_km_per_l numeric(10, 2) NULL,
          number_of_purchases integer DEFAULT 0,
          average_cost_per_liter numeric(10, 2) NULL,
          created_at timestamptz NOT NULL DEFAULT now(),
          updated_at timestamptz NOT NULL DEFAULT now(),
          deleted_at timestamptz NULL,
          CONSTRAINT pk_fuel_consumption PRIMARY KEY (id),
          CONSTRAINT fk_fuel_consumption_vehicle FOREIGN KEY (vehicle_id)
            REFERENCES freight.vehicles (id) ON DELETE CASCADE,
          CONSTRAINT uq_fuel_consumption_vehicle_month UNIQUE (vehicle_id, month)
        );
      `);

      await queryRunner.query(
        `CREATE INDEX idx_fuel_consumption_vehicle_month ON freight.fuel_consumption (vehicle_id, month);`,
      );
    }
  }

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