-- ============================================================
-- Cielo message history — run once in the Supabase SQL editor
-- Stores the full conversation Cielo has had with each user,
-- independently of the general sliding-window chat history.
-- ============================================================

CREATE TABLE IF NOT EXISTS cielo_messages (
    id         UUID DEFAULT gen_random_uuid() PRIMARY KEY,
    user_id    TEXT NOT NULL,
    role       TEXT NOT NULL,        -- 'user' or 'cielo'
    content    TEXT NOT NULL,
    created_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_cielo_messages_user_id_time
    ON cielo_messages (user_id, created_at DESC);

ALTER TABLE cielo_messages ENABLE ROW LEVEL SECURITY;

CREATE POLICY "service_role_all_cielo_messages" ON cielo_messages
    FOR ALL TO service_role USING (true) WITH CHECK (true);
