"""Map DB subscription plans to public API shapes (pricing display rules)."""
from __future__ import annotations

from typing import Literal, Optional, Tuple

from src.domain.admin.models import SubscriptionPlan
from src.domain.admin.schemas import SubscriptionPlanResponse
from src.domain.billing.campaign_service import PlanOffer
from src.domain.billing.constants import ENTERPRISE_PLAN_SLUG
from src.domain.billing.schemas import PlanOfferPublic, SubscriptionPlanPublic

YearlyPriceSource = Literal["catalog", "estimated_monthly_x12"]


def _resolved_yearly_cents_and_source(
    plan: SubscriptionPlan,
) -> Tuple[Optional[int], Optional[YearlyPriceSource]]:
    """Yearly amount in cents and whether it came from DB or is 12× monthly (not Stripe-discounted)."""
    if plan.slug == ENTERPRISE_PLAN_SLUG:
        return None, None
    if plan.price_yearly is not None:
        return plan.price_yearly, "catalog"
    if plan.price_monthly is not None:
        return plan.price_monthly * 12, "estimated_monthly_x12"
    return None, None


def _offer_public(plan: SubscriptionPlan, offer: Optional[PlanOffer]) -> Optional[PlanOfferPublic]:
    """Campaign prices never apply to Enterprise (custom pricing)."""
    if offer is None or plan.slug == ENTERPRISE_PLAN_SLUG:
        return None
    return PlanOfferPublic(
        campaign_slug=offer.campaign_slug,
        price_monthly=offer.price_monthly,
        price_yearly=offer.price_yearly,
        spots_remaining=offer.spots_remaining,
    )


def subscription_plan_to_public(
    plan: SubscriptionPlan, offer: Optional[PlanOffer] = None
) -> SubscriptionPlanPublic:
    """Billing UI plan row; Enterprise shows no fixed prices (custom / sales)."""
    custom = plan.slug == ENTERPRISE_PLAN_SLUG
    yearly_cents, yearly_src = _resolved_yearly_cents_and_source(plan)
    return SubscriptionPlanPublic(
        id=plan.id,
        name=plan.name,
        slug=plan.slug,
        description=plan.description,
        price_monthly=None if custom else plan.price_monthly,
        price_yearly=None if custom else yearly_cents,
        yearly_price_source=None if custom else yearly_src,
        currency=plan.currency,
        features=plan.features,
        limits=plan.limits,
        sort_order=plan.sort_order,
        trial_days=plan.trial_days,
        custom_pricing=custom,
        offer=_offer_public(plan, offer),
    )


def subscription_plan_response_for_public(
    plan: SubscriptionPlan, offer: Optional[PlanOffer] = None
) -> SubscriptionPlanResponse:
    """Unauthenticated pricing page; same Enterprise and yearly rules as dashboard plans."""
    yearly_cents, yearly_src = _resolved_yearly_cents_and_source(plan)
    base = SubscriptionPlanResponse.model_validate(plan)
    if plan.slug == ENTERPRISE_PLAN_SLUG:
        return base.model_copy(
            update={
                "price_monthly": None,
                "price_yearly": None,
                "custom_pricing": True,
                "yearly_price_source": None,
            }
        )
    return base.model_copy(
        update={
            "price_yearly": yearly_cents,
            "yearly_price_source": yearly_src,
            "offer": _offer_public(plan, offer),
        }
    )
