from __future__ import annotations

from datetime import datetime
from typing import Any

from pydantic import BaseModel, ConfigDict, Field

from app.core.agent_enums import (
    AgentAssetContentType,
    AgentAssetDomain,
    AgentAssetStatus,
    AgentAssetType,
    AgentReviewStatus,
)


class AgentAssetCreate(BaseModel):
    asset_type: AgentAssetType
    code: str = Field(min_length=1, max_length=100)
    name: str = Field(min_length=1, max_length=200)
    description: str = ""
    domain: AgentAssetDomain
    scenario_json: list[Any] = Field(default_factory=list)
    owner: str = Field(min_length=1, max_length=100)
    reviewer: str | None = Field(default=None, max_length=100)
    status: AgentAssetStatus = AgentAssetStatus.DRAFT
    config_json: dict[str, Any] = Field(default_factory=dict)


class AgentAssetUpdate(BaseModel):
    name: str | None = Field(default=None, min_length=1, max_length=200)
    description: str | None = None
    domain: AgentAssetDomain | None = None
    scenario_json: list[Any] | None = None
    owner: str | None = Field(default=None, min_length=1, max_length=100)
    reviewer: str | None = Field(default=None, max_length=100)
    status: AgentAssetStatus | None = None
    current_version: str | None = Field(default=None, max_length=30)
    config_json: dict[str, Any] | None = None


class AgentAssetVersionCreate(BaseModel):
    version: str = Field(min_length=1, max_length=30)
    content: Any
    content_type: AgentAssetContentType
    change_note: str | None = None
    created_by: str = Field(min_length=1, max_length=100)


class RuleMarkdownUpdate(BaseModel):
    version: str = Field(min_length=1, max_length=30)
    content: str
    change_note: str | None = None
    created_by: str = Field(min_length=1, max_length=100)


class AgentAssetReviewCreate(BaseModel):
    version: str = Field(min_length=1, max_length=30)
    reviewer: str = Field(min_length=1, max_length=100)
    review_status: AgentReviewStatus
    review_note: str | None = None


class AgentAssetReviewRead(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: str
    asset_id: str
    version: str
    reviewer: str
    review_status: str
    review_note: str | None
    reviewed_at: datetime | None
    created_at: datetime


class AgentAssetVersionRead(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: str
    asset_id: str
    version: str
    content: Any
    content_type: str
    change_note: str | None
    created_by: str
    created_at: datetime
    is_current: bool = False


class AgentAssetListItem(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: str
    asset_type: str
    code: str
    name: str
    description: str
    domain: str
    scenario_json: list[Any]
    owner: str
    reviewer: str | None
    status: str
    current_version: str | None
    config_json: dict[str, Any]
    created_at: datetime
    updated_at: datetime


class AgentAssetRead(AgentAssetListItem):
    current_version_content: Any | None = None
    current_version_content_type: str | None = None
    current_version_change_note: str | None = None
    recent_versions: list[AgentAssetVersionRead] = Field(default_factory=list)
    latest_review: AgentAssetReviewRead | None = None
