# app/modules/billing/routes/api/platform.py
"""
Platform pricing API endpoints.

Provides subscription tier and add-on product information
for the marketing homepage and signup flow.

All endpoints are unauthenticated (no authentication required).
"""

from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy.orm import Session

from app.core.database import get_db
from app.exceptions import ResourceNotFoundException
from app.modules.billing.models import SubscriptionTier, TierCode
from app.modules.billing.services.platform_pricing_service import (
    platform_pricing_service,
)

router = APIRouter(prefix="/pricing")


# =============================================================================
# Response Schemas
# =============================================================================


class TierFeature(BaseModel):
    """Feature included in a tier."""

    code: str
    name: str
    description: str | None = None


class TierResponse(BaseModel):
    """Subscription tier details for public display."""

    code: str
    name: str
    description: str | None
    price_monthly: float
    price_annual: float | None
    price_monthly_cents: int
    price_annual_cents: int | None
    feature_codes: list[str] = []
    products_limit: int | None = None
    orders_per_month: int | None = None
    team_members: int | None = None
    is_popular: bool = False
    is_enterprise: bool = False

    class Config:
        from_attributes = True


class AddOnResponse(BaseModel):
    """Add-on product details for public display."""

    code: str
    name: str
    description: str | None
    category: str
    price: float  # Price in euros
    price_cents: int
    billing_period: str
    quantity_unit: str | None
    quantity_value: int | None

    class Config:
        from_attributes = True


class PricingResponse(BaseModel):
    """Complete pricing information."""

    tiers: list[TierResponse]
    addons: list[AddOnResponse]
    trial_days: int
    annual_discount_months: int  # e.g., 2 = "2 months free"


# =============================================================================
# Feature Descriptions
# =============================================================================

FEATURE_DESCRIPTIONS = {
    "letzshop_sync": "Letzshop Order Sync",
    "inventory_basic": "Basic Inventory Management",
    "inventory_locations": "Warehouse Locations",
    "inventory_purchase_orders": "Purchase Orders",
    "invoice_lu": "Luxembourg VAT Invoicing",
    "invoice_eu_vat": "EU VAT Invoicing",
    "invoice_bulk": "Bulk Invoicing",
    "customer_view": "Customer List",
    "customer_export": "Customer Export",
    "analytics_dashboard": "Analytics Dashboard",
    "accounting_export": "Accounting Export",
    "api_access": "API Access",
    "automation_rules": "Automation Rules",
    "team_roles": "Team Roles & Permissions",
    "white_label": "White-Label Option",
    "multi_store": "Multi-Store Support",
    "custom_integrations": "Custom Integrations",
    "sla_guarantee": "SLA Guarantee",
    "dedicated_support": "Dedicated Account Manager",
}


# =============================================================================
# Helper Functions
# =============================================================================


def _tier_to_response(tier: SubscriptionTier) -> TierResponse:
    """Convert a SubscriptionTier to TierResponse."""
    feature_codes = sorted(tier.get_feature_codes())
    return TierResponse(
        code=tier.code,
        name=tier.name,
        description=tier.description,
        price_monthly=tier.price_monthly_cents / 100,
        price_annual=(tier.price_annual_cents / 100) if tier.price_annual_cents else None,
        price_monthly_cents=tier.price_monthly_cents,
        price_annual_cents=tier.price_annual_cents,
        feature_codes=feature_codes,
        products_limit=tier.get_limit_for_feature("products_limit"),
        orders_per_month=tier.get_limit_for_feature("orders_per_month"),
        team_members=tier.get_limit_for_feature("team_members"),
        is_popular=tier.code == TierCode.PROFESSIONAL.value,
        is_enterprise=tier.code == TierCode.ENTERPRISE.value,
    )


def _addon_to_response(addon) -> AddOnResponse:
    """Convert an AddOnProduct to AddOnResponse."""
    return AddOnResponse(
        code=addon.code,
        name=addon.name,
        description=addon.description,
        category=addon.category,
        price=addon.price_cents / 100,
        price_cents=addon.price_cents,
        billing_period=addon.billing_period,
        quantity_unit=addon.quantity_unit,
        quantity_value=addon.quantity_value,
    )


# =============================================================================
# Endpoints
# =============================================================================


@router.get("/tiers", response_model=list[TierResponse])  # public
def get_tiers(db: Session = Depends(get_db)) -> list[TierResponse]:
    """Get all public subscription tiers."""
    db_tiers = platform_pricing_service.get_public_tiers(db)
    return [_tier_to_response(tier) for tier in db_tiers]


@router.get("/tiers/{tier_code}", response_model=TierResponse)  # public
def get_tier(tier_code: str, db: Session = Depends(get_db)) -> TierResponse:
    """Get a specific tier by code."""
    tier = platform_pricing_service.get_tier_by_code(db, tier_code)
    if not tier:
        raise ResourceNotFoundException(resource_type="SubscriptionTier", identifier=tier_code)
    return _tier_to_response(tier)


@router.get("/addons", response_model=list[AddOnResponse])  # public
def get_addons(db: Session = Depends(get_db)) -> list[AddOnResponse]:
    """
    Get all available add-on products.

    Returns add-ons from database, or empty list if none configured.
    """
    addons = platform_pricing_service.get_active_addons(db)
    return [_addon_to_response(addon) for addon in addons]


@router.get("", response_model=PricingResponse)  # public
def get_pricing(db: Session = Depends(get_db)) -> PricingResponse:
    """
    Get complete pricing information (tiers + add-ons).

    This is the main endpoint for the pricing page.
    """
    from app.core.config import settings

    return PricingResponse(
        tiers=get_tiers(db),
        addons=get_addons(db),
        trial_days=settings.stripe_trial_days,
        annual_discount_months=2,  # "2 months free" with annual billing
    )
