"""Tenant schemas for multi-tenancy management."""

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

from pydantic import BaseModel, Field

from vikdata.models.common import PaginatedResponse


class TenantBase(BaseModel):
    """Base tenant model."""

    name: str = Field(..., min_length=1, max_length=255, description="Tenant display name")
    slug: str = Field(
        ...,
        min_length=1,
        max_length=63,
        pattern=r"^[a-z0-9]([a-z0-9-]*[a-z0-9])?$",
        description="Tenant slug (used in URLs/subdomains)",
    )


class TenantCreate(TenantBase):
    """Tenant creation request.

    Only admin users can create tenants.
    """

    settings: dict[str, Any] | None = Field(
        default=None, description="Tenant-specific configuration settings"
    )


class TenantUpdate(BaseModel):
    """Tenant update request.

    All fields are optional.
    """

    name: str | None = Field(
        default=None, min_length=1, max_length=255, description="Tenant display name"
    )
    settings: dict[str, Any] | None = Field(
        default=None, description="Tenant-specific configuration settings"
    )


class TenantSettingsUpdate(BaseModel):
    """Partial tenant settings update request.

    Updates are merged with existing settings (shallow merge).
    """

    settings: dict[str, Any] = Field(..., description="Settings to merge with existing settings")


class TenantResponse(TenantBase):
    """Tenant response model."""

    id: UUID = Field(..., description="Tenant ID")
    status: str = Field(..., description="Tenant status (active, suspended, deleted)")
    settings: dict[str, Any] = Field(
        default_factory=dict, description="Tenant-specific configuration settings"
    )
    created_at: datetime = Field(..., description="Tenant creation timestamp")
    updated_at: datetime = Field(..., description="Last update timestamp")
    deleted_at: datetime | None = Field(
        default=None, description="Soft deletion timestamp (null if active)"
    )


class TenantListResponse(PaginatedResponse[TenantResponse]):
    """List of tenants response with pagination."""

    pass


class TenantStatusResponse(BaseModel):
    """Response for status change operations."""

    id: UUID = Field(..., description="Tenant ID")
    status: str = Field(..., description="New tenant status")
    message: str = Field(..., description="Status change confirmation message")
