# edited by glg
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path

import pytest

pytestmark = [pytest.mark.unit, pytest.mark.critical_flow]


def _load_guard_module():
    repo_root = Path(__file__).resolve().parents[1]
    module_path = repo_root / "scripts" / "ci" / "run_penjualan_quality_guard.py"
    spec = spec_from_file_location("run_penjualan_quality_guard", module_path)
    module = module_from_spec(spec)
    assert spec and spec.loader
    spec.loader.exec_module(module)
    return module


def test_penjualan_quality_guard_lulus():
    guard = _load_guard_module()
    issues = guard.run_penjualan_quality_guard()
    assert issues == []


def test_penjualan_quality_guard_kpi_wajib_100():
    guard = _load_guard_module()
    summary = guard.evaluate_penjualan_kpi()
    assert int(summary.get("score") or 0) == int(summary.get("pass_score") or 0) == 100


def test_budget_delete_transaksi_by_id_tetap_kecil():
    guard = _load_guard_module()
    repo_root = Path(__file__).resolve().parents[1]
    abs_path = repo_root / "pypos" / "modules" / "penjualan" / "models" / "load_transaksi_model.py"
    length = guard._resolve_function_length(
        abs_path=abs_path,
        class_name="LoadTransaksiModel",
        function_name="delete_transaksi_by_id",
    )
    assert int(length) <= 70


def test_budget_init_controller_tetap_ringkas():
    guard = _load_guard_module()
    repo_root = Path(__file__).resolve().parents[1]
    abs_path = (
        repo_root
        / "pypos"
        / "modules"
        / "penjualan"
        / "controllers"
        / "transaksi_penjualan_controller.py"
    )
    length = guard._resolve_function_length(
        abs_path=abs_path,
        class_name="TransaksiPenjualanController",
        function_name="__init__",
    )
    assert int(length) <= 40


def test_budget_build_transaksi_payload_tetap_ringkas():
    guard = _load_guard_module()
    repo_root = Path(__file__).resolve().parents[1]
    abs_path = (
        repo_root
        / "pypos"
        / "modules"
        / "penjualan"
        / "services"
        / "transaksi_payload_service.py"
    )
    length = guard._resolve_function_length(
        abs_path=abs_path,
        class_name="TransaksiPayloadService",
        function_name="build_transaksi_payload",
    )
    assert int(length) <= 60


def test_budget_build_barang_table_and_diskon_info_tetap_ringkas():
    guard = _load_guard_module()
    repo_root = Path(__file__).resolve().parents[1]
    abs_path = (
        repo_root
        / "pypos"
        / "modules"
        / "penjualan"
        / "views"
        / "index.py"
    )
    length = guard._resolve_function_length(
        abs_path=abs_path,
        class_name="TransaksiPenjualanView",
        function_name="build_barang_table_and_diskon_info",
    )
    assert int(length) <= 50


def test_budget_cari_barang_by_id_tetap_ringkas():
    guard = _load_guard_module()
    repo_root = Path(__file__).resolve().parents[1]
    abs_path = (
        repo_root
        / "pypos"
        / "modules"
        / "penjualan"
        / "services"
        / "transaksi_lookup_harga_service.py"
    )
    length = guard._resolve_function_length(
        abs_path=abs_path,
        class_name="TransaksiLookupHargaService",
        function_name="cari_barang_by_id",
    )
    assert int(length) <= 55
