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


class ValuationAssessmentBase(BaseModel):
    """估值评估基础模型"""
    # 基础信息
    asset_name: str = Field(..., description="资产名称")
    institution: str = Field(..., description="所属机构")
    industry: str = Field(..., description="所属行业")
    
    # 财务状况
    annual_revenue: Optional[str] = Field(None, description="近12个月机构营收/万元")
    rd_investment: Optional[str] = Field(None, description="近12个月机构研发投入/万元")
    three_year_income: Optional[List[Any]] = Field(None, description="近三年机构收益/万元")
    funding_status: Optional[str] = Field(None, description="资产受资助情况")
    
    # 非遗等级与技术
    inheritor_level: Optional[str] = Field(None, description="非遗传承人等级")
    inheritor_age_count: Optional[List[Any]] = Field(None, description="非遗传承人年龄水平及数量")
    inheritor_certificates: Optional[List[Any]] = Field(None, description="非遗传承人等级证书")
    heritage_level: Optional[str] = Field(None, description="非遗等级")
    patent_application_no: Optional[str] = Field(None, description="非遗资产所用专利的申请号")
    historical_evidence: Optional[List[Any]] = Field(None, description="非遗资产历史证明证据及数量")
    patent_certificates: Optional[List[Any]] = Field(None, description="非遗资产所用专利的证书")
    pattern_images: Optional[List[Any]] = Field(None, description="非遗纹样图片")
    
    # 非遗应用与推广
    application_maturity: Optional[str] = Field(None, description="非遗资产应用成熟度")
    application_coverage: Optional[str] = Field(None, description="非遗资产应用覆盖范围")
    cooperation_depth: Optional[str] = Field(None, description="非遗资产跨界合作深度")
    offline_activities: Optional[str] = Field(None, description="近12个月线下相关宣讲活动次数")
    online_accounts: Optional[List[Any]] = Field(None, description="线上相关宣传账号信息")
    
    # 非遗资产衍生商品信息
    sales_volume: Optional[str] = Field(None, description="该商品近12个月销售量")
    link_views: Optional[str] = Field(None, description="该商品近12个月的链接浏览量")
    circulation: Optional[str] = Field(None, description="该商品的发行量")
    last_market_activity: Optional[str] = Field(None, description="该商品最近一次市场活动时间")
    monthly_transaction: Optional[str] = Field(None, description="月交易额")
    price_fluctuation: Optional[str] = Field(None, description="该商品近30天价格波动区间")


class ValuationAssessmentCreate(ValuationAssessmentBase):
    """创建估值评估模型"""
    pass


class ValuationAssessmentUpdate(BaseModel):
    """更新估值评估模型"""
    # 基础信息
    asset_name: Optional[str] = Field(None, description="资产名称")
    institution: Optional[str] = Field(None, description="所属机构")
    industry: Optional[str] = Field(None, description="所属行业")
    
    # 财务状况
    annual_revenue: Optional[str] = Field(None, description="近12个月机构营收/万元")
    rd_investment: Optional[str] = Field(None, description="近12个月机构研发投入/万元")
    three_year_income: Optional[List[Any]] = Field(None, description="近三年机构收益/万元")
    funding_status: Optional[str] = Field(None, description="资产受资助情况")
    
    # 非遗等级与技术
    inheritor_level: Optional[str] = Field(None, description="非遗传承人等级")
    inheritor_age_count: Optional[List[Any]] = Field(None, description="非遗传承人年龄水平及数量")
    inheritor_certificates: Optional[List[Any]] = Field(None, description="非遗传承人等级证书")
    heritage_level: Optional[str] = Field(None, description="非遗等级")
    patent_application_no: Optional[str] = Field(None, description="非遗资产所用专利的申请号")
    historical_evidence: Optional[List[Any]] = Field(None, description="非遗资产历史证明证据及数量")
    patent_certificates: Optional[List[Any]] = Field(None, description="非遗资产所用专利的证书")
    pattern_images: Optional[List[Any]] = Field(None, description="非遗纹样图片")
    
    # 非遗应用与推广
    application_maturity: Optional[str] = Field(None, description="非遗资产应用成熟度")
    application_coverage: Optional[str] = Field(None, description="非遗资产应用覆盖范围")
    cooperation_depth: Optional[str] = Field(None, description="非遗资产跨界合作深度")
    offline_activities: Optional[str] = Field(None, description="近12个月线下相关宣讲活动次数")
    online_accounts: Optional[List[Any]] = Field(None, description="线上相关宣传账号信息")
    
    # 非遗资产衍生商品信息
    sales_volume: Optional[str] = Field(None, description="该商品近12个月销售量")
    link_views: Optional[str] = Field(None, description="该商品近12个月的链接浏览量")
    circulation: Optional[str] = Field(None, description="该商品的发行量")
    last_market_activity: Optional[str] = Field(None, description="该商品最近一次市场活动时间")
    monthly_transaction: Optional[str] = Field(None, description="月交易额")
    price_fluctuation: Optional[str] = Field(None, description="该商品近30天价格波动区间")
    
    is_active: Optional[bool] = Field(None, description="是否激活")


