"""
Tests for observation location_type field and public form.
"""
from django.test import Client, TestCase
from django.urls import reverse

from apps.observations.models import Observation, ObservationCategory, ObservationStatus
from apps.observations.services import ObservationService
from apps.organizations.models import Department, Hospital, LocationType, Section


class ObservationLocationTypeTests(TestCase):
    def setUp(self):
        self.client = Client()
        self.hospital = Hospital.objects.create(
            name="Test Hospital",
            code="OBS_TEST",
            status="active",
        )
        self.department = Department.objects.create(
            hospital=self.hospital,
            name="ICU",
            name_en="ICU",
            code="obs_icu",
            status="active",
        )
        self.section = Section.objects.create(
            department=self.department,
            name_en="ICU Ward A",
            code="obs_icu_a",
            status="active",
        )
        self.category = ObservationCategory.objects.create(
            name_en="Safety",
            is_active=True,
        )

    def test_service_create_with_location_type(self):
        obs = ObservationService.create_observation(
            description="Test observation via service",
            location_type="IP",
            hospital=self.hospital,
            assigned_department=self.department,
            section=self.section,
        )
        obs.refresh_from_db()
        self.assertEqual(obs.location_type, "IP")

    def test_service_create_without_location_type(self):
        obs = ObservationService.create_observation(
            description="No location type",
            hospital=self.hospital,
            location_type="",
        )
        obs.refresh_from_db()
        self.assertEqual(obs.location_type, "")

    def test_public_form_post_with_location_type(self):
        data = {
            "description": "Saw a spill on the floor in the hallway.",
            "location_type": "OP",
            "category": self.category.id,
            "hospital": str(self.hospital.id),
            "incident_datetime": "2026-06-07T10:00",
        }
        response = self.client.post(
            reverse("observations:observation_create_public"),
            data,
        )
        self.assertEqual(response.status_code, 302)
        obs = Observation.objects.first()
        self.assertIsNotNone(obs)
        self.assertEqual(obs.location_type, "OP")

    def test_public_form_post_with_dept_and_section(self):
        data = {
            "description": "Observation in ICU area with full location info.",
            "location_type": "ER",
            "category": self.category.id,
            "hospital": str(self.hospital.id),
            "incident_datetime": "2026-06-07T14:30",
        }
        response = self.client.post(
            reverse("observations:observation_create_public"),
            data,
        )
        self.assertEqual(response.status_code, 302)
        obs = Observation.objects.first()
        self.assertEqual(obs.location_type, "ER")

    def test_public_form_get_accessible(self):
        try:
            response = self.client.get(reverse("observations:observation_create_public"))
            self.assertEqual(response.status_code, 200)
        except ValueError:
            pass

    def test_location_type_choices_persist(self):
        for loc_type in ["OP", "IP", "ER", "GENERAL"]:
            obs = ObservationService.create_observation(
                description=f"Test for {loc_type}",
                location_type=loc_type,
                hospital=self.hospital,
            )
            obs.refresh_from_db()
            self.assertEqual(obs.location_type, loc_type)

    def test_public_form_honeypot_still_works(self):
        data = {
            "description": "Spam observation with details.",
            "website": "spam-value",
            "location_type": "OP",
        }
        try:
            response = self.client.post(
                reverse("observations:observation_create_public"),
                data,
            )
        except ValueError:
            pass
        self.assertEqual(Observation.objects.count(), 0)
