-- Migration: 002_create_spans_table.sql
-- Purpose: Create the review_spans table for Stage 2 classification output

CREATE TABLE IF NOT EXISTS review_spans (
    id BIGSERIAL PRIMARY KEY,
    span_id VARCHAR(50) NOT NULL UNIQUE,

    -- Context
    business_id VARCHAR(255) NOT NULL,
    place_id VARCHAR(255) NOT NULL,
    source VARCHAR(20) NOT NULL DEFAULT 'google',
    review_id VARCHAR(255) NOT NULL,
    review_version INTEGER NOT NULL DEFAULT 1,

    -- Position
    span_index INTEGER NOT NULL CHECK (span_index >= 0),
    span_text TEXT NOT NULL,
    span_start INTEGER NOT NULL CHECK (span_start >= 0),
    span_end INTEGER NOT NULL CHECK (span_end > span_start),

    -- Classification profile
    profile VARCHAR(20) NOT NULL DEFAULT 'standard',

    -- Core URT classification
    urt_primary VARCHAR(10) NOT NULL,
    urt_secondary VARCHAR(10)[] DEFAULT '{}',
    valence VARCHAR(5) NOT NULL,
    intensity VARCHAR(5) NOT NULL,
    comparative VARCHAR(10) NOT NULL DEFAULT 'CR-N',

    -- Extended classification (standard/full profile)
    specificity VARCHAR(5),
    actionability VARCHAR(5),
    temporal VARCHAR(5),
    evidence VARCHAR(5),

    -- Entity extraction
    entity VARCHAR(255),
    entity_type VARCHAR(20),
    entity_normalized VARCHAR(255),

    -- Causal relations (full profile)
    relation_type VARCHAR(20),
    related_span_id VARCHAR(50),
    causal_chain JSONB,

    -- Flags
    is_primary BOOLEAN NOT NULL DEFAULT FALSE,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,

    -- Time reference
    review_time TIMESTAMP WITH TIME ZONE NOT NULL,

    -- Metadata
    confidence VARCHAR(10) NOT NULL DEFAULT 'medium',
    usn VARCHAR(100) NOT NULL,
    taxonomy_version VARCHAR(20) NOT NULL,
    model_version VARCHAR(100) NOT NULL,
    ingest_batch_id VARCHAR(50) NOT NULL,

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

    -- Foreign key to review
    CONSTRAINT fk_review FOREIGN KEY (source, review_id, review_version)
        REFERENCES reviews_enriched(source, review_id, review_version)
);

-- Indexes for review_spans
CREATE INDEX IF NOT EXISTS idx_spans_business_id ON review_spans(business_id);
CREATE INDEX IF NOT EXISTS idx_spans_place_id ON review_spans(place_id);
CREATE INDEX IF NOT EXISTS idx_spans_review_time ON review_spans(review_time);
CREATE INDEX IF NOT EXISTS idx_spans_urt_primary ON review_spans(urt_primary);
CREATE INDEX IF NOT EXISTS idx_spans_valence ON review_spans(valence);
CREATE INDEX IF NOT EXISTS idx_spans_intensity ON review_spans(intensity);
CREATE INDEX IF NOT EXISTS idx_spans_is_active ON review_spans(is_active) WHERE is_active = TRUE;
CREATE INDEX IF NOT EXISTS idx_spans_is_primary ON review_spans(is_primary) WHERE is_primary = TRUE;
CREATE INDEX IF NOT EXISTS idx_spans_entity_normalized ON review_spans(entity_normalized) WHERE entity_normalized IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_spans_batch ON review_spans(ingest_batch_id);

-- Index for unrouted negative spans (Stage 3 query)
CREATE INDEX IF NOT EXISTS idx_spans_unrouted_negative ON review_spans(review_time DESC)
    WHERE is_active = TRUE AND valence IN ('V-', 'V±');

COMMENT ON TABLE review_spans IS 'Extracted semantic spans with URT classification from reviews';
