-- Global News Interpolation Layer
-- Adds tables for macro event classification, company exposure profiles,
-- macro impact scoring, and trend projections.

-- ============================================================
-- Global Events
-- ============================================================

CREATE TABLE global_events (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    event_types TEXT[] NOT NULL,
    severity VARCHAR(20) NOT NULL,
    affected_regions TEXT[] NOT NULL DEFAULT '{}',
    affected_sectors TEXT[] NOT NULL DEFAULT '{}',
    affected_commodities TEXT[] NOT NULL DEFAULT '{}',
    summary TEXT NOT NULL,
    key_facts JSONB NOT NULL DEFAULT '[]',
    estimated_duration VARCHAR(20) NOT NULL,
    confidence FLOAT NOT NULL,
    source_document_id UUID REFERENCES documents(id),
    model_provider VARCHAR(100),
    model_name VARCHAR(200),
    prompt_version VARCHAR(100),
    schema_version VARCHAR(20),
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_global_events_created ON global_events(created_at);

-- ============================================================
-- Macro Impact Records
-- ============================================================

CREATE TABLE macro_impact_records (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    event_id UUID NOT NULL REFERENCES global_events(id),
    company_id UUID NOT NULL REFERENCES companies(id),
    ticker VARCHAR(20) NOT NULL,
    macro_impact_score FLOAT NOT NULL,
    impact_direction VARCHAR(20) NOT NULL,
    contributing_factors JSONB NOT NULL DEFAULT '[]',
    confidence FLOAT NOT NULL,
    computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_macro_impact_event ON macro_impact_records(event_id);
CREATE INDEX idx_macro_impact_company_computed ON macro_impact_records(company_id, computed_at);
CREATE INDEX idx_macro_impact_ticker_computed ON macro_impact_records(ticker, computed_at);

-- ============================================================
-- Exposure Profiles
-- ============================================================

CREATE TABLE exposure_profiles (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    company_id UUID NOT NULL REFERENCES companies(id),
    geographic_revenue_mix JSONB NOT NULL DEFAULT '{}',
    supply_chain_regions TEXT[] NOT NULL DEFAULT '{}',
    key_input_commodities TEXT[] NOT NULL DEFAULT '{}',
    regulatory_jurisdictions TEXT[] NOT NULL DEFAULT '{}',
    market_position_tier VARCHAR(30) NOT NULL DEFAULT 'regional',
    export_dependency_pct FLOAT NOT NULL DEFAULT 0.0,
    source VARCHAR(20) NOT NULL DEFAULT 'manual',
    confidence FLOAT NOT NULL DEFAULT 1.0,
    version INTEGER NOT NULL DEFAULT 1,
    active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_exposure_profiles_company_active ON exposure_profiles(company_id, active);

-- ============================================================
-- Trend Projections
-- ============================================================

CREATE TABLE trend_projections (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    trend_window_id UUID NOT NULL REFERENCES trend_windows(id),
    projected_direction VARCHAR(20) NOT NULL,
    projected_strength FLOAT NOT NULL,
    projected_confidence FLOAT NOT NULL,
    projection_horizon VARCHAR(10) NOT NULL,
    driving_factors JSONB NOT NULL DEFAULT '[]',
    macro_contribution_pct FLOAT NOT NULL DEFAULT 0.0,
    diverges_from_current BOOLEAN NOT NULL DEFAULT FALSE,
    computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_trend_projections_window ON trend_projections(trend_window_id);
