import { describe, expect, it, vi } from 'vitest';
import { createServerSiteClient } from '../server-site.js';
function makeDb(overrides = {}) {
    return {
        document: {
            findFirst: vi.fn(async () => null),
            ...overrides.document,
        },
        media: {
            findUnique: vi.fn(async () => null),
            ...overrides.media,
        },
    };
}
describe('createServerSiteClient', () => {
    it('throws if db is missing', () => {
        expect(() => createServerSiteClient(undefined, {})).toThrow(/Prisma-compatible/);
    });
    it('reads global data directly from the database', async () => {
        const findFirst = vi.fn(async () => ({
            id: 'g-1',
            collection: 'site-settings',
            data: { brand: 'Actuate' },
            deletedAt: null,
        }));
        const db = makeDb({ document: { findFirst } });
        const cms = createServerSiteClient(db, {
            globals: { 'site-settings': { access: { read: () => true } } },
        });
        const settings = await cms.getGlobal('site-settings');
        expect(settings).toEqual({ brand: 'Actuate' });
        expect(findFirst).toHaveBeenCalledWith({
            where: { collection: 'site-settings', deletedAt: null },
        });
    });
    it('defaults to public read when no access policy is set', async () => {
        const findFirst = vi.fn(async () => ({
            id: 'g-1',
            collection: 'site-settings',
            data: { brand: 'Actuate' },
            deletedAt: null,
        }));
        const db = makeDb({ document: { findFirst } });
        const cms = createServerSiteClient(db, { globals: { 'site-settings': {} } });
        const settings = await cms.getGlobal('site-settings');
        expect(settings).toEqual({ brand: 'Actuate' });
    });
    it('returns null when access.read denies', async () => {
        const findFirst = vi.fn(async () => ({
            id: 'g-1',
            collection: 'site-settings',
            data: { brand: 'Secret' },
            deletedAt: null,
        }));
        const db = makeDb({ document: { findFirst } });
        const cms = createServerSiteClient(db, {
            globals: { 'site-settings': { access: { read: () => false } } },
        });
        const settings = await cms.getGlobal('site-settings');
        expect(settings).toBeNull();
    });
    it('returns null for missing global rows', async () => {
        const db = makeDb();
        const cms = createServerSiteClient(db, {});
        expect(await cms.getGlobal('settings')).toBeNull();
    });
    it('resolves the home document for the root path', async () => {
        const findFirst = vi.fn(async () => ({
            id: 'p-1',
            collection: 'pages',
            slug: 'home',
            status: 'PUBLISHED',
            publishedAt: new Date('2025-01-01T00:00:00Z'),
            structuredData: null,
            data: { slug: 'home', title: 'Home', _layout: { hero: { id: 'v-1' } } },
        }));
        const db = makeDb({ document: { findFirst } });
        const cms = createServerSiteClient(db, {
            collections: { pages: { slug: 'pages', type: 'page' } },
        });
        const result = await cms.resolveDocument('/');
        expect(result?.data.id).toBe('p-1');
        // _layout must be stripped from data and exposed top-level
        expect(result?.data.data).toEqual({ slug: 'home', title: 'Home' });
        expect(result?.layout).toEqual({ hero: { id: 'v-1' } });
        // findFirst should target PUBLISHED + non-deleted
        const firstCall = findFirst.mock.calls[0];
        const args = firstCall[0];
        expect(args.where.status).toBe('PUBLISHED');
        expect(args.where.deletedAt).toBeNull();
    });
    it('returns null when no document matches the resolved path', async () => {
        const db = makeDb();
        const cms = createServerSiteClient(db, {
            collections: { pages: { slug: 'pages', type: 'page' } },
        });
        expect(await cms.resolveDocument('/non-existent')).toBeNull();
    });
    it('getNavigation returns a clean tree and strips internal ids', async () => {
        const findFirst = vi.fn(async () => ({
            id: 'nav-1',
            collection: 'navigations',
            title: 'Main',
            data: {
                name: 'Main Menu',
                slug: 'main',
                location: 'header',
                items: [
                    { id: 'a', label: 'Home', url: '/' },
                    { id: 'b', label: '', url: '/skip' },
                    {
                        id: 'c',
                        label: 'Shop',
                        url: '/shop',
                        children: [{ id: 'd', label: 'Widgets', url: '/shop/widgets' }],
                    },
                ],
            },
            deletedAt: null,
        }));
        const db = makeDb({ document: { findFirst } });
        const cms = createServerSiteClient(db, {});
        const nav = await cms.getNavigation('main');
        expect(nav).toEqual({
            name: 'Main Menu',
            slug: 'main',
            location: 'header',
            items: [
                { label: 'Home', url: '/' },
                { label: 'Shop', url: '/shop', children: [{ label: 'Widgets', url: '/shop/widgets' }] },
            ],
        });
        expect(findFirst).toHaveBeenCalledWith({
            where: {
                collection: 'navigations',
                deletedAt: null,
                OR: [{ slug: 'main' }, { data: { path: ['slug'], equals: 'main' } }],
            },
            orderBy: { updatedAt: 'desc' },
        });
    });
    it('getNavigation returns null when no menu matches', async () => {
        const db = makeDb();
        const cms = createServerSiteClient(db, {});
        expect(await cms.getNavigation('missing')).toBeNull();
    });
    it('resolveMedia falls back to a database lookup when given an id', async () => {
        const findUnique = vi.fn(async () => ({
            id: 'm-1',
            storageKey: 'https://cdn.example.com/hero.jpg',
            altText: 'Hero',
            title: null,
            width: 1200,
            height: 600,
        }));
        const db = makeDb({ media: { findUnique } });
        const cms = createServerSiteClient(db, {});
        const media = await cms.resolveMedia('m-1');
        expect(media).toEqual({
            id: 'm-1',
            url: 'https://cdn.example.com/hero.jpg',
            alt: 'Hero',
            title: null,
            width: 1200,
            height: 600,
            storageKey: 'https://cdn.example.com/hero.jpg',
        });
        expect(findUnique).toHaveBeenCalledWith({ where: { id: 'm-1' } });
    });
});
//# sourceMappingURL=server-site.test.js.map