from __future__ import annotations

import unittest
from datetime import UTC, date, datetime

from sqlalchemy import BigInteger, create_engine
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.orm import Session, sessionmaker


@compiles(BigInteger, "sqlite")
def _compile_bigint_for_sqlite(type_, compiler, **kw) -> str:
    _ = type_, compiler, kw
    return "INTEGER"


import app.models.master_data  # noqa: E402,F401
import app.models.miniapp  # noqa: E402,F401
import app.models.operations  # noqa: E402,F401
import app.models.org  # noqa: E402,F401
import app.models.planning  # noqa: E402,F401
import app.models.sales  # noqa: E402,F401
from app.api.routes.purchase import build_purchase_order_no  # noqa: E402
from app.models.base import Base  # noqa: E402
from app.models.operations import PurchaseOrder, Supplier  # noqa: E402


class PurchaseOrderNoGenerationTest(unittest.TestCase):
    def setUp(self) -> None:
        engine = create_engine("sqlite+pysqlite:///:memory:", future=True)
        Base.metadata.create_all(engine)
        self.SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, future=True)
        self.db: Session = self.SessionLocal()
        self.supplier = Supplier(
            id=1,
            supplier_code="SUP-001",
            supplier_name="测试供应商",
            short_name="供应商",
            lead_time_days=0,
            default_tax_rate=0,
            status="ACTIVE",
        )
        self.db.add(self.supplier)
        self.db.commit()

    def tearDown(self) -> None:
        self.db.close()

    def _add_purchase_order(self, po_no: str) -> None:
        now = datetime.now(UTC)
        self.db.add(
            PurchaseOrder(
                po_no=po_no,
                supplier_id=self.supplier.id,
                order_date=date.today(),
                expected_date=None,
                purchaser_employee_id=None,
                tax_rate=0,
                total_amount=0,
                status="OPEN",
                remark=None,
                created_at=now,
                updated_at=now,
            )
        )
        self.db.commit()

    def test_purchase_order_no_starts_from_named_year_sequence(self) -> None:
        year = date.today().strftime("%Y")

        self.assertEqual(build_purchase_order_no(self.db, self.supplier), f"采购{year}-00001")

    def test_purchase_order_no_increments_matching_current_year_codes(self) -> None:
        year = date.today().strftime("%Y")
        previous_year = str(int(year) - 1)
        self._add_purchase_order(f"采购{year}-00001")
        self._add_purchase_order(f"采购{year}-00009")
        self._add_purchase_order(f"采购{previous_year}-00099")
        self._add_purchase_order(f"{year}-00099")
        self._add_purchase_order("PO-NB-20260523-SUP001-001")

        self.assertEqual(build_purchase_order_no(self.db, self.supplier), f"采购{year}-00010")

    def test_purchase_order_no_ignores_malformed_same_year_codes(self) -> None:
        year = date.today().strftime("%Y")
        self._add_purchase_order(f"采购{year}-001")
        self._add_purchase_order(f"采购{year}-000001")
        self._add_purchase_order(f"采购{year}-ABCDE")

        self.assertEqual(build_purchase_order_no(self.db, self.supplier), f"采购{year}-00001")


if __name__ == "__main__":
    unittest.main()
