from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

from vbv_lernwelt.core.model_utils import add_countries, add_organisations
from vbv_lernwelt.core.models import User


class MeUserViewTest(APITestCase):
    def setUp(self) -> None:
        self.user = User.objects.create_user(
            "testuser", "test@example.com", "testpassword"
        )
        self.client.login(username="testuser", password="testpassword")
        add_organisations()
        add_countries()

    def test_user_can_update_language(self) -> None:
        # GIVEN
        url = reverse("me_user_view")

        # WHEN
        response = self.client.put(url, {"language": "it"})

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        updated_user = User.objects.get(username="testuser")
        self.assertEquals(updated_user.language, "it")

    def test_user_can_update_org(self) -> None:
        # GIVEN
        url = reverse("me_user_view")  # replace with your actual URL name

        # WHEN
        response = self.client.put(url, {"organisation": 6})

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        updated_user = User.objects.get(username="testuser")
        self.assertEquals(updated_user.organisation.organisation_id, 6)
