import uuid
from datetime import datetime
from typing import Optional

from pydantic import BaseModel, Field


class DetectionTaskCreate(BaseModel):
    brand_id: uuid.UUID
    name: str = Field(..., min_length=1, max_length=200)
    frequency: str = Field(..., pattern="^(hourly|daily|weekly)$")
    engines: list[str] = Field(default=[], min_length=1)
    queries: list[str] = Field(default=[], min_length=1)
    competitor_names: Optional[list[str]] = None


class DetectionTaskUpdate(BaseModel):
    name: Optional[str] = Field(None, min_length=1, max_length=200)
    frequency: Optional[str] = Field(None, pattern="^(hourly|daily|weekly)$")
    engines: Optional[list[str]] = None
    queries: Optional[list[str]] = None
    competitor_names: Optional[list[str]] = None
    is_active: Optional[bool] = None


class DetectionTaskResponse(BaseModel):
    id: uuid.UUID
    brand_id: uuid.UUID
    user_id: uuid.UUID
    name: str
    frequency: str
    engines: list[str]
    queries: list[str]
    competitor_names: Optional[list[str]] = None
    is_active: bool
    last_run_at: Optional[datetime] = None
    next_run_at: Optional[datetime] = None
    created_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}


class DetectionTaskListResponse(BaseModel):
    items: list[DetectionTaskResponse]
    total: int


class DetectionTriggerResponse(BaseModel):
    status: str
    message: Optional[str] = None
