"""Pricing, capabilities & bundles DTOs — Phase 10.4."""
from __future__ import annotations

from datetime import datetime
from decimal import Decimal
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import CapabilityBundleStatus, CapabilityStatus, PricingRuleStatus
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class PricingRuleCreate(BaseModel):
    model_config = _FORBID

    organization_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    status: PricingRuleStatus = PricingRuleStatus.DRAFT
    base_amount: Decimal = Field(gt=0)
    currency_code: str = Field(default="IRR", max_length=3)
    rule_config: dict | None = None
    metadata_json: dict | None = None


class PricingRuleUpdate(BaseModel):
    model_config = _FORBID

    name: str | None = Field(default=None, max_length=255)
    status: PricingRuleStatus | None = None
    base_amount: Decimal | None = Field(default=None, gt=0)
    currency_code: str | None = Field(default=None, max_length=3)
    rule_config: dict | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class PricingRuleRead(ORMBase):
    id: UUID
    tenant_id: UUID
    organization_id: UUID
    code: str
    name: str
    status: PricingRuleStatus
    base_amount: Decimal
    currency_code: str
    rule_config: dict | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class PricingRuleListResponse(BaseModel):
    items: list[PricingRuleRead]
    total: int
    page: int
    page_size: int


class CapabilityCreate(BaseModel):
    model_config = _FORBID

    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    status: CapabilityStatus = CapabilityStatus.ACTIVE
    metadata_json: dict | None = None


class CapabilityUpdate(BaseModel):
    model_config = _FORBID

    name: str | None = Field(default=None, max_length=255)
    description: str | None = None
    status: CapabilityStatus | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class CapabilityRead(ORMBase):
    id: UUID
    tenant_id: UUID
    code: str
    name: str
    description: str | None
    status: CapabilityStatus
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class CapabilityListResponse(BaseModel):
    items: list[CapabilityRead]
    total: int
    page: int
    page_size: int


class BundleCreate(BaseModel):
    model_config = _FORBID

    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    status: CapabilityBundleStatus = CapabilityBundleStatus.DRAFT
    capability_codes: list[str] | None = None
    pricing_rule_id: UUID | None = None
    metadata_json: dict | None = None


class BundleUpdate(BaseModel):
    model_config = _FORBID

    name: str | None = Field(default=None, max_length=255)
    status: CapabilityBundleStatus | None = None
    capability_codes: list[str] | None = None
    pricing_rule_id: UUID | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class BundleRead(ORMBase):
    id: UUID
    tenant_id: UUID
    code: str
    name: str
    status: CapabilityBundleStatus
    capability_codes: list | None
    pricing_rule_id: UUID | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class BundleListResponse(BaseModel):
    items: list[BundleRead]
    total: int
    page: int
    page_size: int
