/**
 * Order history log. Every shipping/tracking change and admin state transition
 * is recorded so the order detail can show a full audit trail.
 * @param {import('node-pg-migrate').MigrationBuilder} pgm
 */
export const up = (pgm) => {
  pgm.sql(`
    CREATE TABLE IF NOT EXISTS orders_order_history (
      id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
      order_id uuid NOT NULL REFERENCES orders_orders(id) ON DELETE CASCADE,
      event_type text NOT NULL,
      message text NOT NULL,
      tracking_number text NULL,
      actor_email text NULL,
      created_at timestamptz NOT NULL DEFAULT now()
    )
  `);
  pgm.sql(
    `CREATE INDEX IF NOT EXISTS idx_orders_order_history_order ON orders_order_history (order_id, created_at)`,
  );
};

export const down = (pgm) => {
  pgm.sql(`DROP TABLE IF EXISTS orders_order_history`);
};
