from django.test import TestCase
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from api.property_api.country.models.country import Country


class CountryAPITestCase(APITestCase):
    def setUp(self):
        self.country_1 = Country.objects.create(name="Bangladesh", code="BD")
        self.country_2 = Country.objects.create(name="India", code="IN", is_active=False)
        self.list_url = reverse("countries-list-create")
        self.dropdown_url = reverse("country-dropdown")
        self.exist_url = lambda name: reverse("country-exist", kwargs={"name": name})
        self.detail_url = lambda pk: reverse("country-detail", kwargs={"pk": pk})

    def test_create_country(self):
        data = {"name": "Nepal", "code": "NP"}
        response = self.client.post(self.list_url, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertTrue(Country.objects.filter(name="Nepal").exists())

    def test_list_active_countries(self):
        response = self.client.get(self.list_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data["results"]), 1)  # only active

    def test_get_country_detail(self):
        response = self.client.get(self.detail_url(self.country_1.id))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["name"], self.country_1.name)

    def test_get_invalid_country_detail(self):
        response = self.client.get(self.detail_url(9999))
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_update_country(self):
        data = {"name": "Bangla", "code": "BD"}
        response = self.client.put(self.detail_url(self.country_1.id), data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.country_1.refresh_from_db()
        self.assertEqual(self.country_1.name, "Bangla")

    def test_soft_delete_country(self):
        response = self.client.delete(self.detail_url(self.country_1.id))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.country_1.refresh_from_db()
        self.assertFalse(self.country_1.is_active)

    def test_dropdown_only_active_countries(self):
        response = self.client.get(self.dropdown_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]["code"], "BD")

    def test_dropdown_no_active_country(self):
        self.country_1.soft_delete()
        response = self.client.get(self.dropdown_url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_country_exist_true(self):
        response = self.client.get(self.exist_url("bangladesh"))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(response.data["isExist"])

    def test_country_exist_false(self):
        response = self.client.get(self.exist_url("nonexistent"))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertFalse(response.data["isExist"])

    def test_country_exist_blank(self):
        response = self.client.get(self.exist_url(" "))
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

