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

const { requireAuthMock, listClassificationMock, createClassificationMock } = vi.hoisted(() => ({
  requireAuthMock: vi.fn(),
  listClassificationMock: vi.fn(),
  createClassificationMock: vi.fn(),
}));

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

vi.mock('@/services/reference-lookups.service', () => ({
  listClassifications: listClassificationMock,
  createClassification: createClassificationMock,
}));

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

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

  it('GET returns the paginated classificationlist', async () => {
    listClassificationMock.mockResolvedValue([
      {
        id: 1,
        classification: 'classificationautorisee',
        libclassification: 'AA',
        date_creation: '2026-05-21T08:16:08.000Z',
      },
    ]);

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

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

    const body = await res.json();
    expect(body).toEqual({
      message: 'Classifications retrieved successfully',
      items: [
        {
          id: 1,
          classification: 'classificationautorisee',
          libclassification: '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/classifications?limit=abc'));

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

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

  it('POST creates a Classification', async () => {
    createClassificationMock.mockResolvedValue({
      id: 1,
      id_classification: 1,
      classification: "test",
      niveau: "fort",
      echelon: "haut",
      coefficient: 1,
      actif: true,
      etablissement: "ici",
    });

    const res = await POST(
      new Request('http://localhost/api/classifications', {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({
          id_classification: 1,
          classification: "test",
          niveau: "fort",
          echelon: "haut",
          coefficient: 1,
          actif: true,
          etablissement: "ici",
        }),
      }),
    );

    expect(res.status).toBe(201);
    expect(createClassificationMock).toHaveBeenCalledWith({
      id_classification: 1,
      classification: "test",
      niveau: "fort",
      echelon: "haut",
      coefficient: 1,
      actif: true,
      etablissement: "ici",
    });

    const body = await res.json();
    expect(body).toEqual({
      message: 'Classification created successfully',
      classification: {
        id: 1,
        id_classification: 1,
        classification: "test",
        niveau: "fort",
        echelon: "haut",
        coefficient: 1,
        actif: true,
        etablissement: "ici",
      },
    });
  });

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

    expect(res.status).toBe(400);
    expect(createClassificationMock).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/api/classifications', {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({ libclassification: 'Missing Classification' }),
      }),
    );

    expect(res.status).toBe(400);
    expect(createClassificationMock).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/classifications'));

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