/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
exports.shorthands = undefined;

exports.up = (pgm) => {
  // ── F-140: add type to users_addresses to distinguish shipping from billing ──
  pgm.sql(`
    ALTER TABLE users_addresses
    ADD COLUMN type text NOT NULL DEFAULT 'shipping'
    CHECK (type IN ('shipping', 'billing'));
  `);
  pgm.sql(`CREATE INDEX users_addresses_user_id_type_idx ON users_addresses (user_id, type);`);

  // ── F-140: add phone to identity_users for customer contact ──────────────────
  pgm.sql(`
    ALTER TABLE identity_users
    ADD COLUMN phone text;
  `);
  pgm.sql(`CREATE INDEX identity_users_phone_idx ON identity_users (phone) WHERE phone IS NOT NULL;`);

  // ── F-140: add customer_name to orders_orders (captured at checkout) ──────────
  pgm.sql(`
    ALTER TABLE orders_orders
    ADD COLUMN customer_name text,
    ADD COLUMN customer_phone text;
  `);
};

exports.down = (pgm) => {
  pgm.sql(`ALTER TABLE orders_orders DROP COLUMN IF EXISTS customer_name, DROP COLUMN IF EXISTS customer_phone;`);
  pgm.sql(`ALTER TABLE identity_users DROP COLUMN IF EXISTS phone;`);
  pgm.sql(`DROP INDEX IF EXISTS users_addresses_user_id_type_idx;`);
  pgm.sql(`ALTER TABLE users_addresses DROP COLUMN IF EXISTS type;`);
};
