/**
 * Users module tables. Module-owned naming: users_<table>.
 * FKs to identity_users are schema-level integrity only; runtime queries in
 * the users module touch users_* tables exclusively.
 */

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const up = (pgm) => {
  pgm.sql(`
    CREATE TABLE users_profiles (
      user_id uuid PRIMARY KEY REFERENCES identity_users(id) ON DELETE CASCADE,
      display_name text,
      phone text,
      created_at timestamptz NOT NULL DEFAULT now(),
      updated_at timestamptz NOT NULL DEFAULT now()
    )
  `);
  pgm.sql(`
    CREATE TABLE users_addresses (
      id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
      user_id uuid NOT NULL REFERENCES identity_users(id) ON DELETE CASCADE,
      label text,
      recipient_name text NOT NULL,
      street text NOT NULL,
      city text NOT NULL,
      postal_code text NOT NULL,
      country text NOT NULL,
      is_default boolean NOT NULL DEFAULT false,
      created_at timestamptz NOT NULL DEFAULT now(),
      updated_at timestamptz NOT NULL DEFAULT now()
    )
  `);
  pgm.sql('CREATE INDEX users_addresses_user_id_idx ON users_addresses (user_id)');
};

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const down = (pgm) => {
  pgm.sql('DROP TABLE IF EXISTS users_addresses');
  pgm.sql('DROP TABLE IF EXISTS users_profiles');
};