class ValuationAssessmentOut(ValuationAssessmentBase):
    """输出估值评估模型"""
    id: int = Field(..., description="主键ID")
    user_id: int = Field(..., description="用户ID")
    status: str = Field(..., description="评估状态")
    admin_notes: Optional[str] = Field(None, description="管理员备注")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    is_active: bool = Field(..., description="是否激活")

    class Config:
        from_attributes = True


# 用户端专用模式
class UserValuationCreate(ValuationAssessmentBase):
    """用户端创建估值评估模型"""
    pass


class UserValuationOut(BaseModel):
    """用户端输出估值评估模型"""
    id: int = Field(..., description="主键ID")
    asset_name: str = Field(..., description="资产名称")
    institution: str = Field(..., description="所属机构")
    industry: str = Field(..., description="所属行业")
    status: str = Field(..., description="评估状态")
    admin_notes: Optional[str] = Field(None, description="管理员备注")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")

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


class UserValuationDetail(ValuationAssessmentBase):
    """用户端详细估值评估模型"""
    id: int = Field(..., description="主键ID")
    status: str = Field(..., description="评估状态")
    admin_notes: Optional[str] = Field(None, description="管理员备注")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")

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


class UserValuationList(BaseModel):
    """用户端估值评估列表模型"""
    items: List[UserValuationOut] = Field(..., description="估值评估列表")
    total: int = Field(..., description="总数量")
    page: int = Field(..., description="当前页码")
    size: int = Field(..., description="每页数量")
    pages: int = Field(..., description="总页数")


class UserValuationQuery(BaseModel):
    """用户端估值评估查询模型"""
    status: Optional[str] = Field(None, description="评估状态")
    asset_name: Optional[str] = Field(None, description="资产名称")
    page: int = Field(1, ge=1, description="页码")
    size: int = Field(10, ge=1, le=100, description="每页数量")


class ValuationAssessmentList(BaseModel):
    """估值评估列表模型"""
    items: List[ValuationAssessmentOut] = Field(..., description="估值评估列表")
    total: int = Field(..., description="总数量")
    page: int = Field(..., description="当前页码")
    size: int = Field(..., description="每页数量")
    pages: int = Field(..., description="总页数")


class ValuationAssessmentQuery(BaseModel):
    """估值评估查询模型"""
    asset_name: Optional[str] = Field(None, description="资产名称")
    institution: Optional[str] = Field(None, description="所属机构")
    industry: Optional[str] = Field(None, description="所属行业")
    heritage_level: Optional[str] = Field(None, description="非遗等级")
    status: Optional[str] = Field(None, description="评估状态: pending(待审核), approved(已通过), rejected(已拒绝)")
    is_active: Optional[bool] = Field(None, description="是否激活")
    page: int = Field(1, ge=1, description="页码")
    size: int = Field(10, ge=1, le=100, description="每页数量")


# 管理端审核相关模型
class ValuationApprovalRequest(BaseModel):
    """估值评估审核请求模型"""
    admin_notes: Optional[str] = Field(None, description="管理员备注")


class ValuationAdminNotesUpdate(BaseModel):
    """管理员备注更新模型"""
    admin_notes: str = Field(..., description="管理员备注")