// tests/unit/routes/employes.route.spec.ts
import { beforeEach, describe, expect, it, vi } from 'vitest';

const { requireAuthMock, getEmployesFeedMock, createEmployeFeedMock } = vi.hoisted(() => ({
  requireAuthMock: vi.fn(),
  getEmployesFeedMock: vi.fn(),
  createEmployeFeedMock: vi.fn(),
}));

vi.mock('../../../../src/lib/requireAuth', () => ({
  requireAuth: requireAuthMock,
}));

vi.mock('../../../../src/server/employes.server', () => ({
  getEmployesFeed: getEmployesFeedMock,
  createEmployeFeed: createEmployeFeedMock,
}));

import { GET, POST } from '../../../../src/app/api/employes/route';

describe('Unit — /api/employes route', () => {
  beforeEach(() => {
    vi.resetAllMocks();
    requireAuthMock.mockReturnValue(null);
  });

  it('GET returns 200 and default pagination values', async () => {
    getEmployesFeedMock.mockResolvedValue({
      items: [{ COS: 1, PRE: 'Alice' }],
      count: 1,
    });

    const res = await GET(new Request('http://localhost/api/employes'));

    expect(res.status).toBe(200);
    expect(getEmployesFeedMock).toHaveBeenCalledWith(20, 0);

    const body = await res.json();
    expect(body).toEqual({
      message: 'Employes retrieved successfully',
      items: [{ COS: 1, PRE: 'Alice' }],
      count: 1,
    });
  });

  it('GET returns 400 when query params are invalid', async () => {
    const res = await GET(new Request('http://localhost/api/employes?limit=0&offset=-1'));

    expect(res.status).toBe(400);
    expect(getEmployesFeedMock).not.toHaveBeenCalled();

    const body = await res.json();
    expect(body.message).toBe('Invalid query parameters');
  });

  it('GET returns auth error when request is unauthorized', async () => {
    requireAuthMock.mockReturnValue(new Response('Unauthorized', { status: 401 }));

    const res = await GET(new Request('http://localhost/api/employes'));

    expect(res.status).toBe(401);
    expect(getEmployesFeedMock).not.toHaveBeenCalled();
  });

  it('POST returns 201 when body is valid', async () => {
    createEmployeFeedMock.mockResolvedValue({
      employe: { COS: 123, PRE: 'Alice' },
    });

    const res = await POST(
      new Request('http://localhost/api/employes', {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({ COS: 123, PRE: 'Alice' }),
      }),
    );

    expect(res.status).toBe(201);
    expect(createEmployeFeedMock).toHaveBeenCalledWith({ COS: 123, PRE: 'Alice' });

    const body = await res.json();
    expect(body).toEqual({
      message: 'Employe created successfully',
      employe: { COS: 123, PRE: 'Alice' },
    });
  });

  it('POST returns 400 when JSON is malformed', async () => {
    const res = await POST(
      new Request('http://localhost/api/employes', {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: '{"COS": 1',
      }),
    );

    expect(res.status).toBe(400);
    expect(createEmployeFeedMock).not.toHaveBeenCalled();

    const body = await res.json();
    expect(body.message).toBe('Invalid JSON body');
  });

  it('POST returns 400 when body validation fails', async () => {
    const res = await POST(
      new Request('http://localhost/api/employes', {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({ PRE: 'Missing COS' }),
      }),
    );

    expect(res.status).toBe(400);
    expect(createEmployeFeedMock).not.toHaveBeenCalled();

    const body = await res.json();
    expect(body.message).toBe('Invalid body parameters');
  });
  it('Post returns auth error when request is unauthorized', async () => {
    requireAuthMock.mockReturnValue(new Response('Unauthorized', { status: 401 }));

    const res = await POST(new Request('http://localhost/api/employes'));

    expect(res.status).toBe(401);
    expect(getEmployesFeedMock).not.toHaveBeenCalled();
  });
});