import { render, screen } from '@testing-library/svelte';
import { describe, expect, it } from 'vitest';
import { aboutPageContent } from '$lib/content/about';
import { dogWalkingContent } from '$lib/content/dog-walking';
import { ourPricingContent } from '$lib/content/our-pricing';
import { packWalksContent } from '$lib/content/pack-walks';
import { puppyVisitsContent } from '$lib/content/puppy-visits';
import { staticPages } from '$lib/content/static-pages';
import SlugPage from './+page.svelte';
import { createStaticRouteData } from '../../test/fixtures';

describe('static slug route page', () => {
  it.each([
    ['pack-walks', packWalksContent.hero.title],
    ['dog-walking', dogWalkingContent.hero.title],
    ['puppy-visits', puppyVisitsContent.hero.title],
    ['our-pricing', ourPricingContent.subtitle],
    ['about', aboutPageContent.sections[0].title],
    ['contact-us', "Let's meet!"],
    ['terms-and-conditions', '1. Application of Terms'],
    ['privacy-policy', 'How we collect your information']
  ] as const)('renders the %s page branch', (slug, expectedText) => {
    render(SlugPage, {
      data: createStaticRouteData(slug)
    });

    if (slug === 'contact-us') {
      expect(screen.getByRole('heading', { name: expectedText })).toBeInTheDocument();
      return;
    }

    expect(screen.getByText(expectedText)).toBeInTheDocument();
  });

  it('sets SEO metadata for rendered slug pages', () => {
    render(SlugPage, {
      data: createStaticRouteData('about')
    });

    expect(document.title).toBe(staticPages.about.title);
    expect(document.head.innerHTML).toContain('AboutPage');
  });

  it.each(['terms-and-conditions', 'privacy-policy'] as const)(
    'does not render booking or testimonial sections on %s',
    (slug) => {
      render(SlugPage, {
        data: createStaticRouteData(slug)
      });

      expect(screen.queryByText("Let's meet!")).not.toBeInTheDocument();
      expect(screen.queryByText('What our clients say')).not.toBeInTheDocument();
    }
  );

  it('shows the general enquiry option on the contact page only', () => {
    const { rerender } = render(SlugPage, {
      data: {
        ...createStaticRouteData('contact-us'),
        generalEnquiryEnabled: true
      }
    });

    expect(screen.getByLabelText(/General enquiry/i)).toBeInTheDocument();

    rerender({
      data: createStaticRouteData('pack-walks')
    });

    expect(screen.queryByLabelText(/General enquiry/i)).not.toBeInTheDocument();
  });

  it('renders the shared FAQ section on the contact page', () => {
    render(SlugPage, {
      data: createStaticRouteData('contact-us')
    });

    expect(screen.getByText('FAQs')).toBeInTheDocument();
    expect(screen.getByText('Can any dog use your service?')).toBeInTheDocument();
  });
});
