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

vi.mock('$lib/server/db', () => ({
  getPool: vi.fn()
}));

import { getPool } from '$lib/server/db';
import { getHomepageContent, getSharedPageContent, saveHomepageContent } from './content';

describe('content server helpers', () => {
  beforeEach(() => {
    vi.mocked(getPool).mockReset();
  });

  it('returns a cloned homepage fallback when the database is disabled', async () => {
    vi.mocked(getPool).mockReturnValue(null);

    const result = await getHomepageContent();

    expect(result).toEqual(homepageContent);
    expect(result).not.toBe(homepageContent);
  });

  it('returns homepage content from the database when present', async () => {
    const dbContent = {
      ...homepageContent,
      seo: {
        ...homepageContent.seo,
        title: 'Updated title'
      }
    };

    vi.mocked(getPool).mockReturnValue({
      query: vi.fn().mockResolvedValue({
        rowCount: 1,
        rows: [{ value: dbContent }]
      })
    } as never);

    await expect(getHomepageContent()).resolves.toEqual(dbContent);
  });

  it('falls back to file content when the database query fails', async () => {
    vi.mocked(getPool).mockReturnValue({
      query: vi.fn().mockRejectedValue(new Error('db unavailable'))
    } as never);

    await expect(getHomepageContent()).resolves.toEqual(homepageContent);
  });

  it('persists homepage content when the database is enabled', async () => {
    const query = vi.fn().mockResolvedValue({});
    vi.mocked(getPool).mockReturnValue({ query } as never);

    const result = await saveHomepageContent(homepageContent);

    expect(result).toEqual(homepageContent);
    expect(query).toHaveBeenCalledWith(expect.stringContaining('insert into site_content'), [
      'homepage',
      JSON.stringify(homepageContent)
    ]);
  });

  it('throws when trying to save content without a database connection', async () => {
    vi.mocked(getPool).mockReturnValue(null);

    await expect(saveHomepageContent(homepageContent)).rejects.toThrow('DATABASE_URL is not configured.');
  });

  it('returns only shared page content for secondary pages', async () => {
    vi.mocked(getPool).mockReturnValue(null);

    await expect(getSharedPageContent()).resolves.toEqual({
      navigation: homepageContent.navigation,
      services: homepageContent.services,
      testimonials: homepageContent.testimonials,
      booking: homepageContent.booking,
      info: homepageContent.info,
      footer: homepageContent.footer
    });
  });

  it('returns cloned shared page sections instead of original references', async () => {
    vi.mocked(getPool).mockReturnValue(null);

    const result = await getSharedPageContent();

    expect(result.navigation).not.toBe(homepageContent.navigation);
    expect(result.services).not.toBe(homepageContent.services);
    expect(result.testimonials).not.toBe(homepageContent.testimonials);
    expect(result.booking).not.toBe(homepageContent.booking);
    expect(result.info).not.toBe(homepageContent.info);
    expect(result.footer).not.toBe(homepageContent.footer);
  });
});
