/**
 * Staff Authentication Utilities Tests
 */

import { hasRole, canPerformAction } from "@/lib/staff-auth";

describe("Staff Authentication Utilities", () => {
  describe("hasRole", () => {
    it("should return true for exact role match", () => {
      const user = { role: "STAFF" };
      expect(hasRole(user, "STAFF")).toBe(true);
    });

    it("should return true for higher role", () => {
      const adminUser = { role: "ADMIN" };
      expect(hasRole(adminUser, "STAFF")).toBe(true);

      const superAdmin = { role: "SUPER_ADMIN" };
      expect(hasRole(superAdmin, "STAFF")).toBe(true);
      expect(hasRole(superAdmin, "ADMIN")).toBe(true);
    });

    it("should return false for lower role", () => {
      const staffUser = { role: "STAFF" };
      expect(hasRole(staffUser, "ADMIN")).toBe(false);
      expect(hasRole(staffUser, "SUPER_ADMIN")).toBe(false);
    });

    it("should return false for customer role", () => {
      const customer = { role: "CUSTOMER" };
      expect(hasRole(customer, "STAFF")).toBe(false);
      expect(hasRole(customer, "ADMIN")).toBe(false);
      expect(hasRole(customer, "SUPER_ADMIN")).toBe(false);
    });
  });

  describe("canPerformAction", () => {
    it("STAFF can view demos", () => {
      const staff = { role: "STAFF" };
      expect(canPerformAction(staff, "view_demos")).toBe(true);
    });

    it("STAFF cannot export data", () => {
      const staff = { role: "STAFF" };
      expect(canPerformAction(staff, "export_data")).toBe(false);
    });

    it("STAFF cannot manage other staff", () => {
      const staff = { role: "STAFF" };
      expect(canPerformAction(staff, "manage_staff")).toBe(false);
    });

    it("ADMIN can export data", () => {
      const admin = { role: "ADMIN" };
      expect(canPerformAction(admin, "export_data")).toBe(true);
    });

    it("ADMIN cannot manage staff", () => {
      const admin = { role: "ADMIN" };
      expect(canPerformAction(admin, "manage_staff")).toBe(false);
    });

    it("SUPER_ADMIN can do everything", () => {
      const superAdmin = { role: "SUPER_ADMIN" };

      const allActions = [
        "view_demos",
        "view_contacts",
        "export_data",
        "manage_users",
        "manage_staff",
        "system_settings",
        "delete_records",
      ];

      allActions.forEach((action) => {
        expect(canPerformAction(superAdmin, action)).toBe(true);
      });
    });

    it("CUSTOMER cannot perform any staff actions", () => {
      const customer = { role: "CUSTOMER" };

      const staffActions = [
        "view_demos",
        "view_contacts",
        "view_users",
        "view_analytics",
        "export_data",
      ];

      staffActions.forEach((action) => {
        expect(canPerformAction(customer, action)).toBe(false);
      });
    });

    it("should handle unknown actions", () => {
      const staff = { role: "STAFF" };
      expect(canPerformAction(staff, "unknown_action")).toBe(false);
    });
  });

  describe("Role Hierarchy", () => {
    it("should enforce correct role hierarchy", () => {
      const roles = [
        { role: "CUSTOMER", level: 0 },
        { role: "STAFF", level: 1 },
        { role: "ADMIN", level: 2 },
        { role: "SUPER_ADMIN", level: 3 },
      ];

      roles.forEach((roleA) => {
        roles.forEach((roleB) => {
          const result = hasRole(roleA, roleB.role as any);
          const expected = roleA.level >= roleB.level;
          expect(result).toBe(expected);
        });
      });
    });
  });
});
