/**
 * POS-002 — Add `store_id` to `inventory_stock` and `inventory_movements`.
 *
 * Steps in `up()`:
 *   1. Add `store_id` as a NULLABLE column (so the backfill doesn't fail).
 *   2. Backfill every existing row with the well-known default store UUID.
 *   3. Make `store_id` NOT NULL and add the FK to `pos_stores`.
 *   4. Drop the old single-column unique constraint on `inventory_stock.variant_id`.
 *   5. Add the new composite unique constraint `(variant_id, store_id)`.
 *   6. Mirror the same 5-step pattern for `inventory_movements` (without the
 *      unique constraint, since movements don't have a UNIQUE invariant).
 *
 * Idempotent: every ALTER uses `IF NOT EXISTS` / `IF EXISTS` / `DO` blocks
 * so re-running is a no-op.
 *
 * @param {import('node-pg-migrate').MigrationBuilder} pgm
 */

const DEFAULT_STORE_ID = '00000000-0000-0000-0000-000000000001';

export const up = async (pgm) => {
  // ── inventory_stock ───────────────────────────────────────────────────────
  pgm.sql(`
    ALTER TABLE inventory_stock
      ADD COLUMN IF NOT EXISTS store_id uuid
  `);
  pgm.sql(`
    UPDATE inventory_stock
    SET store_id = '${DEFAULT_STORE_ID}'
    WHERE store_id IS NULL
  `);
  pgm.sql(`
    ALTER TABLE inventory_stock
      ALTER COLUMN store_id SET NOT NULL
  `);
  pgm.sql(`
    DO $$
    BEGIN
      IF NOT EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'inventory_stock_store_id_fkey'
      ) THEN
        ALTER TABLE inventory_stock
          ADD CONSTRAINT inventory_stock_store_id_fkey
          FOREIGN KEY (store_id) REFERENCES pos_stores(id) ON DELETE RESTRICT;
      END IF;
    END $$
  `);
  pgm.sql(`ALTER TABLE inventory_stock DROP CONSTRAINT IF EXISTS inventory_stock_variant_id_key`);
  pgm.sql(`
    DO $$
    BEGIN
      IF NOT EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'inventory_stock_variant_store_unique'
      ) THEN
        ALTER TABLE inventory_stock
          ADD CONSTRAINT inventory_stock_variant_store_unique
          UNIQUE (variant_id, store_id);
      END IF;
    END $$
  `);

  // ── inventory_movements ──────────────────────────────────────────────────
  pgm.sql(`
    ALTER TABLE inventory_movements
      ADD COLUMN IF NOT EXISTS store_id uuid
  `);
  pgm.sql(`
    UPDATE inventory_movements
    SET store_id = '${DEFAULT_STORE_ID}'
    WHERE store_id IS NULL
  `);
  pgm.sql(`
    ALTER TABLE inventory_movements
      ALTER COLUMN store_id SET NOT NULL
  `);
  pgm.sql(`
    DO $$
    BEGIN
      IF NOT EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'inventory_movements_store_id_fkey'
      ) THEN
        ALTER TABLE inventory_movements
          ADD CONSTRAINT inventory_movements_store_id_fkey
          FOREIGN KEY (store_id) REFERENCES pos_stores(id) ON DELETE RESTRICT;
      END IF;
    END $$
  `);
};

export const down = (pgm) => {
  pgm.sql(`ALTER TABLE inventory_movements DROP CONSTRAINT IF EXISTS inventory_movements_store_id_fkey`);
  pgm.sql(`ALTER TABLE inventory_movements DROP COLUMN IF EXISTS store_id`);

  pgm.sql(`ALTER TABLE inventory_stock DROP CONSTRAINT IF EXISTS inventory_stock_variant_store_unique`);
  pgm.sql(`
    DO $$
    BEGIN
      IF NOT EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'inventory_stock_variant_id_key'
      ) THEN
        ALTER TABLE inventory_stock
          ADD CONSTRAINT inventory_stock_variant_id_key UNIQUE (variant_id);
      END IF;
    END $$
  `);
  pgm.sql(`ALTER TABLE inventory_stock DROP CONSTRAINT IF EXISTS inventory_stock_store_id_fkey`);
  pgm.sql(`ALTER TABLE inventory_stock DROP COLUMN IF EXISTS store_id`);
};
