-- Migration 038: Trading Reports
-- Creates the trading_reports table for storing periodic performance reports
-- and seeds the Report Summarizer AI agent for report section summarization.

-- ============================================================================
-- Table: trading_reports
-- Stores daily and weekly trading performance reports as structured JSONB
-- ============================================================================
CREATE TABLE IF NOT EXISTS trading_reports (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    report_type VARCHAR(20) NOT NULL,
    period_start DATE NOT NULL,
    period_end DATE NOT NULL,
    report_data JSONB NOT NULL,
    validation_status VARCHAR(20) NOT NULL DEFAULT 'passed',
    generated_at TIMESTAMPTZ NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    CONSTRAINT uq_trading_reports_period UNIQUE (report_type, period_start, period_end),
    CONSTRAINT chk_report_type CHECK (report_type IN ('daily', 'weekly'))
);

CREATE INDEX IF NOT EXISTS idx_trading_reports_type ON trading_reports(report_type);
CREATE INDEX IF NOT EXISTS idx_trading_reports_period ON trading_reports(period_start, period_end);
CREATE INDEX IF NOT EXISTS idx_trading_reports_generated ON trading_reports(generated_at DESC);

-- ============================================================================
-- Seed: Report Summarizer Agent
-- Generates concise natural-language summaries of trading performance report
-- sections. Uses chunked data within the 8k-token context window.
-- Only inserted if the slug does not already exist (idempotent).
-- ============================================================================
INSERT INTO ai_agents (name, slug, purpose, model_provider, model_name, system_prompt, prompt_version, schema_version, temperature, max_tokens, timeout_seconds, max_retries, source)
SELECT * FROM (VALUES
    (
        'Report Summarizer',
        'report-summarizer',
        'Generates concise natural-language summaries of trading performance report sections. Processes chunked data within the 8k-token context window.',
        'ollama',
        'qwen3.5:9b-fast',
        E'You are a concise financial performance analyst. You summarize trading performance data into clear, professional prose.\n\nSTRICT RULES:\n1. Do NOT fabricate any data not present in the input.\n2. Do NOT add opinions, predictions, or recommendations.\n3. Keep each summary under 200 words.\n4. Highlight notable trends, outliers, and changes from prior periods.\n5. Use precise numbers from the input data.\n6. Use a neutral, professional tone.\n7. Return ONLY the summary text. No JSON, no markdown, no commentary.',
        'report-summarizer-v1',
        '1.0.0',
        0.0,
        1024,
        60,
        2,
        'system'
    )
) AS v(name, slug, purpose, model_provider, model_name, system_prompt, prompt_version, schema_version, temperature, max_tokens, timeout_seconds, max_retries, source)
WHERE NOT EXISTS (SELECT 1 FROM ai_agents WHERE slug = 'report-summarizer');
