import { describe, expect, it, vi } from 'vitest';
import type { PostData, PostFilter } from '../../src/main/engine/PostEngine';
import {
  findSinglePostBySlug,
  loadPostsForDayPage,
  loadPublishedSnapshotsPage,
  type SharedSnapshotPostEngine,
} from '../../src/main/engine/SharedSnapshotService';

function makePost(overrides: Partial<PostData> = {}): PostData {
  const createdAt = overrides.createdAt ?? new Date('2025-01-02T10:00:00.000Z');
  const updatedAt = overrides.updatedAt ?? createdAt;
  const title = overrides.title ?? 'Title';

  return {
    id: overrides.id ?? 'post-1',
    projectId: overrides.projectId ?? 'default',
    title,
    slug: overrides.slug ?? 'title',
    excerpt: overrides.excerpt,
    content: overrides.content ?? `# ${title}\n\nBody`,
    status: overrides.status ?? 'published',
    author: overrides.author,
    language: overrides.language,
    createdAt,
    updatedAt,
    publishedAt: overrides.publishedAt,
    tags: overrides.tags ?? [],
    categories: overrides.categories ?? [],
  };
}

function makeEngine(posts: PostData[], snapshotsById: Record<string, PostData | null> = {}): SharedSnapshotPostEngine {
  const byId = new Map(posts.map((post) => [post.id, post]));

  return {
    async getPost(id: string): Promise<PostData | null> {
      return byId.get(id) ?? null;
    },
    async getPublishedVersion(id: string): Promise<PostData | null> {
      return snapshotsById[id] ?? null;
    },
    async getPostsFiltered(filter: PostFilter): Promise<PostData[]> {
      let result = posts.filter((post) => post.status === (filter.status ?? post.status));

      if (filter.tags && filter.tags.length > 0) {
        result = result.filter((post) => filter.tags!.every((tag) => post.tags.includes(tag)));
      }

      if (filter.categories && filter.categories.length > 0) {
        result = result.filter((post) => filter.categories!.some((category) => post.categories.includes(category)));
      }

      if (filter.excludeCategories && filter.excludeCategories.length > 0) {
        result = result.filter((post) => !filter.excludeCategories!.some((category: string) => post.categories.includes(category)));
      }

      if (filter.year !== undefined) {
        result = result.filter((post) => post.createdAt.getFullYear() === filter.year);
      }

      if (filter.month !== undefined && filter.year !== undefined) {
        result = result.filter((post) => post.createdAt.getMonth() === filter.month - 1);
      }

      if (filter.startDate) {
        result = result.filter((post) => post.createdAt >= filter.startDate!);
      }

      if (filter.endDate) {
        result = result.filter((post) => post.createdAt <= filter.endDate!);
      }

      return result.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
    },
  };
}

