import unittest

from real_estate_agent.scraper import build_queries, extract_price, parse_bing_rss_results, parse_duckduckgo_results, score_result


class ScraperTests(unittest.TestCase):
    def test_build_queries_includes_montpellier_ring_towns(self):
        queries = build_queries(center="Montpellier", radius_km=20, max_price=400000, property_type="maison")
        self.assertGreaterEqual(len(queries), 1)
        joined = "\n".join(queries)
        self.assertIn("Montpellier", joined)
        self.assertIn("Castelnau-le-Lez", joined)
        self.assertIn("Lattes", joined)
        self.assertIn("maison à vendre", joined)

    def test_parse_duckduckgo_results_extracts_title_link_and_snippet(self):
        html = """
        <html><body>
          <a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com%2Flisting1">Maison Montpellier 340000</a>
          <a rel="nofollow" class="result__snippet">Belle maison 4 pièces, jardin, Montpellier.</a>
          <a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com%2Flisting2">Villa proche Lattes</a>
          <a rel="nofollow" class="result__snippet">400 000 € max, 15 km de Montpellier.</a>
        </body></html>
        """
        results = parse_duckduckgo_results(html)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]["title"], "Maison Montpellier 340000")
        self.assertEqual(results[0]["url"], "https://example.com/listing1")
        self.assertIn("jardin", results[0]["snippet"])
        self.assertEqual(results[1]["title"], "Villa proche Lattes")
        self.assertEqual(results[1]["url"], "https://example.com/listing2")

    def test_parse_bing_rss_results_extracts_entries(self):
        xml = """
        <rss version="2.0">
          <channel>
            <item>
              <title>Maison à vendre Montpellier</title>
              <link>https://example.com/1</link>
              <description>Maison 390 000 € près de Castelnau-le-Lez</description>
            </item>
            <item>
              <title>Villa à vendre Lattes</title>
              <link>https://example.com/2</link>
              <description>400k€ max</description>
            </item>
          </channel>
        </rss>
        """
        results = parse_bing_rss_results(xml)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]["title"], "Maison à vendre Montpellier")
        self.assertEqual(results[0]["url"], "https://example.com/1")
        self.assertIn("Castelnau", results[0]["snippet"])

    def test_extract_price_supports_formats(self):
        self.assertEqual(extract_price("Maison à 399 000 € avec jardin"), 399000)
        self.assertEqual(extract_price("Prix: 400k€"), 400000)
        self.assertIsNone(extract_price("Prix sur demande"))

    def test_score_result_prefers_price_and_distance_match(self):
        result = {
            "title": "Maison Montpellier",
            "snippet": "À 12 km de Montpellier, 390 000 €",
            "url": "https://example.com",
            "source": "PAP",
        }
        score = score_result(result, center="Montpellier", radius_km=20, max_price=400000, property_type="maison")
        self.assertGreater(score, 0)


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