# app/modules/prospecting/models/performance_profile.py
"""
Performance profile for a prospect's website.

Stores Lighthouse audit results including Core Web Vitals,
mobile-friendliness, and asset size analysis.
"""

from sqlalchemy import Boolean, Column, Float, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship

from app.core.database import Base
from models.database.base import TimestampMixin


class ProspectPerformanceProfile(Base, TimestampMixin):
    """Performance audit results from PageSpeed Insights / Lighthouse."""

    __tablename__ = "prospect_performance_profiles"

    id = Column(Integer, primary_key=True, index=True)
    prospect_id = Column(Integer, ForeignKey("prospects.id", ondelete="CASCADE"), nullable=False, unique=True)

    # Lighthouse Scores (0-100)
    performance_score = Column(Integer, nullable=True)
    accessibility_score = Column(Integer, nullable=True)
    best_practices_score = Column(Integer, nullable=True)
    seo_score = Column(Integer, nullable=True)

    # Core Web Vitals
    first_contentful_paint_ms = Column(Integer, nullable=True)
    largest_contentful_paint_ms = Column(Integer, nullable=True)
    total_blocking_time_ms = Column(Integer, nullable=True)
    cumulative_layout_shift = Column(Float, nullable=True)
    speed_index = Column(Integer, nullable=True)
    time_to_interactive_ms = Column(Integer, nullable=True)

    # Mobile
    is_mobile_friendly = Column(Boolean, nullable=True)
    viewport_configured = Column(Boolean, nullable=True)
    font_size_ok = Column(Boolean, nullable=True)
    tap_targets_ok = Column(Boolean, nullable=True)

    # Asset Sizes (bytes)
    total_bytes = Column(Integer, nullable=True)
    html_bytes = Column(Integer, nullable=True)
    css_bytes = Column(Integer, nullable=True)
    js_bytes = Column(Integer, nullable=True)
    image_bytes = Column(Integer, nullable=True)
    font_bytes = Column(Integer, nullable=True)

    # Request Counts
    total_requests = Column(Integer, nullable=True)
    js_requests = Column(Integer, nullable=True)
    css_requests = Column(Integer, nullable=True)
    image_requests = Column(Integer, nullable=True)

    # Raw data
    lighthouse_json = Column(Text, nullable=True)  # JSON string
    scan_strategy = Column(String(20), nullable=True)  # mobile or desktop
    scan_error = Column(Text, nullable=True)

    # Relationships
    prospect = relationship("Prospect", back_populates="performance_profile")
