"""Admin models for vikdata API (superadmin dashboard).

ALL field names use snake_case to match the backend API.

NOTE: Types like status, tier, pricing_model are stored as strings.
Valid values come from database lookup tables, not hardcoded enums.
"""

from datetime import datetime
from typing import Any
from uuid import UUID

from pydantic import BaseModel, Field

from .billing import ResourceLimits, UsagePricingConfig


# =============================================================================
# Dashboard Stats
# =============================================================================


class AdminStats(BaseModel):
    """Admin dashboard statistics."""

    tenant_count: int = Field(..., ge=0, description="Total tenant count")
    active_tenant_count: int = Field(..., ge=0, description="Active tenant count")
    suspended_tenant_count: int = Field(..., ge=0, description="Suspended tenant count")
    user_count: int = Field(..., ge=0, description="Total user count")
    subscription_count: int = Field(..., ge=0, description="Total subscription count")
    trial_count: int = Field(..., ge=0, description="Subscriptions in trial")
    mrr: int = Field(..., ge=0, description="Monthly recurring revenue in cents")
    arr: int = Field(..., ge=0, description="Annual recurring revenue in cents")


# =============================================================================
# Admin Subscription (cross-tenant view)
# =============================================================================


class AdminSubscription(BaseModel):
    """Admin subscription (cross-tenant view)."""

    id: UUID = Field(..., description="Subscription ID")
    tenant_id: UUID = Field(..., description="Tenant ID")
    tenant_name: str = Field(..., description="Tenant name")
    tenant_slug: str = Field(..., description="Tenant slug")
    series_id: UUID = Field(..., description="Series ID")
    series_name: str = Field(..., description="Series name")
    series_slug: str = Field(..., description="Series slug")
    plan_id: UUID = Field(..., description="Plan ID")
    plan_name: str = Field(..., description="Plan name")
    plan_slug: str = Field(..., description="Plan slug")
    status: str = Field(..., description="Subscription status (from lookup)")
    price_monthly: int = Field(..., ge=0, description="Monthly price in cents")
    current_period_start: datetime | None = Field(default=None, description="Current period start")
    current_period_end: datetime | None = Field(default=None, description="Current period end")
    trial_ends_at: datetime | None = Field(default=None, description="Trial end date")
    canceled_at: datetime | None = Field(default=None, description="Cancellation date")
    created_at: datetime = Field(..., description="Creation timestamp")


class AdminSubscriptionListParams(BaseModel):
    """Query parameters for listing admin subscriptions."""

    page: int | None = Field(default=1, ge=1, description="Page number")
    per_page: int | None = Field(default=20, ge=1, le=100, description="Items per page")
    status: str | None = Field(default=None, description="Filter by status (from lookup)")


# =============================================================================
# Admin Plan Management
# =============================================================================


class AdminPlan(BaseModel):
    """Admin plan response (full details including private)."""

    id: UUID = Field(..., description="Plan ID")
    series_id: UUID = Field(..., description="Series ID")
    series_name: str = Field(..., description="Series name")
    series_slug: str = Field(..., description="Series slug")
    name: str = Field(..., description="Plan name")
    slug: str = Field(..., description="URL-friendly slug")
    description: str | None = Field(default=None, description="Plan description")
    tier: str = Field(..., description="Plan tier (from lookup)")
    pricing_model: str = Field(..., description="Pricing model (from lookup)")
    price_monthly: int = Field(..., ge=0, description="Monthly price in cents")
    price_yearly: int = Field(..., ge=0, description="Yearly price in cents")
    usage_pricing: dict[str, UsagePricingConfig] | None = Field(default=None, description="Usage pricing")
    resource_limits: ResourceLimits | None = Field(default=None, description="Resource limits")
    features: dict[str, Any] | None = Field(default=None, description="Feature flags")
    is_public: bool = Field(..., description="Whether plan is publicly available")
    trial_days: int = Field(..., ge=0, description="Trial period in days")
    display_order: int = Field(..., description="Display order")
    created_at: datetime = Field(..., description="Creation timestamp")
    updated_at: datetime = Field(..., description="Last update timestamp")


class CreatePlanRequest(BaseModel):
    """Create plan request."""

    series_id: UUID = Field(..., description="Series ID")
    name: str = Field(..., min_length=1, max_length=100, description="Plan name")
    slug: str = Field(..., min_length=1, max_length=100, description="URL-friendly slug")
    description: str | None = Field(default=None, max_length=1000, description="Plan description")
    tier: str = Field(default="basic", description="Plan tier (from lookup)")
    pricing_model: str = Field(default="fixed", description="Pricing model (from lookup)")
    price_monthly: int = Field(default=0, ge=0, description="Monthly price in cents")
    price_yearly: int = Field(default=0, ge=0, description="Yearly price in cents")
    usage_pricing: dict[str, UsagePricingConfig] | None = Field(default=None, description="Usage pricing")
    resource_limits: ResourceLimits | None = Field(default=None, description="Resource limits")
    features: dict[str, Any] | None = Field(default=None, description="Feature flags")
    is_public: bool = Field(default=True, description="Whether plan is publicly available")
    trial_days: int = Field(default=0, ge=0, description="Trial period in days")
    display_order: int = Field(default=0, description="Display order")


class UpdatePlanRequest(BaseModel):
    """Update plan request."""

    name: str | None = Field(default=None, min_length=1, max_length=100, description="Plan name")
    description: str | None = Field(default=None, max_length=1000, description="Plan description")
    tier: str | None = Field(default=None, description="Plan tier (from lookup)")
    pricing_model: str | None = Field(default=None, description="Pricing model (from lookup)")
    price_monthly: int | None = Field(default=None, ge=0, description="Monthly price in cents")
    price_yearly: int | None = Field(default=None, ge=0, description="Yearly price in cents")
    usage_pricing: dict[str, UsagePricingConfig] | None = Field(default=None, description="Usage pricing")
    resource_limits: ResourceLimits | None = Field(default=None, description="Resource limits")
    features: dict[str, Any] | None = Field(default=None, description="Feature flags")
    is_public: bool | None = Field(default=None, description="Whether plan is publicly available")
    trial_days: int | None = Field(default=None, ge=0, description="Trial period in days")
    display_order: int | None = Field(default=None, description="Display order")


class AdminPlanListParams(BaseModel):
    """Query parameters for listing admin plans."""

    include_private: bool | None = Field(default=None, description="Include private plans")
