"""Pydantic schemas — define the JSON shape of every request and response.

These also drive the auto-generated OpenAPI/Swagger docs at /docs.
The `example` values shown in each Field() appear in the Swagger UI as
pre-filled example payloads — handy for the handoff team to click 'Try it out'."""
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, EmailStr, Field


# ============================================================
# Product
# ============================================================
class ProductCreate(BaseModel):
    product_code: str = Field(..., max_length=50, examples=["WEBAPP"])
    product_name: str = Field(..., max_length=200, examples=["Web Application Suite"])


class ProductOut(BaseModel):
    product_code: str
    product_name: str

    class Config:
        from_attributes = True


# ============================================================
# System
# ============================================================
class SystemCreate(BaseModel):
    system_code: str = Field(..., max_length=50, examples=["WEBAPP-AUTH"])
    system_name: str = Field(..., max_length=200, examples=["Authentication Module"])
    product_code: str = Field(..., max_length=50, examples=["WEBAPP"])


class SystemOut(BaseModel):
    system_code: str
    system_name: str
    product_code: str

    class Config:
        from_attributes = True


# ============================================================
# Support
# ============================================================
class SupportCreate(BaseModel):
    support_email: EmailStr = Field(..., examples=["auth-team@example.com"])
    system_code: str = Field(..., max_length=50, examples=["WEBAPP-AUTH"])


class SupportOut(BaseModel):
    id: int
    support_email: EmailStr
    system_code: str

    class Config:
        from_attributes = True


# ============================================================
# Ticket
# ============================================================
class TicketCreate(BaseModel):
    customer_company: str = Field(..., max_length=200, examples=["Acme Sdn Bhd"])
    customer_name: str = Field(..., max_length=200, examples=["John Tan"])
    customer_mobilephone: str = Field(..., max_length=50, examples=["+60123456789"])
    system_code: str = Field(..., max_length=50, examples=["WEBAPP-AUTH"])
    product_code: str = Field(..., max_length=50, examples=["WEBAPP"])
    issue: str = Field(
        ...,
        examples=["Cannot log in — receiving 'invalid token' error after password reset."],
    )


class TicketOut(BaseModel):
    id: int
    customer_company: str
    customer_name: str
    customer_mobilephone: str
    system_code: str
    product_code: str
    issue: str
    devops_work_item_id: Optional[int] = None
    devops_url: Optional[str] = None
    assigned_to: Optional[str] = None
    created_at: datetime

    class Config:
        from_attributes = True
