from __future__ import annotations

from pathlib import Path
from typing import Any

from pydantic import BaseModel, Field

STORAGE_VAULT = Path("storage/secure_vault")
STORAGE_VAULT.mkdir(parents=True, exist_ok=True)


class TakeoffItemUpdateRequest(BaseModel):
    item_name: str | None = None
    wbs_code: str | None = None
    norm_code: str | None = None
    category: str | None = None
    dimension_formula: str | None = None
    unit: str | None = None
    quantity: float | None = None
    unit_price_vnd: float | None = None
    total_amount_vnd: float | None = None


class SyncWbsRequest(BaseModel):
    project_id: str = Field(..., description="ID của Dự Án cần đồng bộ WBS")


class ChatTrainRequest(BaseModel):
    message: str | None = Field(
        None,
        description="Nội dung ý kiến chỉ đạo, phản hồi sai sót hoặc quy tắc huấn luyện",
    )
    feedback_message: str | None = Field(
        None, description="Tên tương đương của message"
    )
    auto_apply: bool = Field(
        False,
        description="Tự động áp dụng danh mục hiệu chỉnh vào bảng BoQ chính ngay lập tức",
    )
    auto_apply_corrections: bool = Field(
        False, description="Tên tương đương của auto_apply"
    )
    sender_role: str = Field(
        "Kỹ Sư Trưởng QS", description="Vai trò của người gửi phản hồi"
    )

    def get_message(self) -> str:
        return (self.message or self.feedback_message or "").strip()

    def should_auto_apply(self) -> bool:
        return bool(self.auto_apply or self.auto_apply_corrections)


class ApplyChatCorrectionsRequest(BaseModel):
    message_id: str | None = Field(None, description="ID tin nhắn đề xuất hiệu chỉnh")
    suggested_items: list[dict[str, Any]] | None = Field(
        None, description="Danh sách các dòng BoQ được áp dụng"
    )
    suggested_boq_items: list[dict[str, Any]] | None = Field(
        None, description="Tên tương đương của suggested_items"
    )

    def get_items(self) -> list[dict[str, Any]]:
        return self.suggested_items or self.suggested_boq_items or []


class LearnedRuleCreateRequest(BaseModel):
    drawing_type: str = Field("general", description="Loại công trình áp dụng")
    category: str = Field("all", description="Nhóm công tác")
    rule_category: str | None = Field(None, description="Tên tương đương category")
    error_pattern: str | None = Field(None, description="Dạng sai sót phát hiện")
    trigger_pattern: str | None = Field(
        None, description="Tên tương đương error_pattern"
    )
    rule_title: str | None = Field(None, description="Tiêu đề quy tắc")
    correction_rule: str | None = Field(
        None, description="Quy tắc định mức / kinh nghiệm bóc tách"
    )
    correction_directive: str | None = Field(
        None, description="Tên tương đương correction_rule"
    )
    created_by_role: str = Field("Kỹ Sư Trưởng QS", description="Người tạo")
    confidence_weight: float = Field(1.0, description="Trọng số tin cậy")

    def get_category(self) -> str:
        return self.category or self.rule_category or "all"

    def get_error_pattern(self) -> str:
        return self.error_pattern or self.trigger_pattern or self.rule_title or ""

    def get_correction_rule(self) -> str:
        return self.correction_rule or self.correction_directive or ""


class ItemQuantitiesUpdateRequest(BaseModel):
    hsmt_quantity: float | None = Field(
        None, description="Khối lượng HSMT / Tiên lượng mời thầu CĐT"
    )
    actual_quantity: float | None = Field(
        None, description="Khối lượng thực tế thi công / Nghiệm thu"
    )
