import { beforeEach, describe, expect, it, vi } from 'vitest';

const { requireAuthMock, listSecteurMock, createSecteurMock } = vi.hoisted(() => ({
  requireAuthMock: vi.fn(),
  listSecteurMock: vi.fn(),
  createSecteurMock: vi.fn(),
}));

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

vi.mock('@/services/reference-lookups.service', () => ({
  listSecteurs: listSecteurMock,
  createSecteur: createSecteurMock,
}));

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

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

  it('GET returns the paginated secteurlist', async () => {
    listSecteurMock.mockResolvedValue([
      {
        id: 1,
        secteur: 'secteurautorisee',
        libsecteur: 'AA',
        date_creation: '2026-05-21T08:16:08.000Z',
      },
    ]);

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

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

    const body = await res.json();
    expect(body).toEqual({
      message: 'Secteurs retrieved successfully',
      items: [
        {
          id: 1,
          secteur: 'secteurautorisee',
          libsecteur: 'AA',
          date_creation: '2026-05-21T08:16:08.000Z',
        },
      ],
      count: 1,
    });
  });

  it('GET returns 400 for invalid query parameters', async () => {
    const res = await GET(new Request('http://localhost/api/secteurs?limit=abc'));

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

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

  it('POST creates a Secteur', async () => {
    createSecteurMock.mockResolvedValue({
      id: 1,
      tse : "tse"
    });

    const res = await POST(
      new Request('http://localhost/apisecteurs', {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({
          tse : "tse"
        }),
      }),
    );

    expect(res.status).toBe(201);
    expect(createSecteurMock).toHaveBeenCalledWith({
      tse : "tse"
    });

    const body = await res.json();
    expect(body).toEqual({
      message: 'Secteur created successfully',
      secteur: {
        id: 1,
        tse : "tse"
      },
    });
  });

  it('POST returns 400 for invalid JSON', async () => {
    const res = await POST(
      new Request('http://localhost/apisecteurs', {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: '{"Secteur": "secteurautorisee"',
      }),
    );

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

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

  it('POST returns 400 for invalid body', async () => {
    const res = await POST(
      new Request('http://localhost/apisecteurs', {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({ libsecteur: 'Missing Secteur' }),
      }),
    );

    expect(res.status).toBe(400);
    expect(createSecteurMock).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/type-secteur'));
  
      expect(res.status).toBe(401);
      expect(createSecteurMock).not.toHaveBeenCalled();
    });
});