from typing import Optional

from db.db import get_db
from db.models import InvestorTable, ProjectTable
from fastapi import APIRouter, Depends, HTTPException
from schemas.insight_schema import InsightResponse
from services.compatibility_score import (
    calculate_project_investor_compatibility,
    generate_compatibility_explanation,
)
from services.insight import QueryProcessor
from sqlalchemy.orm import Session

router = APIRouter()


@router.get(
    "/insights/{investor_id}", response_model=InsightResponse, tags=["Insights"]
)
async def get_insights(
    investor_id: int, project_id: Optional[int] = None, db: Session = Depends(get_db)
):
    """
    Get investor insights including investment pattern analysis, market position,
    and optionally compatibility score with a project.

    Args:
        investor_id: The ID of the investor to analyze
        project_id: Optional project ID to calculate compatibility score

    Returns:
        InsightResponse with investment_pattern_analysis, market_position,
        and compatibility_score (if project_id provided)
    """
    # Get investor from database
    investor = db.query(InvestorTable).filter(InvestorTable.id == investor_id).first()
    if not investor:
        raise HTTPException(
            status_code=404, detail=f"Investor with id {investor_id} not found"
        )

    # Initialize the query processor for insights
    query_processor = QueryProcessor()

    # Get investment pattern analysis and market position using web search
    insights = await query_processor.get_investor_insights(
        investor_name=investor.name,
        investor_website=investor.website,
        investor_description=investor.description,
        investor_headquarters=investor.headquarters,
        investment_thesis=investor.investment_thesis,
        portfolio_highlights=investor.portfolio_highlights,
    )

    # Calculate compatibility score if project_id is provided
    compatibility_score = None
    if project_id:
        project = db.query(ProjectTable).filter(ProjectTable.id == project_id).first()
        if not project:
            raise HTTPException(
                status_code=404, detail=f"Project with id {project_id} not found"
            )

        # Calculate the compatibility score
        score = calculate_project_investor_compatibility(
            project, investor, use_funds=True
        )

        # Generate detailed explanation
        compatibility_score = generate_compatibility_explanation(
            project, investor, score, use_funds=True
        )
    else:
        compatibility_score = "Select a project to see compatibility analysis"

    return InsightResponse(
        investment_pattern_analysis=insights["investment_pattern_analysis"],
        market_position=insights["market_position"],
        compatibility_score=compatibility_score,
    )
