import { MigrationInterface, QueryRunner, Table } from 'typeorm';

/** Batch 6 — warehouse fee invoices + invoice items. Idempotent (createTable ifNotExists). */
export class AddWarehouseFeeInvoices1791000000001 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'warehouse_fee_invoices',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, default: 'uuid_generate_v4()' },
          { name: 'invoice_number', type: 'varchar', length: '40', isUnique: true },
          { name: 'booking_id', type: 'uuid', isNullable: true },
          { name: 'customer_id', type: 'uuid', isNullable: true },
          { name: 'inventory_id', type: 'uuid' },
          { name: 'facility_id', type: 'uuid', isNullable: true },
          { name: 'warehouse_id', type: 'uuid', isNullable: true },
          { name: 'yard_id', type: 'uuid', isNullable: true },
          { name: 'zone_id', type: 'uuid', isNullable: true },
          { name: 'invoice_type', type: 'varchar', length: '32', default: "'MIXED_WAREHOUSE_FEES'" },
          { name: 'status', type: 'varchar', length: '20', default: "'DRAFT'" },
          { name: 'subtotal_amount', type: 'numeric', precision: 14, scale: 2, default: 0 },
          { name: 'tax_amount', type: 'numeric', precision: 14, scale: 2, default: 0 },
          { name: 'total_amount', type: 'numeric', precision: 14, scale: 2, default: 0 },
          { name: 'paid_amount', type: 'numeric', precision: 14, scale: 2, default: 0 },
          { name: 'balance_amount', type: 'numeric', precision: 14, scale: 2, default: 0 },
          { name: 'currency', type: 'varchar', length: '8', default: "'USD'" },
          { name: 'period_start', type: 'timestamptz', isNullable: true },
          { name: 'period_end', type: 'timestamptz', isNullable: true },
          { name: 'issued_at', type: 'timestamptz', isNullable: true },
          { name: 'due_date', type: 'timestamptz', isNullable: true },
          { name: 'paid_at', type: 'timestamptz', isNullable: true },
          { name: 'cancelled_at', type: 'timestamptz', isNullable: true },
          { name: 'payments', type: 'jsonb', default: "'[]'" },
          { name: 'notes', type: 'text', isNullable: true },
          { name: 'created_at', type: 'timestamptz', default: 'now()' },
          { name: 'updated_at', type: 'timestamptz', default: 'now()' },
          { name: 'deleted_at', type: 'timestamptz', isNullable: true },
        ],
        indices: [
          { name: 'idx_wfi_booking', columnNames: ['booking_id'] },
          { name: 'idx_wfi_inventory', columnNames: ['inventory_id'] },
          { name: 'idx_wfi_status', columnNames: ['status'] },
        ],
      }),
      true,
    );

    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'warehouse_fee_invoice_items',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, default: 'uuid_generate_v4()' },
          { name: 'invoice_id', type: 'uuid' },
          { name: 'fee_rule_id', type: 'uuid', isNullable: true },
          { name: 'fee_type', type: 'varchar', length: '32' },
          { name: 'description', type: 'varchar', length: '255' },
          { name: 'quantity', type: 'numeric', precision: 12, scale: 2, default: 1 },
          { name: 'unit_rate', type: 'numeric', precision: 14, scale: 2, default: 0 },
          { name: 'amount', type: 'numeric', precision: 14, scale: 2, default: 0 },
          { name: 'currency', type: 'varchar', length: '8', default: "'USD'" },
          { name: 'chargeable_days', type: 'int', isNullable: true },
          { name: 'free_days', type: 'int', isNullable: true },
          { name: 'created_at', type: 'timestamptz', default: 'now()' },
          { name: 'updated_at', type: 'timestamptz', default: 'now()' },
          { name: 'deleted_at', type: 'timestamptz', isNullable: true },
        ],
        foreignKeys: [
          {
            columnNames: ['invoice_id'],
            referencedSchema: 'freight',
            referencedTableName: 'warehouse_fee_invoices',
            referencedColumnNames: ['id'],
            onDelete: 'CASCADE',
          },
        ],
        indices: [{ name: 'idx_wfii_invoice', columnNames: ['invoice_id'] }],
      }),
      true,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable('freight.warehouse_fee_invoice_items', true);
    await queryRunner.dropTable('freight.warehouse_fee_invoices', true);
  }
}
