/**
 * Import Validation Tests for Careers Dashboard
 * Prevents missing import errors like the Briefcase icon issue
 */

import { describe, it, expect } from "@jest/globals";
import * as fs from "fs";
import * as path from "path";

describe("Careers Dashboard - Import Validation", () => {
  const careersComponentsDir = path.join(
    process.cwd(),
    "app/staff/dashboard/careers/components",
  );

  describe("JobsList Component", () => {
    const filePath = path.join(careersComponentsDir, "JobsList.tsx");
    const fileContent = fs.readFileSync(filePath, "utf-8");

    it("should import all lucide-react icons that are used", () => {
      // Extract import statement
      const importMatch = fileContent.match(
        /import\s+{([^}]+)}\s+from\s+['"]lucide-react['"]/,
      );
      expect(importMatch).toBeTruthy();

      const importedIcons = importMatch![1]
        .split(",")
        .map((icon) => icon.trim())
        .filter(Boolean);

      // Icons that should be imported
      const requiredIcons = [
        "Search",
        "Edit",
        "Trash2",
        "Eye",
        "Calendar",
        "MapPin",
        "Briefcase",
      ];

      requiredIcons.forEach((icon) => {
        expect(importedIcons).toContain(icon);
      });
    });

    it("should not use icons that are not imported", () => {
      // Check for JSX usage of icons not in imports
      const importMatch = fileContent.match(
        /import\s+{([^}]+)}\s+from\s+['"]lucide-react['"]/,
      );
      const importedIcons = importMatch![1]
        .split(",")
        .map((icon) => icon.trim())
        .filter(Boolean);

      // Find all potential icon usages like <IconName or {IconName
      const iconUsagePattern = /<([A-Z][a-zA-Z]+)\s|{([A-Z][a-zA-Z]+)}/g;
      const matches = fileContent.matchAll(iconUsagePattern);

      const usedComponents = new Set<string>();
      for (const match of matches) {
        const componentName = match[1] || match[2];
        // Filter for likely lucide-react icons (capital start, likely icon names)
        if (
          componentName &&
          !componentName.startsWith("Edit") &&
          !componentName.includes("Modal") &&
          !componentName.includes("Job")
        ) {
          usedComponents.add(componentName);
        }
      }

      // All icons used should be imported
      const lucideIcons = [
        "Briefcase",
        "Search",
        "Edit",
        "Trash2",
        "Eye",
        "Calendar",
        "MapPin",
      ];
      lucideIcons.forEach((icon) => {
        if (fileContent.includes(`<${icon}`)) {
          expect(importedIcons).toContain(icon);
        }
      });
    });

    it("should have Briefcase icon imported (regression test)", () => {
      // Briefcase may be on a different line from the import keyword (multiline import)
      expect(fileContent).toContain("Briefcase");
      expect(fileContent).toContain("lucide-react");
    });
  });

  describe("ApplicationsList Component", () => {
    const filePath = path.join(careersComponentsDir, "ApplicationsList.tsx");
    const fileContent = fs.readFileSync(filePath, "utf-8");

    it("should import all required lucide-react icons", () => {
      const importMatch = fileContent.match(
        /import\s+{([^}]+)}\s+from\s+['"]lucide-react['"]/,
      );
      expect(importMatch).toBeTruthy();

      const importedIcons = importMatch![1]
        .split(",")
        .map((icon) => icon.trim())
        .filter(Boolean);

      // Icons used in ApplicationsList
      const requiredIcons = [
        "Search",
        "Download",
        "Eye",
        "Calendar",
        "Mail",
        "Phone",
        "ExternalLink",
      ];

      requiredIcons.forEach((icon) => {
        if (fileContent.includes(`<${icon}`)) {
          expect(importedIcons).toContain(icon);
        }
      });
    });
  });

  describe("CreateJobModal Component", () => {
    const filePath = path.join(careersComponentsDir, "CreateJobModal.tsx");
    const fileContent = fs.readFileSync(filePath, "utf-8");

    it("should import all required lucide-react icons", () => {
      const importMatch = fileContent.match(
        /import\s+{([^}]+)}\s+from\s+['"]lucide-react['"]/,
      );
      expect(importMatch).toBeTruthy();

      const importedIcons = importMatch![1]
        .split(",")
        .map((icon) => icon.trim())
        .filter(Boolean);

      // Icons used in CreateJobModal
      const requiredIcons = ["X", "Save", "FileText"];

      requiredIcons.forEach((icon) => {
        if (fileContent.includes(`<${icon}`)) {
          expect(importedIcons).toContain(icon);
        }
      });
    });
  });

  describe("ApplicationDetailModal Component", () => {
    const filePath = path.join(
      careersComponentsDir,
      "ApplicationDetailModal.tsx",
    );
    const fileContent = fs.readFileSync(filePath, "utf-8");

    it("should import all required lucide-react icons", () => {
      const importMatch = fileContent.match(
        /import\s+{([^}]+)}\s+from\s+['"]lucide-react['"]/,
      );
      expect(importMatch).toBeTruthy();

      const importedIcons = importMatch![1]
        .split(",")
        .map((icon) => icon.trim())
        .filter(Boolean);

      // Icons used in ApplicationDetailModal
      const requiredIcons = [
        "X",
        "Download",
        "Mail",
        "Phone",
        "ExternalLink",
        "Calendar",
        "Save",
        "AlertCircle",
      ];

      requiredIcons.forEach((icon) => {
        if (fileContent.includes(`<${icon}`)) {
          expect(importedIcons).toContain(icon);
        }
      });
    });
  });

  describe("API Routes - Auth Import Validation", () => {
    it("should use correct auth pattern in /api/staff/jobs/route.ts", () => {
      const filePath = path.join(process.cwd(), "app/api/staff/jobs/route.ts");
      const fileContent = fs.readFileSync(filePath, "utf-8");

      // Should import getStaffSession, not getServerSession
      expect(fileContent).toMatch(/import.*getStaffSession.*from.*staff-auth/);
      expect(fileContent).not.toMatch(
        /import.*getServerSession.*from.*next-auth/,
      );
      expect(fileContent).not.toMatch(/import.*authOptions/);
    });

    it("should use correct auth pattern in /api/staff/applications/route.ts", () => {
      const filePath = path.join(
        process.cwd(),
        "app/api/staff/applications/route.ts",
      );
      const fileContent = fs.readFileSync(filePath, "utf-8");

      expect(fileContent).toMatch(/import.*getStaffSession.*from.*staff-auth/);
      expect(fileContent).not.toMatch(
        /import.*getServerSession.*from.*next-auth/,
      );
    });

    it("should use correct auth pattern in download-cv route", () => {
      const filePath = path.join(
        process.cwd(),
        "app/api/staff/applications/[id]/download-cv/route.ts",
      );
      const fileContent = fs.readFileSync(filePath, "utf-8");

      expect(fileContent).toMatch(/import.*getStaffSession.*from.*staff-auth/);
      expect(fileContent).not.toMatch(
        /import.*getServerSession.*from.*next-auth/,
      );
    });
  });
});
