import uuid
from datetime import datetime
from typing import Any, Optional
from pydantic import BaseModel, ConfigDict, Field


class LocaleResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    code: str
    name: str
    native_name: str
    flag: str
    is_rtl: bool
    is_active: bool


class SetLocaleRequest(BaseModel):
    locale: str = Field(min_length=2, max_length=10)


class SaveTranslationRequest(BaseModel):
    locale: str = Field(min_length=2, max_length=10)
    namespace: str = Field(default="bot", min_length=2, max_length=50)
    key: str = Field(min_length=1, max_length=100)
    value: str
    tenant_id: Optional[str] = None
    is_approved: bool = True


class TranslationBundleResponse(BaseModel):
    locale: str
    namespace: str
    translations: dict[str, str]


class TranslationItemResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: uuid.UUID
    tenant_id: Optional[uuid.UUID]
    locale: str
    namespace: str
    key: str
    value: str
    is_approved: bool
    created_at: datetime
    updated_at: datetime


class MissingTranslationsResponse(BaseModel):
    locale: str
    namespace: str
    total_keys: int
    translated_keys: int
    missing_keys: list[str]
    coverage_percentage: float


class AuditSummaryResponse(BaseModel):
    locales: dict[str, dict[str, Any]]
    total_locales: int
    overall_coverage: float


class AITranslationRequest(BaseModel):
    key: str
    source_text: str
    source_locale: str = "en"
    target_locale: str
    namespace: str = "bot"
    auto_save_as_pending: bool = True


class AITranslationResponse(BaseModel):
    key: str
    source_text: str
    source_locale: str
    target_locale: str
    suggested_translation: str
    is_approved: bool = False
    translation_id: Optional[uuid.UUID] = None


class TenantLanguageSettings(BaseModel):
    default_locale: str
    enabled_locales: list[str]


class UpdateTenantLanguageSettings(BaseModel):
    default_locale: Optional[str] = None
    enabled_locales: Optional[list[str]] = None


class BotLocaleRequest(BaseModel):
    locale: str = Field(min_length=2, max_length=10)

