"""
Tests for public inquiry form and view (both complaints and core app handlers).
"""
from datetime import date

from django.test import Client, TestCase
from django.urls import reverse

from apps.complaints.models import Inquiry
from apps.organizations.models import Area, Department, Hospital, LocationType, Section


class PublicInquiryViewTests(TestCase):
    def setUp(self):
        self.client = Client()
        self.hospital = Hospital.objects.create(
            name="Test Hospital",
            code="TEST",
            status="active",
        )
        self.department = Department.objects.create(
            hospital=self.hospital,
            name="Reception",
            name_en="Reception",
            code="test_recv",
            status="active",
        )
        self.section = Section.objects.create(
            department=self.department,
            name_en="Front Desk",
            code="test_recv_fd",
            status="active",
        )

    def test_public_inquiry_form_get(self):
        response = self.client.get(reverse("inquiries:public_inquiry_submit"))
        self.assertEqual(response.status_code, 200)

    def test_public_inquiry_post_saves_location_type(self):
        data = {
            "name": "Jane Doe",
            "phone": "0598765432",
            "email": "jane@example.com",
            "hospital": str(self.hospital.id),
            "location_type": "OP",
            "category": "general",
            "subject": "Test Inquiry",
            "message": "This is a test inquiry message.",
        }
        response = self.client.post(
            reverse("inquiries:public_inquiry_submit"),
            data,
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertTrue(result["success"])

        inquiry = Inquiry.objects.first()
        self.assertIsNotNone(inquiry)
        self.assertEqual(inquiry.location_type, "OP")
        self.assertEqual(inquiry.hospital_id, self.hospital.id)

    def test_public_inquiry_post_with_dept_and_section(self):
        data = {
            "name": "Jane Doe",
            "phone": "0598765432",
            "email": "",
            "hospital": str(self.hospital.id),
            "location_type": "IP",
            "department": str(self.department.id),
            "section": str(self.section.id),
            "category": "billing",
            "subject": "Billing Question",
            "message": "I have a question about my bill.",
        }
        response = self.client.post(
            reverse("inquiries:public_inquiry_submit"),
            data,
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(response.status_code, 200)
        inquiry = Inquiry.objects.first()
        self.assertEqual(inquiry.location_type, "IP")
        self.assertEqual(inquiry.department_id, self.department.id)
        self.assertEqual(inquiry.section_id, self.section.id)

    def test_public_inquiry_post_missing_required_fields(self):
        data = {
            "name": "",
            "phone": "",
            "message": "",
        }
        response = self.client.post(
            reverse("inquiries:public_inquiry_submit"),
            data,
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(response.status_code, 400)
        result = response.json()
        self.assertFalse(result["success"])

    def test_public_inquiry_post_invalid_hospital(self):
        data = {
            "name": "Jane",
            "phone": "0598765432",
            "hospital": "00000000-0000-0000-0000-000000000000",
            "subject": "Test",
            "message": "Test message.",
        }
        response = self.client.post(
            reverse("inquiries:public_inquiry_submit"),
            data,
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(response.status_code, 400)


class CoreInquiryViewTests(TestCase):
    def setUp(self):
        self.client = Client()
        self.hospital = Hospital.objects.create(
            name="Core Test Hospital",
            code="CORE",
            status="active",
        )
        self.department = Department.objects.create(
            hospital=self.hospital,
            name="Admin",
            name_en="Admin",
            code="core_admin",
            status="active",
        )
        self.section = Section.objects.create(
            department=self.department,
            name_en="HR Section",
            code="core_admin_hr",
            status="active",
        )

    def test_core_inquiry_post_saves_location_type(self):
        data = {
            "name": "Alice Smith",
            "phone": "0511122233",
            "email": "alice@example.com",
            "hospital": str(self.hospital.id),
            "location_type": "ER",
            "category": "appointment",
            "subject": "Appointment Inquiry",
            "message": "When is my appointment?",
            "department": str(self.department.id),
            "section": str(self.section.id),
        }
        response = self.client.post(reverse("core:public_inquiry_submit"), data)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertTrue(result["success"])

        inquiry = Inquiry.objects.first()
        self.assertIsNotNone(inquiry)
        self.assertEqual(inquiry.location_type, "ER")
        self.assertEqual(inquiry.department_id, self.department.id)
        self.assertEqual(inquiry.section_id, self.section.id)

    def test_core_inquiry_post_without_location_type(self):
        data = {
            "name": "Bob Jones",
            "phone": "0544455566",
            "email": "",
            "hospital": str(self.hospital.id),
            "location_type": "",
            "subject": "General Question",
            "message": "Just wondering about something.",
        }
        response = self.client.post(reverse("core:public_inquiry_submit"), data)
        self.assertEqual(response.status_code, 200)
        inquiry = Inquiry.objects.first()
        self.assertEqual(inquiry.location_type, "")

    def test_core_inquiry_post_missing_name(self):
        data = {
            "name": "",
            "phone": "0544455566",
            "hospital": str(self.hospital.id),
            "message": "Test.",
        }
        response = self.client.post(reverse("core:public_inquiry_submit"), data)
        self.assertEqual(response.status_code, 400)


class CoreObservationViewTests(TestCase):
    def setUp(self):
        self.client = Client()
        self.hospital = Hospital.objects.create(
            name="Obs Test Hospital",
            code="OBS",
            status="active",
        )
        self.department = Department.objects.create(
            hospital=self.hospital,
            name="Lab",
            name_en="Lab",
            code="obs_lab",
            status="active",
        )
        self.section = Section.objects.create(
            department=self.department,
            name_en="Blood Draw",
            code="obs_lab_bd",
            status="active",
        )

    def test_core_observation_post_saves_location_type(self):
        data = {
            "hospital": str(self.hospital.id),
            "location_type": "OP",
            "description": "I noticed something wrong in the lab area.",
            "severity": "medium",
            "department": str(self.department.id),
            "section": str(self.section.id),
        }
        response = self.client.post(reverse("core:public_observation_submit"), data)
        self.assertEqual(response.status_code, 200)
        result = response.json()
        self.assertTrue(result["success"])

        from apps.observations.models import Observation

        obs = Observation.objects.first()
        self.assertIsNotNone(obs)
        self.assertEqual(obs.location_type, "OP")
        self.assertEqual(obs.assigned_department_id, self.department.id)
        self.assertEqual(obs.section_id, self.section.id)
