CREATE TABLE IF NOT EXISTS ai_engineer_insights (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    status VARCHAR(50) NOT NULL DEFAULT 'PENDING_REVIEW', -- PENDING_REVIEW, APPROVED, REJECTED, IMPLEMENTED
    issue_summary TEXT NOT NULL,
    root_cause_analysis TEXT NOT NULL,
    proposed_solution TEXT NOT NULL,
    suggested_code_diff TEXT, -- Git diff format
    metrics_affected JSONB NOT NULL DEFAULT '{}'::jsonb
);

CREATE TABLE IF NOT EXISTS error_telemetry (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    source VARCHAR(50) NOT NULL, -- FRONTEND, BACKEND, SYSTEM
    error_level VARCHAR(20) NOT NULL,
    message TEXT NOT NULL,
    stack_trace TEXT,
    user_context JSONB NOT NULL DEFAULT '{}'::jsonb,
    analyzed BOOLEAN NOT NULL DEFAULT false
);

CREATE INDEX IF NOT EXISTS idx_error_telemetry_analyzed ON error_telemetry(analyzed);
