# edited by glg
import unittest

from pypos.modules.sinkronisasi.services.sync_api_service import SyncApiService


class _DummyResponse:
    status_code = 200
    headers = {}
    text = ""

    def json(self):
        return {"row": 0, "data": {}, "connection": 1}


class _DummyModel:
    def __init__(self):
        self.logs = []

    def get_last_sync_info(self):
        return {
            "produk": {"last_update": "2026-03-13 00:00:00", "last_id": 10},
            "price": {"last_update": "2026-03-13 00:00:00", "last_id": 20},
        }

    def _resolve_cabang_id(self, machine_id, cabang_id):
        return int(cabang_id or 0)

    def _log_sync(self, message, level="INFO", context=None):
        self.logs.append(
            {
                "message": str(message or ""),
                "level": str(level or "INFO"),
                "context": context if isinstance(context, dict) else {},
            }
        )


class SyncApiServiceCheckUpdatePayloadTests(unittest.TestCase):
    def test_check_update_server_mengirim_payload_dasar_tanpa_mode_tambahan(self):
        service = SyncApiService()
        model = _DummyModel()
        captured = {}

        def _fake_request(method, url, data=None, timeout=None, retry_on=None, auth_required=True):
            captured["method"] = method
            captured["url"] = url
            captured["data"] = dict(data or {})
            captured["auth_required"] = bool(auth_required)
            return _DummyResponse()

        service._build_url = lambda endpoint_key: "http://localhost/check-update"
        service.request_with_retry = _fake_request

        out = service.check_update_server(
            model=model,
            machine_id="MACHINE-1",
            cabang_id=100,
            tables=["produk", "price"],
            force_full_tables=[],
        )

        self.assertEqual(out.get("row"), 0)
        self.assertEqual(captured["method"], "POST")
        self.assertEqual(captured["data"].get("machine_id"), "MACHINE-1")
        self.assertEqual(int(captured["data"].get("cabang_id") or 0), 100)
        self.assertNotIn("notify_mode", captured["data"])
        self.assertNotIn("watch_tables", captured["data"])

    def test_check_update_server_tanpa_param_baru_tetap_kompatibel(self):
        service = SyncApiService()
        model = _DummyModel()
        captured = {}

        def _fake_request(method, url, data=None, timeout=None, retry_on=None, auth_required=True):
            captured["data"] = dict(data or {})
            return _DummyResponse()

        service._build_url = lambda endpoint_key: "http://localhost/check-update"
        service.request_with_retry = _fake_request

        out = service.check_update_server(
            model=model,
            machine_id="MACHINE-1",
            cabang_id=100,
            tables=["produk"],
            force_full_tables=[],
        )

        self.assertEqual(out.get("connection"), 1)
        self.assertNotIn("notify_mode", captured["data"])
        self.assertNotIn("watch_tables", captured["data"])


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