import { beforeEach, describe, expect, it, vi } from 'vitest';
import { staticPages } from '$lib/content/static-pages';
import { sharedPageContent } from '../../test/fixtures';

const { getSharedPageContent } = vi.hoisted(() => ({
  getSharedPageContent: vi.fn()
}));

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

import { load } from './+page.server';

describe('static slug page server load', () => {
  beforeEach(() => {
    getSharedPageContent.mockReset();
    vi.unstubAllEnvs();
  });

  it('redirects the legacy about-us slug to /about', async () => {
    await expect(load({ params: { slug: 'about-us' } } as never)).rejects.toMatchObject({
      status: 301,
      location: '/about'
    });
  });

  it('redirects the legacy booking slug to /contact-us', async () => {
    await expect(load({ params: { slug: 'booking' } } as never)).rejects.toMatchObject({
      status: 301,
      location: '/contact-us'
    });
  });

  it('redirects the legacy home-stays slug to the homepage', async () => {
    await expect(load({ params: { slug: 'home-stays' } } as never)).rejects.toMatchObject({
      status: 301,
      location: '/'
    });
  });

  it('throws a 404 for unknown slugs', async () => {
    await expect(load({ params: { slug: 'missing-page' } } as never)).rejects.toMatchObject({
      status: 404
    });
  });

  it('returns the shared content and page metadata for valid static routes', async () => {
    getSharedPageContent.mockResolvedValue(sharedPageContent);

    await expect(load({ params: { slug: 'pack-walks' } } as never)).resolves.toEqual({
      content: sharedPageContent,
      generalEnquiryEnabled: false,
      page: staticPages['pack-walks'],
      slug: 'pack-walks'
    });
  });

  it('keeps general enquiries disabled on contact-us by default', async () => {
    getSharedPageContent.mockResolvedValue(sharedPageContent);

    await expect(load({ params: { slug: 'contact-us' } } as never)).resolves.toEqual({
      content: sharedPageContent,
      generalEnquiryEnabled: false,
      page: {
        ...staticPages['contact-us'],
        description: 'Book a Meet & Greet with Goodwalk Auckland dog walking services.'
      },
      slug: 'contact-us'
    });
  });

  it('enables general enquiries on contact-us when the env flag is turned on', async () => {
    vi.stubEnv('ENABLE_GENERAL_ENQUIRIES', 'enabled');
    getSharedPageContent.mockResolvedValue(sharedPageContent);

    await expect(load({ params: { slug: 'contact-us' } } as never)).resolves.toEqual({
      content: sharedPageContent,
      generalEnquiryEnabled: true,
      page: staticPages['contact-us'],
      slug: 'contact-us'
    });
  });
});