describe('SharedSnapshotService', () => {
  it('loads published snapshots merged from published and draft rows', async () => {
    const published = makePost({ id: 'p1', slug: 'published-1', status: 'published' });
    const draft = makePost({ id: 'd1', slug: 'draft-1', status: 'draft' });
    const draftPublishedSnapshot = makePost({ id: 'd1', slug: 'draft-1', status: 'published' });

    const engine = makeEngine([published, draft], { d1: draftPublishedSnapshot });

    const result = await loadPublishedSnapshotsPage(engine, { status: 'published' }, { maxPostsPerPage: 50, page: 1 });

    expect(result.totalPosts).toBe(2);
    expect(result.posts.map((post) => post.id).sort()).toEqual(['d1', 'p1']);
  });

  it('loads day page strictly for given day', async () => {
    const dayA = makePost({ id: 'a', slug: 'a', createdAt: new Date('2025-01-15T10:00:00.000Z') });
    const dayB = makePost({ id: 'b', slug: 'b', createdAt: new Date('2025-01-16T10:00:00.000Z') });
    const engine = makeEngine([dayA, dayB]);

    const result = await loadPostsForDayPage(engine, 2025, 1, 15, { maxPostsPerPage: 50, page: 1 });

    expect(result.totalPosts).toBe(1);
    expect(result.posts).toHaveLength(1);
    expect(result.posts[0]?.id).toBe('a');
  });

  it('prefers matching draft post when draft preview options are provided', async () => {
    const draft = makePost({ id: 'draft-1', slug: 'my-post', status: 'draft', createdAt: new Date('2025-03-21T10:00:00.000Z') });
    const published = makePost({ id: 'pub-1', slug: 'my-post', status: 'published', createdAt: new Date('2025-03-20T10:00:00.000Z') });
    const engine = makeEngine([published, draft]);

    const result = await findSinglePostBySlug(
      engine,
      'my-post',
      { useDraftContent: true, draftPostId: 'draft-1' },
      { year: 2025, month: 3, day: 21 },
    );

    expect(result?.id).toBe('draft-1');
  });

  it('returns a translated draft variant when preview language is provided', async () => {
    const draft = makePost({ id: 'draft-1', slug: 'my-post', status: 'draft', language: 'en', title: 'Canonical title', content: 'Canonical body', createdAt: new Date('2025-03-21T10:00:00.000Z') });
    const engine = {
      ...makeEngine([draft]),
      async getPostTranslation(postId: string, language: string) {
        if (postId === 'draft-1' && language === 'fr') {
          return {
            id: 'translation-1',
            translationFor: 'draft-1',
            language: 'fr',
            title: 'Titre traduit',
            excerpt: 'Resume traduit',
            content: 'Contenu traduit',
            status: 'draft',
            createdAt: new Date('2025-03-21T10:00:00.000Z'),
            updatedAt: new Date('2025-03-21T11:00:00.000Z'),
            publishedAt: null,
            filePath: 'posts/my-post.fr.md',
          };
        }
        return null;
      },
    } as SharedSnapshotPostEngine & {
      getPostTranslation: (postId: string, language: string) => Promise<any>;
    };

    const result = await findSinglePostBySlug(
      engine,
      'my-post',
      { useDraftContent: true, draftPostId: 'draft-1', lang: 'fr' },
      { year: 2025, month: 3, day: 21 },
    );

    expect(result).toMatchObject({
      id: 'translation-1',
      slug: 'my-post.fr',
      language: 'fr',
      title: 'Titre traduit',
      excerpt: 'Resume traduit',
      content: 'Contenu traduit',
    });
  });

  it('prefers project main language content and falls back to canonical language when no translation exists', async () => {
    const published = makePost({
      id: 'post-1',
      slug: 'my-post',
      status: 'published',
      language: 'en',
      title: 'Canonical title',
      content: 'Canonical body',
      createdAt: new Date('2025-03-21T10:00:00.000Z'),
    });
    const getPostTranslation = vi.fn(async (postId: string, language: string) => {
      if (postId === 'post-1' && language === 'fr') {
        return {
          id: 'translation-1',
          projectId: 'default',
          translationFor: 'post-1',
          language: 'fr',
          title: 'Titre principal',
          excerpt: 'Resume principal',
          content: 'Contenu principal',
          status: 'published',
          createdAt: new Date('2025-03-21T10:00:00.000Z'),
          updatedAt: new Date('2025-03-21T11:00:00.000Z'),
          publishedAt: new Date('2025-03-21T11:00:00.000Z'),
          filePath: 'posts/my-post.fr.md',
        };
      }
      return null;
    });
    const engine = {
      ...makeEngine([published]),
      getPostTranslation,
    } as SharedSnapshotPostEngine & {
      getPostTranslation: (postId: string, language: string) => Promise<any>;
    };

    const translated = await findSinglePostBySlug(
      engine,
      'my-post',
      { preferredLanguage: 'fr' },
      { year: 2025, month: 3, day: 21 },
    );

    expect(translated).toMatchObject({
      id: 'translation-1',
      slug: 'my-post.fr',
      language: 'fr',
      title: 'Titre principal',
      content: 'Contenu principal',
    });

    const fallback = await findSinglePostBySlug(
      engine,
      'my-post',
      { preferredLanguage: 'de' },
      { year: 2025, month: 3, day: 21 },
    );

    expect(fallback).toMatchObject({
      id: 'post-1',
      slug: 'my-post',
      language: 'en',
      title: 'Canonical title',
      content: 'Canonical body',
    });
    expect(getPostTranslation).toHaveBeenNthCalledWith(1, 'post-1', 'fr');
    expect(getPostTranslation).toHaveBeenNthCalledWith(2, 'post-1', 'de');
  });

  it('uses findPublishedBySlug shortcut when present', async () => {
    const post = makePost({ id: 'x1', slug: 'shortcut', status: 'published' });
    const engine = makeEngine([post]);
    const findPublishedBySlug = vi.fn(async () => post);
    const engineWithShortcut: SharedSnapshotPostEngine = {
      ...engine,
      findPublishedBySlug,
    };

    const result = await findSinglePostBySlug(engineWithShortcut, 'shortcut', undefined, { year: 2025, month: 1, day: 2 });

    expect(result?.id).toBe('x1');
    expect(findPublishedBySlug).toHaveBeenCalled();
  });
});
