-- Migration: 001_create_reviews_tables.sql
-- Purpose: Create the core reviews tables for Stage 1 normalization

-- Raw reviews table (immutable audit log)
CREATE TABLE IF NOT EXISTS reviews_raw (
    id BIGSERIAL PRIMARY KEY,
    source VARCHAR(20) NOT NULL DEFAULT 'google',
    review_id VARCHAR(255) NOT NULL,
    place_id VARCHAR(255) NOT NULL,
    raw_payload JSONB NOT NULL DEFAULT '{}',
    review_text TEXT,
    rating SMALLINT NOT NULL CHECK (rating BETWEEN 1 AND 5),
    review_time TIMESTAMP WITH TIME ZONE NOT NULL,
    reviewer_name VARCHAR(255) NOT NULL,
    reviewer_id VARCHAR(255),
    review_version INTEGER NOT NULL DEFAULT 1,
    pulled_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),

    CONSTRAINT reviews_raw_unique UNIQUE (source, review_id, review_version)
);

-- Indexes for reviews_raw
CREATE INDEX IF NOT EXISTS idx_reviews_raw_place_id ON reviews_raw(place_id);
CREATE INDEX IF NOT EXISTS idx_reviews_raw_review_time ON reviews_raw(review_time);
CREATE INDEX IF NOT EXISTS idx_reviews_raw_pulled_at ON reviews_raw(pulled_at);

-- Enriched reviews table (mutable, updated by classification)
CREATE TABLE IF NOT EXISTS reviews_enriched (
    id BIGSERIAL PRIMARY KEY,
    source VARCHAR(20) NOT NULL DEFAULT 'google',
    review_id VARCHAR(255) NOT NULL,
    review_version INTEGER NOT NULL DEFAULT 1,
    is_latest BOOLEAN NOT NULL DEFAULT TRUE,
    raw_id BIGINT REFERENCES reviews_raw(id),

    -- Tenant context
    business_id VARCHAR(255) NOT NULL,
    place_id VARCHAR(255) NOT NULL,

    -- Content
    text TEXT NOT NULL,
    text_normalized TEXT NOT NULL,
    rating SMALLINT NOT NULL CHECK (rating BETWEEN 1 AND 5),
    review_time TIMESTAMP WITH TIME ZONE NOT NULL,

    -- Normalization fields
    language VARCHAR(10) NOT NULL DEFAULT 'en',
    taxonomy_version VARCHAR(20) NOT NULL DEFAULT 'v5.1',

    -- Classification fields (NULL until Stage 2)
    urt_primary VARCHAR(10),
    urt_secondary VARCHAR(10)[] DEFAULT '{}',
    valence VARCHAR(5),
    intensity VARCHAR(5),
    comparative VARCHAR(10),
    staff_mentions VARCHAR(255)[] DEFAULT '{}',
    quotes JSONB DEFAULT '{}',
    embedding REAL[] DEFAULT '{}',
    trust_score REAL,
    classification_model VARCHAR(100),
    classification_confidence JSONB DEFAULT '{}',
    processed_at TIMESTAMP WITH TIME ZONE,

    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),

    CONSTRAINT reviews_enriched_unique UNIQUE (source, review_id, review_version)
);

-- Indexes for reviews_enriched
CREATE INDEX IF NOT EXISTS idx_reviews_enriched_business_id ON reviews_enriched(business_id);
CREATE INDEX IF NOT EXISTS idx_reviews_enriched_place_id ON reviews_enriched(place_id);
CREATE INDEX IF NOT EXISTS idx_reviews_enriched_review_time ON reviews_enriched(review_time);
CREATE INDEX IF NOT EXISTS idx_reviews_enriched_urt_primary ON reviews_enriched(urt_primary) WHERE urt_primary IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_reviews_enriched_unclassified ON reviews_enriched(review_time DESC) WHERE urt_primary IS NULL AND is_latest = TRUE;
CREATE INDEX IF NOT EXISTS idx_reviews_enriched_valence ON reviews_enriched(valence) WHERE valence IS NOT NULL;

-- Comment on tables
COMMENT ON TABLE reviews_raw IS 'Immutable raw review data as scraped from source';
COMMENT ON TABLE reviews_enriched IS 'Enriched reviews with normalization and classification';
