# app/modules/hosting/models/client_service.py
"""
ClientService model - operational tracking for services.

Complements billing StoreAddOn with operational details like domain expiry,
registrar info, and mailbox counts.
"""

import enum

from sqlalchemy import (
    Boolean,
    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 ServiceType(str, enum.Enum):
    DOMAIN = "domain"
    EMAIL = "email"
    SSL = "ssl"
    HOSTING = "hosting"
    WEBSITE_MAINTENANCE = "website_maintenance"


class ClientServiceStatus(str, enum.Enum):
    PENDING = "pending"
    ACTIVE = "active"
    SUSPENDED = "suspended"
    EXPIRED = "expired"
    CANCELLED = "cancelled"


class BillingPeriod(str, enum.Enum):
    MONTHLY = "monthly"
    ANNUAL = "annual"
    ONE_TIME = "one_time"


class ClientService(Base, TimestampMixin):
    """Represents an operational service for a hosted site."""

    __tablename__ = "client_services"

    id = Column(Integer, primary_key=True, index=True)
    hosted_site_id = Column(
        Integer,
        ForeignKey("hosted_sites.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )
    service_type = Column(Enum(ServiceType), nullable=False)
    name = Column(String(255), nullable=False)
    description = Column(Text, nullable=True)
    status = Column(
        Enum(ClientServiceStatus),
        nullable=False,
        default=ClientServiceStatus.PENDING,
    )

    # Billing
    billing_period = Column(Enum(BillingPeriod), nullable=True)
    price_cents = Column(Integer, nullable=True)
    currency = Column(String(3), nullable=False, default="EUR")
    addon_product_id = Column(
        Integer,
        ForeignKey("addon_products.id", ondelete="SET NULL"),
        nullable=True,
    )

    # Domain-specific
    domain_name = Column(String(255), nullable=True)
    registrar = Column(String(100), nullable=True)

    # Email-specific
    mailbox_count = Column(Integer, nullable=True)

    # Expiry and renewal
    expires_at = Column(DateTime, nullable=True)
    period_start = Column(DateTime, nullable=True)
    period_end = Column(DateTime, nullable=True)
    auto_renew = Column(Boolean, nullable=False, default=True)

    # Notes
    notes = Column(Text, nullable=True)

    # Relationships
    hosted_site = relationship("HostedSite", back_populates="client_services")
