from fastapi import APIRouter, Depends, HTTPException from fastapi.responses import HTMLResponse from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select import json from app.database import get_db from app.models.scan import Scan from app.models.product import Product router = APIRouter(prefix="/api", tags=["share"]) @router.get("/scan/{scan_id}/share", response_class=HTMLResponse) async def share_scan(scan_id: int, db: AsyncSession = Depends(get_db)): res = await db.execute(select(Scan).where(Scan.id == scan_id)) scan = res.scalar_one_or_none() if not scan: raise HTTPException(status_code=404, detail="Scan não encontrado") analysis = json.loads(scan.analysis_json or '{}') score = scan.score or 0 color = '#10B981' if score >= 70 else '#EAB308' if score >= 50 else '#F97316' if score >= 30 else '#EF4444' label = 'Excelente' if score >= 90 else 'Bom' if score >= 70 else 'Regular' if score >= 50 else 'Ruim' if score >= 30 else 'Péssimo' positives = ''.join(f'
  • ✅ {p}
  • ' for p in analysis.get("positives", [])) negatives = ''.join(f'
  • ❌ {n}
  • ' for n in analysis.get("negatives", [])) html = f""" ALETHEIA - {scan.product_name}
    {score}/100

    {scan.product_name or 'Produto'}

    {scan.brand or ''}

    {label}

    {scan.summary or ''}

    {f'' if positives else ''} {f'' if negatives else ''}
    """ return HTMLResponse(content=html)