"""Regression tests for LSQ prospect resolution — customer vs agent phone mismatch."""

from __future__ import annotations

import unittest

from leadsquared_activity_push import (
    _accept_customer_prospect,
    _agent_phone_cores,
    _customer_phone_candidates,
    _lead_is_agent_prospect,
    _lead_matches_customer_phones,
    call_dict_for_lsq_push,
)


class LsqProspectResolutionTests(unittest.TestCase):
    def test_outbound_excludes_agent_phone(self):
        call = {
            "callid": "99862429051783490087",
            "direction": "outbound",
            "agentname": "Pooja Kumar",
            "callfrom": "9986242905",
            "callto": "8281553440",
            "customer_callinfo": "8281553440",
            "customer_phone": "8281553440",
            "agent_callinfo": "9986242905",
        }
        phones = _customer_phone_candidates(call)
        self.assertEqual(phones, ["8281553440"])
        self.assertNotIn("9986242905", phones)

    def test_inbound_keeps_customer_phone(self):
        call = {
            "callid": "98201313321782888128",
            "direction": "inbound",
            "callfrom": "9739390254",
            "callto": "9820131332",
            "customer_callinfo": "9820131332",
            "agent_callinfo": "9739390254",
        }
        phones = _customer_phone_candidates(call)
        self.assertEqual(phones, ["9820131332"])
        self.assertNotIn("9739390254", phones)

    def test_swapped_customer_phone_falls_back_to_callto(self):
        """If customer_callinfo was wrongly stored as the agent, still use callto."""
        call = {
            "direction": "outbound",
            "callfrom": "9986242905",
            "callto": "8281553440",
            "customer_callinfo": "9986242905",
            "customer_phone": "9986242905",
            "agent_callinfo": "9986242905",
        }
        phones = _customer_phone_candidates(call)
        self.assertEqual(phones, ["8281553440"])
        self.assertNotIn("9986242905", phones)

    def test_lead_phone_match_handles_country_code(self):
        lead = {"Phone": "+91-8281553440", "Mobile": None}
        self.assertTrue(_lead_matches_customer_phones(lead, ["8281553440"]))
        self.assertFalse(_lead_matches_customer_phones(lead, ["9986242905"]))

    def test_outbound_callid_prefix_treated_as_agent(self):
        call = {
            "callid": "79960405061784010167",
            "direction": "outbound",
            "agent_callinfo": "7996040506",
            "customer_callinfo": "9842654716",
        }
        cores = _agent_phone_cores(call)
        self.assertIn("7996040506", cores)
        phones = _customer_phone_candidates(call)
        self.assertEqual(phones, ["9842654716"])
        self.assertNotIn("7996040506", phones)

    def test_reject_agent_prospect_even_if_search_returns_it(self):
        agent_lead = {
            "ProspectID": "522349e6-agent",
            "Phone": "+91-7996040506",
        }
        customer_lead = {
            "ProspectID": "bd8b2162-customer",
            "Phone": "+91-9842654716",
        }
        agent_cores = {"7996040506"}
        self.assertTrue(_lead_is_agent_prospect(agent_lead, agent_cores))
        self.assertIsNone(
            _accept_customer_prospect(agent_lead, ["9842654716"], agent_cores)
        )
        self.assertEqual(
            _accept_customer_prospect(customer_lead, ["9842654716"], agent_cores),
            "bd8b2162-customer",
        )

    def test_call_dict_outbound_keeps_customer_and_agent(self):
        shaped = call_dict_for_lsq_push(
            {
                "callid": "79960405061784010167",
                "direction": "outbound",
                "agentname": "Bidhiska",
                "agent_callinfo": "7996040506",
                "customer_callinfo": "9842654716",
                "call_starttime": "2026-07-14 11:52:48",
            }
        )
        self.assertEqual(shaped["customer_callinfo"], "9842654716")
        self.assertEqual(shaped["agent_callinfo"], "7996040506")
        self.assertEqual(shaped["callfrom"], "7996040506")
        self.assertEqual(shaped["callto"], "9842654716")
        self.assertNotEqual(shaped["clicktocalldid"], "9842654716")


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