# app/modules/hosting/models/hosted_site.py
"""
HostedSite model - links a Store to a Prospect and tracks the POC → live lifecycle.

Lifecycle: draft → poc_ready → proposal_sent → accepted → live → suspended | cancelled
"""

import enum

from sqlalchemy import Column, DateTime, Enum, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship

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


class HostedSiteStatus(str, enum.Enum):
    DRAFT = "draft"
    POC_READY = "poc_ready"
    PROPOSAL_SENT = "proposal_sent"
    ACCEPTED = "accepted"
    LIVE = "live"
    SUSPENDED = "suspended"
    CANCELLED = "cancelled"


class HostedSite(Base, TimestampMixin):
    """Represents a hosted website linking a Store to a Prospect."""

    __tablename__ = "hosted_sites"

    id = Column(Integer, primary_key=True, index=True)
    store_id = Column(
        Integer,
        ForeignKey("stores.id", ondelete="CASCADE"),
        nullable=False,
        unique=True,
    )
    prospect_id = Column(
        Integer,
        ForeignKey("prospects.id", ondelete="SET NULL"),
        nullable=True,
    )
    status = Column(
        Enum(HostedSiteStatus),
        nullable=False,
        default=HostedSiteStatus.DRAFT,
    )

    # Denormalized for dashboard display
    business_name = Column(String(255), nullable=False)
    contact_name = Column(String(255), nullable=True)
    contact_email = Column(String(255), nullable=True)
    contact_phone = Column(String(50), nullable=True)

    # Lifecycle timestamps
    proposal_sent_at = Column(DateTime, nullable=True)
    proposal_accepted_at = Column(DateTime, nullable=True)
    went_live_at = Column(DateTime, nullable=True)

    # Proposal
    proposal_notes = Column(Text, nullable=True)

    # Denormalized from StoreDomain
    live_domain = Column(String(255), nullable=True, unique=True)

    # Internal notes
    internal_notes = Column(Text, nullable=True)

    # Relationships
    store = relationship("Store", backref="hosted_site", uselist=False)
    prospect = relationship("Prospect", backref="hosted_sites")
    client_services = relationship(
        "ClientService",
        back_populates="hosted_site",
        cascade="all, delete-orphan",
    )

    @property
    def display_name(self) -> str:
        return self.business_name or f"Site #{self.id}"
