/** Verified-purchase reviews. */

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const up = (pgm) => {
  pgm.sql(`
    CREATE TABLE reviews_reviews (
      id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
      user_id uuid NOT NULL,
      product_id uuid NOT NULL,
      order_id uuid NOT NULL,
      order_item_id uuid NOT NULL UNIQUE,
      rating integer NOT NULL,
      title text NOT NULL,
      body text NOT NULL,
      status text NOT NULL DEFAULT 'pending',
      created_at timestamptz NOT NULL DEFAULT now(),
      updated_at timestamptz NOT NULL DEFAULT now(),
      CONSTRAINT reviews_reviews_rating_check CHECK (rating BETWEEN 1 AND 5),
      CONSTRAINT reviews_reviews_status_check CHECK (status IN ('pending','published','rejected'))
    )
  `);
  pgm.sql(
    'CREATE INDEX reviews_reviews_product_status_idx ON reviews_reviews (product_id, status)',
  );
  pgm.sql('CREATE INDEX reviews_reviews_user_id_idx ON reviews_reviews (user_id)');
};

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