-- ============================================================
-- Migration 02 — image column on ads + price_history tracking
-- ============================================================

-- -----------------------------------------------------------
-- 1. Add main_image_url to ads (nullable)
-- -----------------------------------------------------------
ALTER TABLE ads
    ADD COLUMN IF NOT EXISTS main_image_url TEXT;

-- -----------------------------------------------------------
-- 2. price_history — record every price change per ad
-- -----------------------------------------------------------
CREATE TABLE IF NOT EXISTS price_history (
    id          uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    ad_id       uuid NOT NULL REFERENCES ads(id) ON DELETE CASCADE,
    old_price   numeric NOT NULL,
    new_price   numeric NOT NULL,
    changed_at  timestamptz NOT NULL DEFAULT now(),
    UNIQUE (ad_id, old_price, new_price)
);

CREATE INDEX IF NOT EXISTS idx_price_history_ad_id
    ON price_history(ad_id);

-- Note: supabase_admin role creation + grants moved to post-boot.sql.
