from __future__ import annotations

from datetime import date, timedelta
from typing import Any


class WbsPhaseLateMixin:
    """Phase 3 & 4 WBS item generators with exact schema."""

    @staticmethod
    def _build_phase_3_items(
        p3_start: date,
        p3_end: date,
        p3_days: int,
        p3_amount: float,
    ) -> list[dict[str, Any]]:
        wbs_items: list[dict[str, Any]] = []
        wbs_items.append(
            {
                "wbs_code": "3.0",
                "task_name": "Giai đoạn 3: Thi công Kết cấu thân & Hoàn thiện Kiến trúc / M&E",
                "task_type": "phase",
                "start_date": p3_start.isoformat(),
                "end_date": p3_end.isoformat(),
                "duration_days": p3_days,
                "progress_percent": 0,
                "status": "todo",
                "assigned_role": "Đội thi công Xây lắp hoàn thiện",
                "sort_order": 10,
                "total_amount_vnd": p3_amount,
            }
        )
        wbs_items.extend(
            [
                {
                    "parent_wbs_code": "3.0",
                    "wbs_code": "3.1",
                    "task_name": "Thi công kết cấu bê tông cốt thép cột, dầm, sàn các tầng",
                    "task_type": "task",
                    "unit": "Sàn",
                    "quantity": 3,
                    "unit_price_vnd": round(p3_amount * 0.45 / 3, 2),
                    "total_amount_vnd": round(p3_amount * 0.45, 2),
                    "start_date": p3_start.isoformat(),
                    "end_date": (
                        p3_start + timedelta(days=max(p3_days // 2, 18))
                    ).isoformat(),
                    "duration_days": max(p3_days // 2, 18),
                    "status": "todo",
                    "assigned_role": "Tổ Xây thô & Bê tông",
                    "sort_order": 11,
                },
                {
                    "parent_wbs_code": "3.0",
                    "wbs_code": "3.2",
                    "task_name": "Xây tường bao, vách ngăn & Trát tường trong/ngoài nhà",
                    "task_type": "task",
                    "unit": "m2",
                    "quantity": 1800,
                    "unit_price_vnd": round(p3_amount * 0.25 / 1800, 2),
                    "total_amount_vnd": round(p3_amount * 0.25, 2),
                    "start_date": (
                        p3_start + timedelta(days=max(p3_days // 3, 12))
                    ).isoformat(),
                    "end_date": (
                        p3_start + timedelta(days=max(p3_days * 4 // 5, 30))
                    ).isoformat(),
                    "duration_days": max(p3_days // 2, 20),
                    "status": "todo",
                    "assigned_role": "Tổ Nề hoàn thiện",
                    "sort_order": 12,
                },
                {
                    "parent_wbs_code": "3.0",
                    "wbs_code": "3.3",
                    "task_name": "Lắp đặt hệ thống cấp thoát nước, điện chiếu sáng âm tường (M&E)",
                    "task_type": "task",
                    "unit": "Hệ thống",
                    "quantity": 1,
                    "unit_price_vnd": round(p3_amount * 0.20, 2),
                    "total_amount_vnd": round(p3_amount * 0.20, 2),
                    "start_date": (
                        p3_start + timedelta(days=max(p3_days // 2, 18))
                    ).isoformat(),
                    "end_date": (
                        p3_start + timedelta(days=max(p3_days * 9 // 10, 35))
                    ).isoformat(),
                    "duration_days": max(p3_days * 2 // 5, 18),
                    "status": "todo",
                    "assigned_role": "Tổ Cơ điện M&E",
                    "sort_order": 13,
                },
                {
                    "parent_wbs_code": "3.0",
                    "wbs_code": "3.4",
                    "task_name": "Sơn bả tường, ốp lát gạch sàn & Lắp đặt cửa đi/cửa sổ",
                    "task_type": "task",
                    "unit": "Gói",
                    "quantity": 1,
                    "unit_price_vnd": round(p3_amount * 0.10, 2),
                    "total_amount_vnd": round(p3_amount * 0.10, 2),
                    "start_date": (
                        p3_start + timedelta(days=max(p3_days * 3 // 5, 24))
                    ).isoformat(),
                    "end_date": p3_end.isoformat(),
                    "duration_days": max(p3_days * 2 // 5, 15),
                    "status": "todo",
                    "assigned_role": "Tổ Sơn & Hoàn thiện",
                    "sort_order": 14,
                },
            ]
        )

        # ----------------------------------------------------------------------
        return wbs_items

    @staticmethod
    def _build_phase_4_items(
        p4_start: date,
        p4_end: date,
        p4_days: int,
        p4_amount: float,
    ) -> list[dict[str, Any]]:
        wbs_items: list[dict[str, Any]] = []
        wbs_items.append(
            {
                "wbs_code": "4.0",
                "task_name": "Giai đoạn 4: Nghiệm thu KCS, Bàn giao công trình & Quyết toán FIDIC",
                "task_type": "phase",
                "start_date": p4_start.isoformat(),
                "end_date": p4_end.isoformat(),
                "duration_days": p4_days,
                "progress_percent": 0,
                "status": "todo",
                "assigned_role": "Ban QLDA & Kế toán trưởng",
                "sort_order": 15,
                "total_amount_vnd": p4_amount,
            }
        )
        wbs_items.extend(
            [
                {
                    "parent_wbs_code": "4.0",
                    "wbs_code": "4.1",
                    "task_name": "Lập hồ sơ hoàn công, kiểm định an toàn & Bản vẽ As-Built",
                    "task_type": "task",
                    "unit": "Bộ hồ sơ",
                    "quantity": 1,
                    "unit_price_vnd": round(p4_amount * 0.4, 2),
                    "total_amount_vnd": round(p4_amount * 0.4, 2),
                    "start_date": p4_start.isoformat(),
                    "end_date": (
                        p4_start + timedelta(days=max(p4_days // 2, 6))
                    ).isoformat(),
                    "duration_days": max(p4_days // 2, 6),
                    "status": "todo",
                    "assigned_role": "Kỹ sư QA/QC",
                    "sort_order": 16,
                },
                {
                    "parent_wbs_code": "4.0",
                    "wbs_code": "4.2",
                    "task_name": "Tổ chức nghiệm thu hoàn thành với Chủ đầu tư & Bàn giao đưa vào sử dụng",
                    "task_type": "task",
                    "unit": "Biên bản",
                    "quantity": 1,
                    "unit_price_vnd": round(p4_amount * 0.3, 2),
                    "total_amount_vnd": round(p4_amount * 0.3, 2),
                    "start_date": (
                        p4_start + timedelta(days=max(p4_days // 2, 6))
                    ).isoformat(),
                    "end_date": (
                        p4_start + timedelta(days=max(p4_days * 4 // 5, 10))
                    ).isoformat(),
                    "duration_days": max(p4_days // 3, 4),
                    "status": "todo",
                    "assigned_role": "Chỉ huy trưởng & Giám đốc DA",
                    "sort_order": 17,
                },
                {
                    "parent_wbs_code": "4.0",
                    "wbs_code": "4.3",
                    "task_name": "Đối soát chứng từ IPC, xuất hóa đơn VAT & Thu hồi tiền giữ lại (Retention)",
                    "task_type": "task",
                    "unit": "Đợt",
                    "quantity": 1,
                    "unit_price_vnd": round(p4_amount * 0.3, 2),
                    "total_amount_vnd": round(p4_amount * 0.3, 2),
                    "start_date": (
                        p4_start + timedelta(days=max(p4_days * 3 // 4, 9))
                    ).isoformat(),
                    "end_date": p4_end.isoformat(),
                    "duration_days": max(p4_days // 4, 4),
                    "status": "todo",
                    "assigned_role": "Kế toán trưởng (Nam / Quỳnh)",
                    "sort_order": 18,
                },
            ]
        )

        # 3. Generate Milestones
        return wbs_items
