"""Legacy project models (preserved for backward compatibility).

New code should use the spec-aligned models in project.py:
- NamespacedProject, LinkedResource, ContextConfig, ParsedName

These legacy models exist to avoid breaking existing services,
repositories, and CLI commands that depend on the old API.
"""

from __future__ import annotations

from datetime import datetime
from pathlib import Path

from pydantic import BaseModel, ConfigDict, Field, field_validator


class ProjectSettings(BaseModel):
    """Project-specific settings and configuration (legacy)."""

    auto_build: bool = Field(default=False, description="Automatically build plans")
    auto_apply: bool = Field(
        default=False,
        description="Automatically apply changes",
    )
    confirm_apply: bool = Field(
        default=True,
        description="Require confirmation before applying",
    )
    max_context_size: int = Field(
        default=52428800,
        description="Maximum context size in bytes (50MB)",
    )
    default_model: str = Field(default="mock-gpt", description="Default AI model")
    include_paths: list[str] = Field(
        default_factory=list, description="Relative include globs"
    )
    exclude_paths: list[str] = Field(
        default_factory=list, description="Relative exclude globs"
    )

    model_config = ConfigDict(
        str_strip_whitespace=True,
        validate_assignment=True,
    )


class ProjectStats(BaseModel):
    """Statistics for a project (legacy)."""

    plans: int = Field(default=0, ge=0)
    context_files: int = Field(default=0, ge=0)
    changes: int = Field(default=0, ge=0)
    current_plan: str | None = Field(default=None)

    model_config = ConfigDict(
        str_strip_whitespace=True,
        validate_assignment=True,
    )


class Project(BaseModel):
    """Legacy project model (preserved for backward compatibility).

    New code should use NamespacedProject instead. This model exists
    to avoid breaking existing services, repositories, and CLI commands.
    """

    id: int | None = Field(default=None, description="Project ID")
    name: str = Field(..., min_length=1, max_length=255)
    path: Path = Field(..., description="Project root path")
    created_at: datetime = Field(default_factory=datetime.now)
    updated_at: datetime = Field(default_factory=datetime.now)
    settings: ProjectSettings = Field(
        default_factory=lambda: ProjectSettings()  # type: ignore[arg-type]
    )
    current_plan_id: int | None = Field(default=None)

    @field_validator("name")
    @classmethod
    def validate_name(cls: type[Project], v: str) -> str:
        """Validate project name."""
        if not v.replace("-", "").replace("_", "").replace(" ", "").isalnum():
            raise ValueError(
                "Name must be alphanumeric with hyphens, underscores, or spaces"
            )
        return v

    @field_validator("path")
    @classmethod
    def validate_path(cls: type[Project], v: Path) -> Path:
        """Ensure path is absolute."""
        return v.resolve()

    model_config = ConfigDict(
        str_strip_whitespace=True,
        validate_assignment=True,
        arbitrary_types_allowed=True,
    )
