from __future__ import annotations

from decimal import Decimal
import pytest

from app.modules.invoices.application.invoice_processing.four_pillars_accounting_engine import (
    FourPillarsAccountingEngine,
    PILLAR_1_CONSTRUCTION,
    PILLAR_2_LOGISTICS,
    PILLAR_3_MACHINERY,
    PILLAR_4_MATERIALS,
    PILLAR_FINANCIAL,
)


def test_classify_construction_materials():
    """Hóa đơn mua xi măng và gạch không nung phải được phân loại vào Trụ Cột 1 và định khoản TK 621."""
    inv = {
        "invoice_number": "00000495",
        "seller_name": "CÔNG TY TNHH THƯƠNG MẠI VẬN TẢI & DU LỊCH QUANG TRUNG",
        "seller_tax_code": "0202228536",
        "total_amount_vnd": Decimal("35424000.0000"),
        "subtotal_amount_vnd": Decimal("32800000.0000"),
        "vat_amount_vnd": Decimal("2624000.0000"),
    }
    items = [
        {"item_name": "Xi măng bao PCB30", "quantity": 12000, "unit": "Kg"},
        {"item_name": "Gạch xây không nung", "quantity": 10000, "unit": "Viên"},
    ]
    res = FourPillarsAccountingEngine.classify_invoice_and_account(inv, items)

    assert res["pillar"] == PILLAR_1_CONSTRUCTION
    assert res["pillar_code"] == "pillar_1"
    assert res["debit_account"] == "621"
    assert res["debit_vat_account"] == "1331"
    assert res["credit_account"] == "331"
    assert res["payment_requirement"] == "BAT_BUOC_CHUYEN_KHOAN_UNC"


def test_classify_machinery_rental():
    """Hóa đơn thuê máy đào bánh xích Yanmar, Sumitomo phải được phân loại vào Trụ Cột 3 và định khoản TK 623."""
    inv = {
        "invoice_number": "00000034",
        "seller_name": "CÔNG TY TNHH THƯƠNG MẠI VÀ XÂY DỰNG CÔNG TRÌNH TRUNG NGHĨA",
        "seller_tax_code": "0202123456",
        "total_amount_vnd": Decimal("217080000.0000"),
        "subtotal_amount_vnd": Decimal("201000000.0000"),
        "vat_amount_vnd": Decimal("16080000.0000"),
    }
    items = [
        {"item_name": "Máy đào bánh xích YANMAR", "quantity": 31, "unit": "Ca"},
        {"item_name": "Máy đào bánh xích SUMITOMO SH125X-3", "quantity": 31, "unit": "Ca"},
    ]
    res = FourPillarsAccountingEngine.classify_invoice_and_account(inv, items)

    assert res["pillar"] == PILLAR_3_MACHINERY
    assert res["pillar_code"] == "pillar_3"
    assert res["debit_account"] == "623"
    assert res["credit_account"] == "331"
    assert res["payment_requirement"] == "BAT_BUOC_CHUYEN_KHOAN_UNC"


def test_classify_fleet_fuel_logistics():
    """Hóa đơn mua dầu DO từ Petrolimex phục vụ xe ben phải được phân loại vào Trụ Cột 2 (Logistics)."""
    inv = {
        "invoice_number": "00724096",
        "seller_name": "CÔNG TY TNHH MTV PETROLIMEX HẢI PHÒNG",
        "seller_tax_code": "0200107765",
        "total_amount_vnd": Decimal("1270833.0000"),
        "subtotal_amount_vnd": Decimal("1176697.0000"),
        "vat_amount_vnd": Decimal("94136.0000"),
    }
    items = [
        {"item_name": "Dầu Điêzen 0,05S-II", "quantity": 65, "unit": "Lít"},
    ]
    res = FourPillarsAccountingEngine.classify_invoice_and_account(inv, items)

    assert res["pillar"] == PILLAR_2_LOGISTICS
    assert res["pillar_code"] == "pillar_2"
    assert res["debit_account"] == "154_LOGISTICS"
    assert res["payment_requirement"] == "TIEN_MAT_HOAC_UNC"


def test_classify_financial_banking_fees():
    """Hóa đơn phí dịch vụ tài khoản từ ngân hàng Techcombank phải được phân loại vào Chi phí Tài chính."""
    inv = {
        "invoice_number": "02574306",
        "seller_name": "NGÂN HÀNG THƯƠNG MẠI CỔ PHẦN KỸ THƯƠNG VIỆT NAM",
        "seller_tax_code": "0100230800",
        "total_amount_vnd": Decimal("1074150.0000"),
        "subtotal_amount_vnd": Decimal("976500.0000"),
        "vat_amount_vnd": Decimal("97650.0000"),
    }
    items = [
        {"item_name": "Phí dịch vụ ngân hàng", "quantity": 1, "unit": "Gói"},
    ]
    res = FourPillarsAccountingEngine.classify_invoice_and_account(inv, items)

    assert res["pillar"] == PILLAR_FINANCIAL
    assert res["pillar_code"] == "financial"
    assert res["debit_account"] == "635"
    assert res["credit_account"] == "1121"
