"""
一个接口按 domain 返回项目聚合数据。
"""

from typing import Literal

from pydantic import BaseModel, Field

from ..construction.construction import ConstructionResponse
from ..construction.construction_detail import ConstructionDetailResponse
from ..contract.contract import ContractResponse
from ..contract_settlement.contract_settlement import (
	ContractSettlementResponse,
)
from ..contract_settlement.contract_settlement_detail import (
	ContractSettlementDetailResponse,
)
from ..material.material import MaterialResponse
from ..material.material_detail import MaterialDetailResponse
from ..power.power import PowerResponse
from ..power.power_detail import PowerDetail
from ..project.project_base import ProjectResponseBase
from ..project_extensions.contract_change import ContractChangeResponse
from ..project_extensions.generator_unit import GeneratorUnitProject
from ..project_extensions.lar import (
	LarChangeDetailSchema,
	LarDetailSchema,
)
from ..project_extensions.preparation import PreparationDetail
from ..project_extensions.production import ProductionDetail
from ..project_extensions.project_detail import ProjectDetailProject
from ..project_multi_type import (
	GasTurbineExtension,
	HydropowerExtension,
	OffShoreWindExtension,
	OnShoreWindExtension,
	PhotovoltaicExtension,
	PumpedStorageExtension,
	ThermalPowerExtension,
)


ProjectAggregateDomain = Literal[
	"project",
	"project_detail",
	"generator_units",
	"lar",
	"lar_change",
	"contract_changes",
	"photovoltaic_extension",
	"onshore_wind_extension",
	"offshore_wind_extension",
	"hydropower_extension",
	"pumped_storage_extension",
	"gas_turbine_extension",
	"thermal_power_extension",
	"production_list",
	"preparation_list",
	"contracts",
	"materials",
	"material_details",
	"constructions",
	"construction_details",
	"contract_settlements",
	"contract_settlement_details",
	"power",
	"power_details",
]


ALL_PROJECT_AGGREGATE_DOMAINS: list[ProjectAggregateDomain] = [
	"project",
	"project_detail",
	"generator_units",
	"lar",
	"lar_change",
	"contract_changes",
	"photovoltaic_extension",
	"onshore_wind_extension",
	"offshore_wind_extension",
	"hydropower_extension",
	"pumped_storage_extension",
	"gas_turbine_extension",
	"thermal_power_extension",
	"production_list",
	"preparation_list",
	"contracts",
	"materials",
	"material_details",
	"constructions",
	"construction_details",
	"contract_settlements",
	"contract_settlement_details",
	"power",
	"power_details",
]


class ProjectAggregateData(BaseModel):
	"""按 domain 组织的项目聚合数据。"""

	project: ProjectResponseBase | None = Field(None, description="项目基本信息")
	project_detail: list[ProjectDetailProject] = Field(default_factory=list, description="项目容量信息")
	generator_units: list[GeneratorUnitProject] = Field(default_factory=list, description="发电单元信息")
	lar: LarDetailSchema | None = Field(None, description="移民征地信息")
	lar_change: list[LarChangeDetailSchema] = Field(default_factory=list, description="移民变更信息")
	contract_changes: list[ContractChangeResponse] = Field(default_factory=list, description="合同变更信息")

	photovoltaic_extension: PhotovoltaicExtension | None = Field(None, description="光伏项目扩展信息")
	onshore_wind_extension: OnShoreWindExtension | None = Field(None, description="陆风项目扩展信息")
	offshore_wind_extension: OffShoreWindExtension | None = Field(None, description="海风项目扩展信息")
	hydropower_extension: HydropowerExtension | None = Field(None, description="水电项目扩展信息")
	pumped_storage_extension: PumpedStorageExtension | None = Field(None, description="抽蓄项目扩展信息")
	gas_turbine_extension: GasTurbineExtension | None = Field(None, description="燃气轮机项目扩展信息")
	thermal_power_extension: ThermalPowerExtension | None = Field(None, description="火电项目扩展信息")

	production_list: list[ProductionDetail] = Field(default_factory=list, description="投产节点列表")
	preparation_list: list[PreparationDetail] = Field(default_factory=list, description="里程碑节点列表")

	contracts: list[ContractResponse] = Field(default_factory=list, description="合同列表")
	materials: list[MaterialResponse] = Field(default_factory=list, description="物资主表列表")
	material_details: list[MaterialDetailResponse] = Field(default_factory=list, description="物资明细列表")
	constructions: list[ConstructionResponse] = Field(default_factory=list, description="施工主表列表")
	construction_details: list[ConstructionDetailResponse] = Field(default_factory=list, description="施工明细列表")
	contract_settlements: list[ContractSettlementResponse] = Field(default_factory=list, description="合同结算主表列表")
	contract_settlement_details: list[ContractSettlementDetailResponse] = Field(default_factory=list, description="合同结算明细列表")
	power: list[PowerResponse] = Field(default_factory=list, description="力能主表列表")
	power_details: list[PowerDetail] = Field(default_factory=list, description="力能明细列表")


class ProjectAggregateMeta(BaseModel):
	"""聚合请求回显和截断信息。"""

	project_id: str = Field(..., description="项目ID")
	requested_domains: list[ProjectAggregateDomain] = Field(
		default_factory=lambda: list(ALL_PROJECT_AGGREGATE_DOMAINS),
		description="本次请求的 domain 列表",
	)
	domain_limit: int = Field(10000, description="每个 domain 的最大返回条数")
	truncated_domains: list[ProjectAggregateDomain] = Field(default_factory=list, description="被截断的 domain 列表")


class ProjectAggregateResponse(BaseModel):
	"""项目聚合同步响应。"""

	meta: ProjectAggregateMeta = Field(..., description="请求元信息")
	data: ProjectAggregateData = Field(
		default_factory=lambda: ProjectAggregateData.model_construct(),
		description="按 domain 分组的数据",
	)