from datetime import timedelta

from django.test import TestCase
from django.utils import timezone

from Employee.models import Department
from Recruitment.tests_hiring_plus import client, mk_company, mk_emp, mk_role


class OrgTests(TestCase):
    def setUp(self):
        self.co = mk_company("org")
        self.admin = client(self.co)
        self.design = Department.objects.create(company=self.co, name="Design")
        self.it = Department.objects.create(company=self.co, name="IT")
        today = timezone.localdate()
        self.a = mk_emp(self.co, "Mohit P", department=self.design, Date_of_Birth=today.replace(year=1995) + timedelta(days=0))
        self.b = mk_emp(self.co, "Dhiraj S", Department="Design")                      # legacy text department only
        self.c = mk_emp(self.co, "Ravi", department=self.it, Date_of_Joining=today)

    def test_birthday_and_joining_show_department_not_dash(self):
        d = self.admin.get("/api/people/org/dashboard/").json()["data"]
        self.assertEqual(d["birthdays"][0]["department"], "Design")
        self.assertEqual(d["new_joining"][0]["department"], "IT")
        self.assertEqual(d["summary"]["total_employees"], 3)
        self.assertEqual(sorted(d["departments_without_head"]), ["Design", "IT"])

    def test_department_details_members_have_360_links(self):
        r = self.admin.get(f"/api/people/org/departments/{self.design.id}/").json()["data"]
        self.assertEqual(r["headcount"], 2)                                       # FK + legacy text both counted
        self.assertFalse(r["head_assigned"])
        self.assertIn("assign a head", r["responsibility_note"])
        m = r["members"][0]
        self.assertTrue(m["profile360_api"].startswith("/api/people/profile360/"))
        self.assertTrue(m["profile360_path"].endswith("/360"))

    def test_assign_head(self):
        url = f"/api/people/org/departments/{self.design.id}/head/"
        self.assertEqual(self.admin.post(url, {}, format="json").status_code, 400)           # cannot leave ownerless
        self.assertEqual(self.admin.post(url, {"head": "NOPE0000"}, format="json").status_code, 400)
        r = self.admin.post(url, {"head": self.a.employee_code, "deputy": self.b.employee_code}, format="json")
        self.assertEqual(r.status_code, 200, r.content)
        self.assertEqual(r.json()["data"]["head"]["employee_code"], self.a.employee_code)
        self.assertEqual(self.admin.post(url, {"head": self.a.employee_code, "deputy": self.a.employee_code},
                                         format="json").status_code, 400)
        un = self.admin.get("/api/people/org/departments/unassigned/").json()
        self.assertEqual([x["name"] for x in un["data"]], ["IT"])

    def test_company_isolation(self):
        other = client(mk_company("o2"))
        self.assertEqual(other.get(f"/api/people/org/departments/{self.design.id}/").status_code, 404)
        self.assertEqual(other.post(f"/api/people/org/departments/{self.design.id}/head/",
                                    {"head": self.a.employee_code}, format="json").status_code, 404)
