import { beforeEach, describe, expect, it, vi } from 'vitest';
import { homepageContent } from '$lib/content/homepage';

const { getHomepageContent, saveHomepageContent } = vi.hoisted(() => ({
  getHomepageContent: vi.fn(),
  saveHomepageContent: vi.fn()
}));

vi.mock('$lib/server/content', () => ({
  getHomepageContent,
  saveHomepageContent
}));

import { GET, PUT } from './+server';

describe('homepage content endpoint', () => {
  beforeEach(() => {
    getHomepageContent.mockReset();
    saveHomepageContent.mockReset();
  });

  it('returns homepage content for GET requests', async () => {
    getHomepageContent.mockResolvedValue(homepageContent);

    const response = await GET();

    expect(response.status).toBe(200);
    await expect(response.json()).resolves.toEqual(homepageContent);
  });

  it('saves homepage content for PUT requests', async () => {
    const updatedContent = {
      ...homepageContent,
      seo: {
        ...homepageContent.seo,
        title: 'Updated home title'
      }
    };

    saveHomepageContent.mockResolvedValue(updatedContent);

    const response = await PUT({
      request: new Request('https://www.goodwalk.co.nz/api/content/homepage', {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(updatedContent)
      })
    } as never);

    expect(saveHomepageContent).toHaveBeenCalledWith(updatedContent);
    await expect(response.json()).resolves.toEqual(updatedContent);
  });
});
