from __future__ import annotations

from datetime import date, timedelta
from typing import Any


class WbsPhaseEarlyMixin:
    """Phase 1 & 2 WBS item generators with exact schema."""

    @staticmethod
    def _build_phase_1_items(
        base_start: date,
        p1_start: date,
        p1_end: date,
        p1_days: int,
        p1_amount: float,
        raw_items: list[dict[str, Any]],
    ) -> list[dict[str, Any]]:
        wbs_items: list[dict[str, Any]] = []
        wbs_items.append(
            {
                "wbs_code": "1.0",
                "task_name": "Giai đoạn 1: Chuẩn bị mặt bằng & Hồ sơ pháp lý khởi công",
                "task_type": "phase",
                "start_date": p1_start.isoformat(),
                "end_date": p1_end.isoformat(),
                "duration_days": p1_days,
                "progress_percent": 0,
                "status": "todo",
                "assigned_role": "Ban QLDA & Pháp lý",
                "sort_order": 1,
                "total_amount_vnd": p1_amount,
            }
        )
        wbs_items.extend(
            [
                {
                    "parent_wbs_code": "1.0",
                    "wbs_code": "1.1",
                    "task_name": "Khảo sát trắc địa, định vị tim trục & Bàn giao mốc cao độ chuẩn",
                    "task_type": "task",
                    "unit": "Điểm mốc",
                    "quantity": 4,
                    "unit_price_vnd": round(p1_amount * 0.3 / 4, 2),
                    "total_amount_vnd": round(p1_amount * 0.3, 2),
                    "start_date": p1_start.isoformat(),
                    "end_date": (
                        p1_start + timedelta(days=max(p1_days // 3, 3))
                    ).isoformat(),
                    "duration_days": max(p1_days // 3, 3),
                    "status": "todo",
                    "assigned_role": "Kỹ sư Trắc địa",
                    "sort_order": 2,
                },
                {
                    "parent_wbs_code": "1.0",
                    "wbs_code": "1.2",
                    "task_name": "Lập & Phê duyệt Biện pháp thi công, Kế hoạch ATLĐ & PCCC",
                    "task_type": "task",
                    "unit": "Bộ hồ sơ",
                    "quantity": 1,
                    "unit_price_vnd": round(p1_amount * 0.3, 2),
                    "total_amount_vnd": round(p1_amount * 0.3, 2),
                    "start_date": (
                        p1_start + timedelta(days=max(p1_days // 4, 2))
                    ).isoformat(),
                    "end_date": (
                        p1_start + timedelta(days=max(p1_days * 2 // 3, 5))
                    ).isoformat(),
                    "duration_days": max(p1_days // 3, 3),
                    "status": "todo",
                    "assigned_role": "Chỉ huy trưởng",
                    "sort_order": 3,
                },
                {
                    "parent_wbs_code": "1.0",
                    "wbs_code": "1.3",
                    "task_name": "Huy động máy móc thiết bị đào, lắp dựng lán trại & hàng rào tạm",
                    "task_type": "task",
                    "unit": "Gói",
                    "quantity": 1,
                    "unit_price_vnd": round(p1_amount * 0.4, 2),
                    "total_amount_vnd": round(p1_amount * 0.4, 2),
                    "start_date": (
                        p1_start + timedelta(days=max(p1_days // 2, 4))
                    ).isoformat(),
                    "end_date": p1_end.isoformat(),
                    "duration_days": max(p1_days // 2, 4),
                    "status": "todo",
                    "assigned_role": "Đội thi công cơ giới",
                    "sort_order": 4,
                },
            ]
        )

        # ----------------------------------------------------------------------
        return wbs_items

    @staticmethod
    def _build_phase_2_items(
        p2_start: date,
        p2_end: date,
        p2_days: int,
        p2_amount: float,
    ) -> list[dict[str, Any]]:
        wbs_items: list[dict[str, Any]] = []
        wbs_items.append(
            {
                "wbs_code": "2.0",
                "task_name": "Giai đoạn 2: Thi công Nền móng & Hạ tầng kỹ thuật ngầm",
                "task_type": "phase",
                "start_date": p2_start.isoformat(),
                "end_date": p2_end.isoformat(),
                "duration_days": p2_days,
                "progress_percent": 0,
                "status": "todo",
                "assigned_role": "Đội thi công Xây lắp & Cơ giới",
                "sort_order": 5,
                "total_amount_vnd": p2_amount,
            }
        )

        # Inject parsed foundation items or defaults
        wbs_items.extend(
            [
                {
                    "parent_wbs_code": "2.0",
                    "wbs_code": "2.1",
                    "task_name": "Đào đất hố móng, lu lèn nền đất & Vận chuyển đất thải",
                    "task_type": "task",
                    "unit": "m3",
                    "quantity": 650,
                    "unit_price_vnd": round(p2_amount * 0.25 / 650, 2),
                    "total_amount_vnd": round(p2_amount * 0.25, 2),
                    "start_date": p2_start.isoformat(),
                    "end_date": (
                        p2_start + timedelta(days=max(p2_days // 3, 6))
                    ).isoformat(),
                    "duration_days": max(p2_days // 3, 6),
                    "status": "todo",
                    "assigned_role": "Tổ máy đào PC200",
                    "sort_order": 6,
                },
                {
                    "parent_wbs_code": "2.0",
                    "wbs_code": "2.2",
                    "task_name": "Đổ bê tông lót móng M100 đá 4x6 chiều dày 10cm",
                    "task_type": "task",
                    "unit": "m3",
                    "quantity": 85,
                    "unit_price_vnd": round(p2_amount * 0.15 / 85, 2),
                    "total_amount_vnd": round(p2_amount * 0.15, 2),
                    "start_date": (
                        p2_start + timedelta(days=max(p2_days // 4, 5))
                    ).isoformat(),
                    "end_date": (
                        p2_start + timedelta(days=max(p2_days // 2, 10))
                    ).isoformat(),
                    "duration_days": max(p2_days // 4, 5),
                    "status": "todo",
                    "assigned_role": "Tổ Bê tông",
                    "sort_order": 7,
                },
                {
                    "parent_wbs_code": "2.0",
                    "wbs_code": "2.3",
                    "task_name": "Gia công lắp dựng cốt thép & ván khuôn đài móng, dầm giằng móng",
                    "task_type": "task",
                    "unit": "Tấn",
                    "quantity": 25,
                    "unit_price_vnd": round(p2_amount * 0.35 / 25, 2),
                    "total_amount_vnd": round(p2_amount * 0.35, 2),
                    "start_date": (
                        p2_start + timedelta(days=max(p2_days // 3, 8))
                    ).isoformat(),
                    "end_date": (
                        p2_start + timedelta(days=max(p2_days * 3 // 4, 18))
                    ).isoformat(),
                    "duration_days": max(p2_days // 2, 10),
                    "status": "todo",
                    "assigned_role": "Tổ Cốt thép - Ván khuôn",
                    "sort_order": 8,
                },
                {
                    "parent_wbs_code": "2.0",
                    "wbs_code": "2.4",
                    "task_name": "Đổ bê tông đài giằng móng M300 & Nghiệm thu KCS phần móng",
                    "task_type": "task",
                    "unit": "m3",
                    "quantity": 240,
                    "unit_price_vnd": round(p2_amount * 0.25 / 240, 2),
                    "total_amount_vnd": round(p2_amount * 0.25, 2),
                    "start_date": (
                        p2_start + timedelta(days=max(p2_days * 2 // 3, 15))
                    ).isoformat(),
                    "end_date": p2_end.isoformat(),
                    "duration_days": max(p2_days // 3, 6),
                    "status": "todo",
                    "assigned_role": "Tổ Bê tông & QA/QC",
                    "sort_order": 9,
                },
            ]
        )

        # ----------------------------------------------------------------------
        return wbs_items
