from fastapi import APIRouter, Depends
from sqlalchemy import select
from sqlalchemy.orm import Session

from app.api.deps import AuthSession, require_client_access_manager_session, require_client_module_access
from app.db.session import get_db
from app.models.mix import Mix
from app.models.product import Product
from app.models.raw_material import RawMaterial
from app.models.scenario import Scenario
from app.services.client_access_service import build_client_access_export, list_client_accounts
from app.services.costing_engine import calculate_mix_cost, calculate_product_cost, serialize_raw_material

router = APIRouter(prefix="/api/powerbi", tags=["powerbi"])


@router.get("/raw-material-costs")
def raw_material_costs(session: AuthSession = Depends(require_client_module_access("raw_materials")), db: Session = Depends(get_db)):
    materials = db.scalars(select(RawMaterial).where(RawMaterial.tenant_id == session.tenant_id).order_by(RawMaterial.name)).all()
    return [serialize_raw_material(material) for material in materials]


@router.get("/mix-costs")
def mix_costs(session: AuthSession = Depends(require_client_module_access("mix_master")), db: Session = Depends(get_db)):
    mixes = db.scalars(select(Mix).where(Mix.tenant_id == session.tenant_id).order_by(Mix.name)).all()
    return [calculate_mix_cost(db, mix.id) for mix in mixes]


@router.get("/product-costs")
def product_costs(session: AuthSession = Depends(require_client_module_access("products")), db: Session = Depends(get_db)):
    products = db.scalars(select(Product).where(Product.tenant_id == session.tenant_id).order_by(Product.name)).all()
    return [calculate_product_cost(db, product.id) for product in products]


@router.get("/scenario-results")
def scenario_results(session: AuthSession = Depends(require_client_module_access("scenarios")), db: Session = Depends(get_db)):
    scenarios = db.scalars(select(Scenario).where(Scenario.tenant_id == session.tenant_id).order_by(Scenario.created_at.desc())).all()
    return [
        {
            "scenario_id": scenario.id,
            "scenario_name": scenario.name,
            "status": scenario.status,
            "description": scenario.description,
            "overrides": scenario.overrides,
        }
        for scenario in scenarios
    ]


@router.get("/client-access")
def client_access_export(
    session: AuthSession = Depends(require_client_access_manager_session),
    db: Session = Depends(get_db),
):
    clients = list_client_accounts(db)
    if session.role == "client":
        clients = [client for client in clients if client.id == session.client_account_id]
    return build_client_access_export(clients)


@router.get("/data-quality-issues")
def data_quality_issues(session: AuthSession = Depends(require_client_module_access("dashboard")), db: Session = Depends(get_db)):
    issues: list[dict] = []
    for mix in db.scalars(select(Mix).where(Mix.tenant_id == session.tenant_id)).all():
        result = calculate_mix_cost(db, mix.id)
        for warning in result["warnings"]:
            issues.append({"entity_type": "mix", "entity_id": mix.id, "entity_name": mix.name, "warning": warning})
    for product in db.scalars(select(Product).where(Product.tenant_id == session.tenant_id)).all():
        result = calculate_product_cost(db, product.id)
        for warning in result["warnings"]:
            issues.append({"entity_type": "product", "entity_id": product.id, "entity_name": product.name, "warning": warning})
    for material in db.scalars(select(RawMaterial).where(RawMaterial.tenant_id == session.tenant_id)).all():
        serialized = serialize_raw_material(material)
        if serialized["current_price"] is None:
            issues.append({"entity_type": "raw_material", "entity_id": material.id, "entity_name": material.name, "warning": "No active price"})
    return issues
