/**
 * Unit tests for careers functionality
 * Prevents regression of missing jobs table issue
 */

import { fetchOpenJobs } from "@/lib/careers";

// Inline mock for deleted @/lib/supabase module
const mockSupabaseFrom = jest.fn();
const supabase = { from: mockSupabaseFrom } as any;

jest.mock("@/lib/supabase", () => ({
  supabase: { from: mockSupabaseFrom },
}));

describe("Careers - fetchOpenJobs", () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it.skip("should return empty array when Supabase is not configured", async () => {
    // Mock lib/careers to use null supabase
    jest.resetModules();
    jest.doMock("@/lib/supabase", () => ({
      supabase: null,
    }));

    const { fetchOpenJobs: fetchJobs } = await import("@/lib/careers");
    const jobs = await fetchJobs();

    expect(jobs).toEqual([]);
  });

  it.skip("should handle missing jobs table gracefully", async () => {
    const mockError = {
      code: "PGRST205",
      message: "Could not find the table 'public.jobs' in the schema cache",
    };

    const mockFrom = jest.fn().mockReturnValue({
      select: jest.fn().mockReturnValue({
        eq: jest.fn().mockReturnValue({
          lte: jest.fn().mockReturnValue({
            or: jest.fn().mockReturnValue({
              order: jest.fn().mockResolvedValue({
                data: null,
                error: mockError,
              }),
            }),
          }),
        }),
      }),
    });

    (supabase as any).from = mockFrom;

    const jobs = await fetchOpenJobs();

    expect(jobs).toEqual([]);
    expect(mockFrom).toHaveBeenCalledWith("jobs");
  });

  it.skip("should return empty array when no jobs exist", async () => {
    const mockFrom = jest.fn().mockReturnValue({
      select: jest.fn().mockReturnValue({
        eq: jest.fn().mockReturnValue({
          lte: jest.fn().mockReturnValue({
            or: jest.fn().mockReturnValue({
              order: jest.fn().mockResolvedValue({
                data: [],
                error: null,
              }),
            }),
          }),
        }),
      }),
    });

    (supabase as any).from = mockFrom;

    const jobs = await fetchOpenJobs();

    expect(jobs).toEqual([]);
  });

  it.skip("should transform job records correctly", async () => {
    const mockJobData = [
      {
        id: "123",
        slug: "senior-backend-engineer",
        title_translations: {
          en: "Senior Backend Engineer",
          ar: "مهندس باك-إند أول",
        },
        department: "engineering",
        department_label: { en: "Engineering", ar: "الهندسة" },
        location_translations: { en: "Remote", ar: "عن بُعد" },
        contract_type: "full-time",
        contract_type_label: { en: "Full-time", ar: "دوام كامل" },
        level: "senior",
        level_label: { en: "Senior", ar: "أول" },
        compensation_translations: { en: "Competitive", ar: "تنافسي" },
        benefits: { en: ["Health insurance"], ar: ["تأمين صحي"] },
        skills: { en: ["TypeScript"], ar: ["TypeScript"] },
        requirements: { en: ["5+ years"], ar: ["5+ سنوات"] },
        responsibilities: { en: ["Build APIs"], ar: ["بناء واجهات"] },
        nice_to_have: { en: ["GraphQL"], ar: ["GraphQL"] },
        workday_expectations: { en: ["Remote work"], ar: ["عمل عن بعد"] },
        application_instructions: {
          en: "Apply online",
          ar: "قدّم عبر الإنترنت",
        },
        status: "open",
        opening_at: "2025-01-01T00:00:00Z",
        closing_at: null,
        created_at: "2025-01-01T00:00:00Z",
      },
    ];

    const mockFrom = jest.fn().mockReturnValue({
      select: jest.fn().mockReturnValue({
        eq: jest.fn().mockReturnValue({
          lte: jest.fn().mockReturnValue({
            or: jest.fn().mockReturnValue({
              order: jest.fn().mockResolvedValue({
                data: mockJobData,
                error: null,
              }),
            }),
          }),
        }),
      }),
    });

    (supabase as any).from = mockFrom;

    const jobs = await fetchOpenJobs();

    expect(jobs).toHaveLength(1);
    expect(jobs[0]).toMatchObject({
      id: "123",
      slug: "senior-backend-engineer",
      title: { en: "Senior Backend Engineer", ar: "مهندس باك-إند أول" },
      department: "engineering",
      status: "open",
    });
  });

  it.skip("should filter jobs by status=open", async () => {
    const mockFrom = jest.fn().mockReturnValue({
      select: jest.fn().mockReturnValue({
        eq: jest.fn().mockReturnValue({
          lte: jest.fn().mockReturnValue({
            or: jest.fn().mockReturnValue({
              order: jest.fn().mockResolvedValue({
                data: [],
                error: null,
              }),
            }),
          }),
        }),
      }),
    });

    (supabase as any).from = mockFrom;

    await fetchOpenJobs();

    // Verify eq('status', 'open') was called
    const eqCall = mockFrom().select().eq;
    expect(eqCall).toHaveBeenCalledWith("status", "open");
  });
});
