import { NextResponse } from 'next/server';
import { GET } from '@/app/api/tech-stack/route';
import { getTechStack } from '@/lib/directus';

jest.mock('@/lib/directus', () => ({
  getTechStack: jest.fn(),
}));

jest.mock('next/server', () => ({
  NextRequest: jest.fn((url) => ({
    url,
  })),
  NextResponse: {
    json: jest.fn((data, options) => ({
      json: async () => data,
      status: options?.status || 200,
    })),
  },
}));

describe('GET /api/tech-stack', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('should return tech stack from Directus', async () => {
    const mockTechStack = [
      {
        id: '1',
        key: 'frontend',
        icon: 'Globe',
        name: 'Frontend',
        items: [
          { id: '1-1', name: 'React' }
        ],
      },
    ];

    (getTechStack as jest.Mock).mockResolvedValue(mockTechStack);

    const request = {
      url: 'http://localhost/api/tech-stack?locale=en',
    } as any;

    await GET(request);

    expect(NextResponse.json).toHaveBeenCalledWith(
      expect.objectContaining({
        techStack: mockTechStack,
        source: 'directus',
      })
    );
  });

  it('should return fallback when no tech stack found', async () => {
    (getTechStack as jest.Mock).mockResolvedValue(null);

    const request = {
      url: 'http://localhost/api/tech-stack?locale=en',
    } as any;

    await GET(request);

    expect(NextResponse.json).toHaveBeenCalledWith(
      expect.objectContaining({
        techStack: null,
        source: 'fallback',
      })
    );
  });
});
