"""
Role checkboxes added for modules that previously had no slot of their own:
recruitment, training, communication, timesheet, relations, compliance.

Rules under test
  - every new flag exists, defaults to False, and shows up in the RBAC matrix
  - ticking the module's own flag grants access
  - the old fallbacks (employees / attendance / payroll flags) still work,
    so existing HR roles are not locked out
  - can_manage_<module> grants everything for that module
  - a role with none of them is denied
"""
from django.test import SimpleTestCase

from Employee.models import EmployeeDT, EmployeeRole
from Employee.employee_crud_permissions import role_allows

NEW_MODULES = ["recruitment", "training", "communication", "timesheet", "relations", "compliance"]
ACTIONS = ["view", "create", "edit", "delete", "manage"]


def make_user(**flags):
    role = EmployeeRole(role_name="T", role_code="t", company_id=1, is_active=True)
    for k, v in flags.items():
        setattr(role, k, v)
    user = EmployeeDT(company_id=1, employee_code="EMP900")
    user.role = role
    return user


class NewRoleFlagsExist(SimpleTestCase):
    def test_all_new_flags_exist_and_default_false(self):
        role = EmployeeRole(role_name="T", role_code="t")
        for m in NEW_MODULES:
            for a in ACTIONS:
                self.assertIs(getattr(role, f"can_{a}_{m}"), False, f"can_{a}_{m}")

    def test_new_modules_appear_in_rbac_matrix(self):
        from AdminCenter.views_access import role_flags
        mods = role_flags()
        for m in NEW_MODULES:
            self.assertEqual(mods.get(m), ACTIONS, m)


class RecruitmentAccess(SimpleTestCase):
    def test_own_flag_grants_access(self):
        from Recruitment.common import recruitment_allows
        u = make_user(can_edit_recruitment=True)
        self.assertTrue(recruitment_allows(u, "edit"))
        self.assertTrue(recruitment_allows(u, "approve"))      # approve maps to edit
        self.assertFalse(recruitment_allows(u, "delete"))

    def test_old_employees_flag_still_works(self):
        from Recruitment.common import recruitment_allows
        self.assertTrue(recruitment_allows(make_user(can_edit_employees=True), "edit"))

    def test_no_flags_denied(self):
        from Recruitment.common import recruitment_allows
        self.assertFalse(recruitment_allows(make_user(), "create"))


class TrainingCommunicationTimesheetAccess(SimpleTestCase):
    def test_training(self):
        from Training.common import training_allows
        self.assertTrue(training_allows(make_user(can_view_training=True), "view"))
        self.assertTrue(training_allows(make_user(can_view_employees=True), "view"))   # fallback
        self.assertFalse(training_allows(make_user(can_view_training=True), "edit"))

    def test_communication(self):
        from Communication.common import comm_allows
        self.assertTrue(comm_allows(make_user(can_create_communication=True), "create"))
        self.assertFalse(comm_allows(make_user(), "create"))

    def test_timesheet_falls_back_to_attendance(self):
        from Timesheet.common import timesheet_allows
        self.assertTrue(timesheet_allows(make_user(can_view_timesheet=True), "view"))
        self.assertTrue(timesheet_allows(make_user(can_view_attendance=True), "view"))
        self.assertFalse(timesheet_allows(make_user(), "view"))


class RelationsAccess(SimpleTestCase):
    def test_own_flag_grants_view_without_employee_edit(self):
        from EmployeeRelations.common import relations_allows
        u = make_user(can_view_relations=True)
        self.assertTrue(relations_allows(u, "view"))
        self.assertFalse(relations_allows(u, "edit"))

    def test_old_rule_still_works(self):
        from EmployeeRelations.common import relations_allows
        u = make_user(can_edit_employees=True)
        self.assertTrue(relations_allows(u, "view"))           # old rule: view needs employees EDIT

    def test_view_only_employees_role_is_still_excluded(self):
        from EmployeeRelations.common import relations_allows
        self.assertFalse(relations_allows(make_user(can_view_employees=True), "view"))

    def test_manage_grants_everything(self):
        from EmployeeRelations.common import relations_allows
        u = make_user(can_manage_relations=True)
        for a in ("view", "create", "edit", "delete"):
            self.assertTrue(relations_allows(u, a), a)


class ComplianceAccess(SimpleTestCase):
    def test_each_source_grants(self):
        from People.compliance_common import compliance_allows
        self.assertTrue(compliance_allows(make_user(can_edit_compliance=True), "edit"))
        self.assertTrue(compliance_allows(make_user(can_edit_payroll=True), "edit"))
        self.assertTrue(compliance_allows(make_user(can_edit_employees=True), "edit"))
        self.assertFalse(compliance_allows(make_user(), "edit"))


class RoleAllowsGeneric(SimpleTestCase):
    def test_manage_flag_covers_all_actions_for_every_new_module(self):
        for m in NEW_MODULES:
            role = EmployeeRole(role_name="T", role_code="t")
            setattr(role, f"can_manage_{m}", True)
            for a in ("view", "create", "edit", "delete"):
                self.assertTrue(role_allows(role, m, a), f"{m}.{a}")
