from pydantic import BaseModel, Field

from fastapi_modules.fastapi_leaudit.domian.vo.ruleVo import RuleVersionVO


class RuleGroupBindingVO(BaseModel):
    """二级分组下的规则集绑定。"""

    id: int = Field(..., description="绑定ID")
    group_id: int = Field(..., description="分组ID")
    rule_set_id: int = Field(..., description="规则集ID")
    rule_type_binding_id: int | None = Field(None, description="镜像的运行时绑定ID")
    priority: int = Field(0, description="优先级")
    is_active: bool = Field(True, description="是否启用")
    note: str | None = Field(None, description="备注")
    rule_type: str | None = Field(None, description="规则类型编码")
    rule_name: str | None = Field(None, description="规则集名称")
    current_version_id: int | None = Field(None, description="当前版本ID")
    fallback_version_id: int | None = Field(None, description="回退版本ID")
    has_usable_version: bool = Field(False, description="是否存在可用版本")
    usable_rule_count: int = Field(0, description="可用规则数")


class EvaluationPointGroupVO(BaseModel):
    """评查点分组详情。"""

    id: int = Field(..., description="分组ID")
    pid: int | None = Field(None, description="父分组ID")
    name: str = Field(..., description="分组名称")
    code: str = Field(..., description="分组编码")
    description: str | None = Field(None, description="分组描述")
    document_type_id: int | None = Field(None, description="关联文档类型ID")
    document_type_name: str | None = Field(None, description="关联文档类型名称")
    entry_module_id: int | None = Field(None, description="入口模块ID")
    entry_module_name: str | None = Field(None, description="入口模块名称")
    sort_order: int = Field(0, description="排序")
    is_enabled: bool = Field(True, description="是否启用")
    created_at: str | None = Field(None, description="创建时间")
    updated_at: str | None = Field(None, description="更新时间")
    rule_count: int | None = Field(None, description="绑定的规则集数量")
    bindings: list[RuleGroupBindingVO] = Field(default_factory=list, description="二级分组绑定的规则集")
    children: list["EvaluationPointGroupVO"] | None = Field(None, description="子分组")


class EvaluationPointGroupListVO(BaseModel):
    """评查点分组列表分页。"""

    data: list[EvaluationPointGroupVO] = Field(default_factory=list, description="分组列表")
    total: int = Field(0, description="总数")
    page: int = Field(1, description="页码")
    page_size: int = Field(20, description="分页大小")


class DocTypeInfoVO(BaseModel):
    """换绑提示中的文档类型信息。"""

    id: int = Field(..., description="文档类型ID")
    name: str = Field(..., description="文档类型名称")


class EvaluationPointGroupDeleteVO(BaseModel):
    """删除分组响应。"""

    success: bool = Field(..., description="是否成功")
    message: str | None = Field(None, description="提示信息")
    deleted_count: int | None = Field(None, description="兼容字段")
    deleted_groups: int | None = Field(None, description="删除的分组数")
    deleted_points: int | None = Field(None, description="兼容旧字段：已删除绑定数")
    need_rebind: bool = Field(False, description="是否需要先换绑")
    points_count: int | None = Field(None, description="兼容旧字段")
    single_bound_doc_types: list[DocTypeInfoVO] = Field(default_factory=list, description="兼容旧字段")
    multi_bound_doc_types: list[DocTypeInfoVO] = Field(default_factory=list, description="兼容旧字段")


class EvaluationPointGroupRebindVO(BaseModel):
    """换绑结果。"""

    success: bool = Field(..., description="是否成功")
    message: str = Field(..., description="结果消息")
    rebind_count: int = Field(0, description="迁移的二级分组数量")
    doc_types_updated: int = Field(0, description="兼容字段")


class EvaluationPointGroupBatchStatusVO(BaseModel):
    """批量更新状态结果。"""

    success: bool = Field(..., description="是否成功")
    updated_count: int = Field(0, description="更新数量")
    message: str = Field(..., description="结果消息")


class EvaluationPointGroupBatchDeleteVO(BaseModel):
    """批量删除结果。"""

    success: bool = Field(..., description="是否成功")
    deleted_groups: int = Field(0, description="删除的分组数量")
    deleted_points: int = Field(0, description="兼容旧字段：删除的绑定数量")
    message: str = Field(..., description="结果消息")


class EvaluationPointGroupRuleTemplateContextVO(BaseModel):
    """二级分组生成规则模板所需上下文。"""

    group_id: int = Field(..., description="当前二级分组ID")
    group_code: str = Field(..., description="当前二级分组编码")
    group_name: str = Field(..., description="当前二级分组名称")
    parent_group_id: int = Field(..., description="所属一级分组ID")
    parent_group_code: str = Field(..., description="所属一级分组编码")
    parent_group_name: str = Field(..., description="所属一级分组名称")
    document_type_id: int | None = Field(None, description="关联文档类型ID")
    document_type_code: str | None = Field(None, description="关联文档类型编码")
    document_type_name: str | None = Field(None, description="关联文档类型名称")
    entry_module_id: int | None = Field(None, description="入口模块ID")
    entry_module_name: str | None = Field(None, description="入口模块名称")


class EvaluationPointGroupRuleTemplateVO(BaseModel):
    """二级分组规则模板响应。"""

    context: EvaluationPointGroupRuleTemplateContextVO = Field(..., description="二级分组上下文")
    ruleType: str = Field(..., description="从分组上下文推导出的规则类型编码")
    ruleName: str = Field(..., description="规则集名称")
    nextVersionNo: str = Field(..., description="建议创建的下一个版本号")
    ossPreviewKey: str = Field(..., description="该版本预计写入的 OSS key")
    yamlTemplate: str = Field(..., description="可直接编辑的完整 YAML 模板")
    existingRuleSetId: int | None = Field(None, description="已存在的规则集ID")
    existingBindingId: int | None = Field(None, description="当前分组已存在的绑定ID")


class EvaluationPointGroupRuleDraftVO(BaseModel):
    """二级分组规则草稿创建响应。"""

    packId: int = Field(..., description="规则配置 pack ID，当前等于二级分组ID")
    groupId: int = Field(..., description="二级分组ID")
    ruleName: str = Field(..., description="规则名称")
    ruleType: str = Field(..., description="规则类型编码")
    ruleSetId: int = Field(..., description="所属规则集ID")
    ossKey: str = Field(..., description="草稿写入的 OSS key")
    version: RuleVersionVO = Field(..., description="刚创建的规则版本")
    binding: RuleGroupBindingVO = Field(..., description="当前二级分组与规则集的绑定信息")
    autoBound: bool = Field(..., description="本次是否自动新增了绑定")


EvaluationPointGroupVO.model_rebuild()
