-- Enrichment Cache Table
-- Caches AI enrichment results per distillery to save API calls

CREATE TABLE IF NOT EXISTS enrichment_cache (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    distillery TEXT NOT NULL UNIQUE,
    suggested_tags TEXT[],
    suggested_custom_tags TEXT[],
    search_string TEXT,
    created_at TIMESTAMPTZ DEFAULT now(),
    updated_at TIMESTAMPTZ DEFAULT now(),
    hit_count INTEGER DEFAULT 0
);

-- Index for fast lookups
CREATE INDEX IF NOT EXISTS idx_enrichment_cache_distillery 
ON enrichment_cache(distillery);

-- RLS: Allow all authenticated users to read (it's shared cache)
ALTER TABLE enrichment_cache ENABLE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS "enrichment_cache_select" ON enrichment_cache;
CREATE POLICY "enrichment_cache_select" ON enrichment_cache
    FOR SELECT TO authenticated USING (true);

DROP POLICY IF EXISTS "enrichment_cache_insert" ON enrichment_cache;
CREATE POLICY "enrichment_cache_insert" ON enrichment_cache
    FOR INSERT TO authenticated WITH CHECK (true);

DROP POLICY IF EXISTS "enrichment_cache_update" ON enrichment_cache;
CREATE POLICY "enrichment_cache_update" ON enrichment_cache
    FOR UPDATE TO authenticated USING (true);
