"""
Project Models
"""

from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field


class TargetType(str, Enum):
    """Target application types."""
    GUI = "gui"
    TUI = "tui"
    CLI = "cli"


class ExecutionMode(str, Enum):
    """Test execution modes."""
    SCREEN = "screen"
    HEADLESS = "headless"
    MIXED = "mixed"


class TargetConnect(BaseModel):
    """Connection info for target application (hot start)."""
    pid: Optional[int] = None
    window_title: Optional[str] = None
    port: Optional[int] = None


class Target(BaseModel):
    """Target application configuration."""
    type: TargetType
    connect: Optional[TargetConnect] = None


class TestGroup(BaseModel):
    """Test group for organizing cases."""
    id: str = Field(..., description="Unique group identifier")
    name: str = Field(..., description="Group name")
    delay: int = Field(default=0, description="Delay in milliseconds before starting group")
    parallel: bool = Field(default=False, description="Whether cases in this group can run in parallel")
    cases: List[str] = Field(default_factory=list, description="List of case IDs in this group")


class TestProject(BaseModel):
    """Test project model."""
    id: str = Field(..., description="Unique project identifier")
    name: str = Field(..., description="Project name")
    target: Target = Field(..., description="Target application configuration")
    groups: List[TestGroup] = Field(default_factory=list, description="Test groups")
    cases: Dict[str, Any] = Field(default_factory=dict, description="Test cases dictionary (id -> case)")

    # Context information (provided by Agent)
    prd_content: Optional[str] = Field(default=None, description="PRD content (optional, for reference)")
    source_files: List[str] = Field(default_factory=list, description="Associated source code files")
    feature_hint: Optional[str] = Field(default=None, description="Feature description for matching")

    created_at: datetime = Field(default_factory=datetime.now)
    updated_at: datetime = Field(default_factory=datetime.now)

    class Config:
        json_encoders = {
            datetime: lambda v: v.isoformat()
        }


class ProjectCreate(BaseModel):
    """Request model for creating a project."""
    name: str
    target_type: TargetType
    prd_content: Optional[str] = None
    source_files: List[str] = Field(default_factory=list)
    feature_hint: Optional[str] = None


class ProjectResponse(BaseModel):
    """Response model for a project."""
    id: str
    name: str
    target_type: TargetType
    created_at: str
    updated_at: str
    case_count: int = 0
    group_count: int = 0
