from __future__ import annotations

from typing import Any

from pydantic import BaseModel, Field


class ReconcileRequest(BaseModel):
    project_id: str = Field(..., description="ID của Dự án cần khớp nối")
    wbs_id: str | None = Field(None, description="ID của Hạng mục WBS")


class DirectXmlUploadRequest(BaseModel):
    xml_content: str = Field(..., description="Nội dung chuỗi XML hóa đơn gốc")
    source_channel: str = Field(
        "manual_xml",
        description="Nguồn thu thập: manual_xml, meinvoice_web, email_import",
    )


class FidicIpcItemRequest(BaseModel):
    item_name: str = Field(..., description="Tên công việc / hạng mục nghiệm thu")
    item_code: str | None = Field(None, description="Mã hạng mục WBS hoặc mã định mức")
    unit: str = Field("Đợt", description="Đơn vị tính")
    quantity: float = Field(1.0, description="Khối lượng nghiệm thu")
    unit_price_vnd: float = Field(..., description="Đơn giá hợp đồng (VNĐ)")


class FidicIpcInvoiceRequest(BaseModel):
    project_id: str = Field(..., description="ID Dự án")
    wbs_items: list[FidicIpcItemRequest] = Field(
        ..., description="Danh sách các hạng mục nghiệm thu"
    )
    advance_deduction_pct: float = Field(10.0, description="Tỷ lệ khấu trừ tạm ứng (%)")
    retention_pct: float = Field(5.0, description="Tỷ lệ giữ lại bảo hành (%)")
    vat_rate_pct: float = Field(8.0, description="Thuế suất VAT (%)")
    buyer_tax_code: str | None = Field(None, description="MST Chủ đầu tư / Khách hàng")
    buyer_name: str | None = Field(None, description="Tên Chủ đầu tư / Khách hàng")
    buyer_address: str | None = Field(None, description="Địa chỉ Chủ đầu tư")
    invoice_number: str | None = Field(
        None, description="Số hóa đơn (tự sinh nếu để trống)"
    )
    invoice_series: str | None = Field(
        None, description="Ký hiệu hóa đơn (vd: 1C26TDS)"
    )
    notes: str | None = Field(None, description="Ghi chú đợt nghiệm thu")


class SyncConfigRequest(BaseModel):
    service_type: str = Field(
        ..., description="Loại dịch vụ: gdt_portal, meinvoice_web, email_imap"
    )
    tax_code: str = Field(..., description="Mã số thuế doanh nghiệp")
    username: str = Field(..., description="Tên đăng nhập hoặc Email")
    password: str = Field(
        ..., description="Mật khẩu Cổng Thuế hoặc Mật khẩu ứng dụng Email"
    )
    extra_config: dict[str, Any] | None = Field(
        default_factory=dict,
        description="Cấu hình phụ: imap_server, imap_port, use_ssl",
    )
    is_active: bool = Field(True, description="Kích hoạt")


class TestConnectionRequest(BaseModel):
    service_type: str = Field(
        ..., description="Loại dịch vụ: gdt_portal, meinvoice_web, email_imap"
    )
    tax_code: str = Field(..., description="Mã số thuế doanh nghiệp")
    username: str = Field(..., description="Tên đăng nhập hoặc Email")
    password: str = Field(
        ..., description="Mật khẩu Cổng Thuế hoặc Mật khẩu ứng dụng Email"
    )
    extra_config: dict[str, Any] | None = Field(
        default_factory=dict,
        description="Cấu hình phụ: imap_server, imap_port, use_ssl",
    )


class SchedulerPauseRequest(BaseModel):
    reason: str | None = Field(
        None,
        description="Lý do tạm ngưng (vd: Tài khoản Cổng Thuế bị khóa chờ mở lại)",
    )


class SyncGdtRequest(BaseModel):
    tax_code: str | None = Field(
        None,
        description="Mã số thuế doanh nghiệp (để trống nếu tự động lấy từ cấu hình)",
    )
    from_date: str | None = Field(None, description="Từ ngày (YYYY-MM-DD)")
    to_date: str | None = Field(None, description="Đến ngày (YYYY-MM-DD)")
    sync_type: str = Field("all", description="Loại hóa đơn: all, purchase, sold")


class IssueInvoiceItemRequest(BaseModel):
    item_name: str = Field(..., description="Tên hàng hóa, dịch vụ, hạng mục thi công")
    item_code: str | None = Field(None, description="Mã hàng hóa / vật tư / dịch vụ")
    unit: str = Field("Lô", description="Đơn vị tính: m3, tấn, ca, m2, cái, đợt...")
    quantity: float = Field(1.0, description="Số lượng")
    unit_price_vnd: float = Field(0.0, description="Đơn giá chưa thuế (VNĐ)")
    vat_rate_percent: float | None = Field(
        None, description="Thuế suất VAT riêng từng dòng (nếu có)"
    )
    discount_rate_percent: float | None = Field(0.0, description="Tỷ lệ chiết khấu (%)")
    discount_amount_vnd: float | None = Field(0.0, description="Tiền chiết khấu (VNĐ)")


class IssueInvoiceRequest(BaseModel):
    buyer_name: str = Field(..., description="Tên đơn vị mua hàng / Chủ đầu tư")
    buyer_tax_code: str | None = Field(None, description="Mã số thuế bên mua")
    buyer_address: str | None = Field(None, description="Địa chỉ bên mua")
    buyer_email: str | None = Field(
        None, description="Email nhận hóa đơn điện tử của khách hàng"
    )
    buyer_phone: str | None = Field(None, description="Số điện thoại người mua")
    buyer_representative: str | None = Field(
        None, description="Người mua hàng / đại diện"
    )
    items: list[IssueInvoiceItemRequest] = Field(
        ..., description="Danh sách các dòng hàng hóa / dịch vụ"
    )
    vat_rate_percent: float = Field(
        8.0, description="Thuế suất GTGT chung: 0, 5, 8, 10"
    )
    invoice_series: str = Field("1C26TDS", description="Ký hiệu hóa đơn (vd: 1C26TDS)")
    invoice_number: str | None = Field(
        None, description="Số hóa đơn (để trống để tự động tăng)"
    )
    issue_date: str | None = Field(None, description="Ngày lập hóa đơn (YYYY-MM-DD)")
    payment_method: str = Field(
        "TM/CK", description="Hình thức thanh toán: TM/CK, CK, TM, Đối trừ công nợ"
    )
    notes: str | None = Field(None, description="Ghi chú hóa đơn")
    project_id: str | None = Field(None, description="Khớp nối với ID dự án (nếu có)")
    is_draft: bool = Field(
        False, description="Lưu nháp (True) hoặc Ký số phát hành ngay (False)"
    )
    send_email_to_buyer: bool = Field(
        False, description="Tự động gửi email thông báo hóa đơn cho khách hàng"
    )


class SendEmailInvoiceRequest(BaseModel):
    buyer_email: str = Field(..., description="Địa chỉ email người nhận hóa đơn")
