from pydantic import BaseModel, Field
from typing import Optional, List, Dict
from datetime import datetime

from models.asset import AssetCategory, AssetStatus
from models.task import TaskType, TaskStatus


class AssetBase(BaseModel):
    name: str = Field(..., min_length=1, max_length=255)
    description: Optional[str] = None
    category: AssetCategory
    status: AssetStatus = AssetStatus.NOT_STARTED


class AssetCreate(AssetBase):
    create_default_tasks: bool = Field(default=True, description="Whether to create default tasks for this asset")
    selected_task_types: Optional[List[str]] = Field(default=None, description="Specific task types to create (if None, uses category defaults)")


class AssetUpdate(BaseModel):
    name: Optional[str] = Field(None, min_length=1, max_length=255)
    description: Optional[str] = None
    category: Optional[AssetCategory] = None
    status: Optional[AssetStatus] = None


class AssetResponse(AssetBase):
    id: int
    project_id: int
    created_at: datetime
    updated_at: datetime
    
    # Summary information
    task_count: int = 0

    class Config:
        from_attributes = True


class TaskStatusInfo(BaseModel):
    task_type: str  # Changed from TaskType enum to str to support custom task types
    status: str  # Changed from TaskStatus enum to str to support custom statuses
    task_id: Optional[int] = None
    assigned_user_id: Optional[int] = None


class AssetListResponse(BaseModel):
    id: int
    name: str
    description: Optional[str] = None
    category: AssetCategory
    status: AssetStatus
    project_id: int
    created_at: datetime
    updated_at: datetime
    
    # Summary information
    task_count: int = 0
    
    # Task status information for table display
    task_status: Dict[str, Optional[str]] = Field(default_factory=dict, description="Task status by task type")
    task_details: List[TaskStatusInfo] = Field(default_factory=list, description="Detailed task information")

    class Config:
        from_attributes = True