"""Project Detail SyncNode"""

from typing import Any, Dict, Optional

from ...common.sync_node import SyncNode
from schemas.project_extensions.project_detail import ProjectDetailProject


_PRODUCTION_PROJECT_FIELDS = [
    "plan_production_date",
    "plan_full_production_date",
    "ic_plan_first_production_date",
    "ic_plan_full_production_date",
    "actual_production_date",
    "actual_full_production_date",
]


class ProjectDetailSyncNode(SyncNode[ProjectDetailProject]):
    """项目容量信息同步节点"""
    node_type = "project_detail_data"
    schema = ProjectDetailProject

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @staticmethod
    def _merge_production_extra_fields(target: Optional[Dict[str, Any]], source: Optional[Dict[str, Any]]) -> None:
        if not target or not source or source.get("key") != "production":
            return
        for field in _PRODUCTION_PROJECT_FIELDS:
            if field in source:
                target[field] = source.get(field)

    def _validate_and_set_data(self, data: Dict[str, Any]) -> None:
        super()._validate_and_set_data(data)
        self._merge_production_extra_fields(self.data, data)

    def set_origin_data(self, data: Optional[Dict[str, Any]]) -> None:
        super().set_origin_data(data)
        self._merge_production_extra_fields(self.origin_data, data)
