#!/usr/bin/env python3
"""Regenerate synthesis with new report format."""
import asyncio
import os
import sys
import uuid

sys.path.insert(0, '/app/packages/reviewiq-pipeline/src')
sys.path.insert(0, '/app/packages/pipeline-core/src')

async def main():
    import asyncpg
    from reviewiq_pipeline.config import Config
    from reviewiq_pipeline.services.llm_client import LLMClient
    from reviewiq_pipeline.stages.stage5_synthesize import Stage5Synthesizer

    job_id = "a3813665-ea23-4fb0-aab7-b282ef9443e4"

    database_url = os.getenv(
        'DATABASE_URL',
        'postgresql://scraper:scraper123@scraper-db:5432/scraper'
    )

    print("Connecting to database...")
    pool = await asyncpg.create_pool(database_url)

    # Check if execution exists for this job, create one if not
    print("Checking for existing execution...")
    row = await pool.fetchrow(
        "SELECT id FROM pipeline.executions WHERE job_id = $1::uuid ORDER BY created_at DESC LIMIT 1",
        job_id
    )

    if row:
        execution_id = str(row['id'])
        print(f"Found existing execution: {execution_id}")
    else:
        execution_id = str(uuid.uuid4())
        print(f"Creating new execution: {execution_id}")
        await pool.execute("""
            INSERT INTO pipeline.executions (id, pipeline_id, job_id, status, stages_requested, created_at)
            VALUES ($1::uuid, 'reviewiq', $2::uuid, 'running', ARRAY['synthesize'], NOW())
        """, execution_id, job_id)

    print("Creating LLM client...")
    config = Config()
    llm_client = LLMClient.create(config)

    try:
        print(f"Generating analyst report for job {job_id}...")
        stage5 = Stage5Synthesizer(pool=pool, llm_client=llm_client)
        synthesis = await stage5.run(job_id, execution_id)

        # Mark execution as completed
        await pool.execute(
            "UPDATE pipeline.executions SET status = 'completed', completed_at = NOW() WHERE id = $1::uuid",
            execution_id
        )

        print(f"\n{'='*70}")
        print("ANALYST REPORT GENERATED")
        print(f"{'='*70}")
        print(f"\nHEADLINE: {synthesis.headline}")
        print(f"\nVERDICT: {synthesis.verdict}")
        print(f"\nRATING: {synthesis.current_rating:.1f} → {synthesis.potential_rating:.1f} (gap: +{synthesis.rating_gap:.1f})")
        print(f"\nNARRATIVE:\n{synthesis.narrative[:500]}...")
        print(f"\nPRIMARY PROBLEM: {synthesis.primary_problem}")
        print(f"ROOT CAUSE: {synthesis.root_cause}")
        print(f"\nACTIONS ({len(synthesis.actions)}):")
        for a in synthesis.actions:
            print(f"  [{a.priority}] {a.action}")
            print(f"      Owner: {a.owner} | Impact: {a.impact}")
        print(f"\nEVIDENCE ({len(synthesis.evidence)}):")
        for e in synthesis.evidence[:3]:
            print(f"  [{e.sentiment}] \"{e.quote[:60]}...\"")
            print(f"      Context: {e.context}")

    finally:
        await llm_client.close()
        await pool.close()

if __name__ == "__main__":
    asyncio.run(main())
