"""Pydantic models for task template APIs."""

from datetime import date, datetime
from typing import Literal, Optional

from pydantic import BaseModel, Field


TemplateType = Literal["global", "company", "internal", "deactivated"]
TemplateCategory = Literal[
    "onboarding",
    "offboarding",
    "simkort",
    "hardwarebestilling",
    "brugerandring",
    "andet",
]
TemplateItemType = Literal["task", "subcase"]
RunMode = Literal["tasks", "subcases", "combined"]
AssigneeMode = Literal["template_default", "specific_user", "specific_role"]


class TaskTemplateBase(BaseModel):
    name: str = Field(..., min_length=1, max_length=255)
    description: Optional[str] = None
    template_type: TemplateType = "global"
    customer_id: Optional[int] = None
    category: TemplateCategory = "andet"
    is_active: bool = True


class TaskTemplateCreate(TaskTemplateBase):
    pass


class TaskTemplateUpdate(BaseModel):
    name: Optional[str] = Field(None, min_length=1, max_length=255)
    description: Optional[str] = None
    template_type: Optional[TemplateType] = None
    customer_id: Optional[int] = None
    category: Optional[TemplateCategory] = None
    is_active: Optional[bool] = None


class TaskTemplate(TaskTemplateBase):
    id: int
    created_by: Optional[int] = None
    created_at: datetime
    updated_at: datetime


class TaskTemplateItemBase(BaseModel):
    title: str = Field(..., min_length=1, max_length=255)
    description: Optional[str] = None
    item_type: TemplateItemType = "task"
    default_assignee_user_id: Optional[int] = None
    default_assignee_role_id: Optional[int] = None
    days_offset: Optional[int] = 0
    sort_order: int = 0
    is_required: bool = True
    is_active: bool = True


class TaskTemplateItemCreate(TaskTemplateItemBase):
    pass


class TaskTemplateItemUpdate(BaseModel):
    title: Optional[str] = Field(None, min_length=1, max_length=255)
    description: Optional[str] = None
    item_type: Optional[TemplateItemType] = None
    default_assignee_user_id: Optional[int] = None
    default_assignee_role_id: Optional[int] = None
    days_offset: Optional[int] = None
    sort_order: Optional[int] = None
    is_required: Optional[bool] = None
    is_active: Optional[bool] = None


class TaskTemplateItem(TaskTemplateItemBase):
    id: int
    template_id: int
    created_at: datetime
    updated_at: datetime


class TemplatePreviewRequest(BaseModel):
    template_id: int
    start_date: Optional[date] = None
    mode: RunMode = "combined"
    assignee_mode: AssigneeMode = "template_default"
    assignee_user_id: Optional[int] = None
    assignee_role_id: Optional[int] = None


class TemplateRunRequest(TemplatePreviewRequest):
    pass
