-- Migration: 004_create_issues_tables.sql
-- Purpose: Create tables for Stage 3 issue routing

-- Issue state enum
DO $$ BEGIN
    CREATE TYPE issue_state AS ENUM ('open', 'resolved', 'ignored', 'merged');
EXCEPTION
    WHEN duplicate_object THEN NULL;
END $$;

-- Issues table
CREATE TABLE IF NOT EXISTS issues (
    id BIGSERIAL PRIMARY KEY,
    issue_id VARCHAR(50) NOT NULL UNIQUE,

    -- Context
    business_id VARCHAR(255) NOT NULL,
    place_id VARCHAR(255) NOT NULL,

    -- Classification
    primary_subcode VARCHAR(10) NOT NULL,
    domain CHAR(1) NOT NULL,

    -- State
    state issue_state NOT NULL DEFAULT 'open',
    priority_score REAL NOT NULL DEFAULT 1.0,
    confidence_score REAL NOT NULL DEFAULT 1.0,

    -- Aggregates
    span_count INTEGER NOT NULL DEFAULT 1,
    max_intensity VARCHAR(5) NOT NULL DEFAULT 'I1',

    -- Entity (optional - for entity-specific issues)
    entity VARCHAR(255),
    entity_normalized VARCHAR(255),

    -- Metadata
    taxonomy_version VARCHAR(20) NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

-- Issue-span links (1:1 - each span routes to exactly one issue)
CREATE TABLE IF NOT EXISTS issue_spans (
    id BIGSERIAL PRIMARY KEY,
    issue_id VARCHAR(50) NOT NULL REFERENCES issues(issue_id),
    span_id VARCHAR(50) NOT NULL UNIQUE,

    -- Review reference
    source VARCHAR(20) NOT NULL DEFAULT 'google',
    review_id VARCHAR(255) NOT NULL,
    review_version INTEGER NOT NULL DEFAULT 1,

    -- Match info
    is_primary_match BOOLEAN NOT NULL DEFAULT TRUE,
    intensity VARCHAR(5) NOT NULL,
    review_time TIMESTAMP WITH TIME ZONE NOT NULL,

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

-- Issue events (audit log)
CREATE TABLE IF NOT EXISTS issue_events (
    id BIGSERIAL PRIMARY KEY,
    issue_id VARCHAR(50) NOT NULL REFERENCES issues(issue_id),
    event_type VARCHAR(50) NOT NULL,
    span_id VARCHAR(50),
    old_value TEXT,
    new_value TEXT,
    metadata JSONB,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

-- Indexes for issues
CREATE INDEX IF NOT EXISTS idx_issues_business_id ON issues(business_id);
CREATE INDEX IF NOT EXISTS idx_issues_place_id ON issues(place_id);
CREATE INDEX IF NOT EXISTS idx_issues_state ON issues(state);
CREATE INDEX IF NOT EXISTS idx_issues_primary_subcode ON issues(primary_subcode);
CREATE INDEX IF NOT EXISTS idx_issues_domain ON issues(domain);
CREATE INDEX IF NOT EXISTS idx_issues_entity_normalized ON issues(entity_normalized) WHERE entity_normalized IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_issues_priority ON issues(priority_score DESC) WHERE state = 'open';
CREATE INDEX IF NOT EXISTS idx_issues_created ON issues(created_at);
CREATE INDEX IF NOT EXISTS idx_issues_updated ON issues(updated_at);

-- Indexes for issue_spans
CREATE INDEX IF NOT EXISTS idx_issue_spans_issue_id ON issue_spans(issue_id);
CREATE INDEX IF NOT EXISTS idx_issue_spans_review_time ON issue_spans(review_time);

-- Indexes for issue_events
CREATE INDEX IF NOT EXISTS idx_issue_events_issue_id ON issue_events(issue_id);
CREATE INDEX IF NOT EXISTS idx_issue_events_created ON issue_events(created_at);
CREATE INDEX IF NOT EXISTS idx_issue_events_type ON issue_events(event_type);

COMMENT ON TABLE issues IS 'Aggregated issues derived from negative/mixed spans';
COMMENT ON TABLE issue_spans IS 'Links between issues and their source spans';
COMMENT ON TABLE issue_events IS 'Audit log for issue state changes';
