from datetime import datetime
from uuid import UUID, uuid4
from typing import Optional
from pydantic import BaseModel, Field, model_validator

class Project(BaseModel):
    """
    Core Domain Entity cho Phân hệ Dự Án.
    Hoàn toàn độc lập (Persistence Ignorance), không kế thừa bất kỳ class ORM nào.
    """
    id: UUID = Field(default_factory=uuid4)
    name: str = Field(..., min_length=5, max_length=200)
    project_code: str = Field(..., min_length=3, max_length=50)
    start_date: datetime
    description: Optional[str] = None
    status: str = "DRAFT"
    created_at: datetime = Field(default_factory=datetime.utcnow)

    @model_validator(mode='after')
    def validate_business_rules(self) -> 'Project':
        # Rule 1: Mã dự án phải luôn in hoa
        if not self.project_code.isupper():
            self.project_code = self.project_code.upper()
            
        # Các rules phức tạp về tài chính hoặc trạng thái có thể đưa vào đây
        # Ví dụ: Nếu trạng thái là IN_PROGRESS thì bắt buộc phải có start_date
        if self.status == "IN_PROGRESS" and self.start_date > datetime.utcnow():
            raise ValueError("Dự án đang thi công không thể có ngày bắt đầu trong tương lai.")
            
        return self
