"""Test cases for countryiso library (CRUD and lookup tests)."""

import os
import sys
import unittest


sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src"))

from countryiso.db import countries, Country


class TestCountryISO(unittest.TestCase):
    def setUp(self):
        self.cn = countries.get(alpha2="CN")

    def test_country_lookup_by_alpha2(self):
        c = countries.get(alpha2="CN")
        self.assertIsNotNone(c)
        self.assertEqual(c.alpha2, "CN")

    def test_country_lookup_by_alpha3(self):
        c = countries.get(alpha3="CHN")
        self.assertEqual(c.alpha3, "CHN")

    def test_country_lookup_by_numeric(self):
        c = countries.get(numeric="156")
        self.assertEqual(c.numeric, "156")

    def test_country_lookup_by_chinese_name(self):
        c = countries.get(chinese_name="中国")
        self.assertEqual(c.chinese_name, "中国")

    def test_country_fields_complete(self):
        c = self.cn
        self.assertEqual(c.alpha2, "CN")
        self.assertEqual(c.alpha3, "CHN")
        self.assertEqual(c.name, "China")
        self.assertEqual(c.official_name, "People's Republic of China")
        self.assertEqual(c.numeric, "156")
        self.assertEqual(c.flag, "🇨🇳")
        self.assertEqual(c.chinese_name, "中国")

    def test_update_country_chinese_name(self):
        updated = countries.update(self.cn, chinese_name="中国(中华人民共和国)")
        self.assertEqual(updated.chinese_name, "中国(中华人民共和国)")
        countries.update(updated, chinese_name="中国")

    def test_add_and_remove_country(self):
        test_country = Country(
            alpha_2="XX",
            alpha_3="XXX",
            name="Testland",
            official_name="Test Republic",
            numeric="999",
            flag="🇽🇽",
            chinese_name="测试国"
        )
        countries.add(test_country)
        found = countries.get(alpha2="XX")
        self.assertEqual(found.chinese_name, "测试国")
        countries.remove(test_country)
        gone = countries.get(alpha2="XX")
        self.assertIsNone(gone)


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