import { test, expect, type Page } from './fixtures';

const mangaId = '33333333-3333-3333-3333-333333333333';
const chapter1Id = 'c1111111-3333-3333-3333-333333333333';
const chapter2Id = 'c2222222-3333-3333-3333-333333333333';
const chapter3Id = 'c3333333-3333-3333-3333-333333333333';

// Faithful `MangaDetail` (GET /v1/mangas/:id) so getManga in the reader
// load returns a shape the components can consume without throwing.
const mangaFixture = {
    id: mangaId,
    title: 'Vinland Saga',
    status: 'ongoing',
    alt_titles: [],
    description: null,
    cover_image_path: null,
    created_at: '2026-01-01T00:00:00Z',
    updated_at: '2026-01-01T00:00:00Z',
    authors: [{ id: 'a3333333-3333-3333-3333-333333333333', name: 'Makoto Yukimura' }],
    genres: [],
    tags: [],
    content_warnings: [],
    chapter_storage_bytes: 0
};

const chaptersFixture = [
    {
        id: chapter1Id,
        manga_id: mangaId,
        number: 1,
        title: 'Somewhere, Not Here',
        page_count: 1,
        created_at: '2026-01-01T00:00:00Z'
    },
    {
        id: chapter2Id,
        manga_id: mangaId,
        number: 2,
        title: null,
        page_count: 1,
        created_at: '2026-01-02T00:00:00Z'
    },
    {
        id: chapter3Id,
        manga_id: mangaId,
        number: 3,
        title: 'Sword Dance',
        page_count: 1,
        created_at: '2026-01-03T00:00:00Z'
    }
];

function pageFixture(chapterId: string) {
    return [
        {
            id: `p1111111-${chapterId.slice(1, 8)}-3333-3333-333333333333`,
            chapter_id: chapterId,
            page_number: 1,
            storage_key: `mangas/${mangaId}/chapters/${chapterId}/pages/0001.png`,
            content_type: 'image/png'
        }
    ];
}

async function mockReaderApis(page: Page) {
    // Force public mode so the layout doesn't bounce anonymous visitors
    // to /login (the dev backend on this machine runs with
    // PRIVATE_MODE=true, which the layout's universal load respects).
    await page.route('**/api/v1/auth/config', (route) =>
        route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({ self_register_enabled: true, private_mode: false })
        })
    );
    await page.route('**/api/v1/auth/me', (route) =>
        route.fulfill({
            status: 401,
            contentType: 'application/json',
            body: JSON.stringify({ error: { code: 'unauthenticated', message: '' } })
        })
    );
    await page.route('**/api/v1/auth/me/preferences', (route) =>
        route.fulfill({
            status: 401,
            contentType: 'application/json',
            body: JSON.stringify({ error: { code: 'unauthenticated', message: '' } })
        })
    );
    await page.route('**/api/v1/me/bookmarks*', (route) =>
        route.fulfill({
            status: 401,
            contentType: 'application/json',
            body: JSON.stringify({ error: { code: 'unauthenticated', message: '' } })
        })
    );
    // The reader load reads saved position; guest → 401 → null. Without
    // this the fetch falls through to the (absent) backend and the load
    // rejects before the reader renders.
    await page.route(`**/api/v1/me/read-progress/${mangaId}`, (route) =>
        route.fulfill({
            status: 401,
            contentType: 'application/json',
            body: JSON.stringify({ error: { code: 'unauthenticated', message: '' } })
        })
    );
    await page.route(`**/api/v1/mangas/${mangaId}`, (route) =>
        route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify(mangaFixture)
        })
    );
    await page.route(new RegExp(`/api/v1/mangas/${mangaId}/chapters(\\?.*)?$`), (route) =>
        route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({
                items: chaptersFixture,
                page: { limit: 200, offset: 0, total: chaptersFixture.length }
            })
        })
    );
    for (const c of chaptersFixture) {
        await page.route(`**/api/v1/mangas/${mangaId}/chapters/${c.id}`, (route) =>
            route.fulfill({
                status: 200,
                contentType: 'application/json',
                body: JSON.stringify(c)
            })
        );
        await page.route(
            `**/api/v1/mangas/${mangaId}/chapters/${c.id}/pages`,
            (route) =>
                route.fulfill({
                    status: 200,
                    contentType: 'application/json',
                    body: JSON.stringify({ pages: pageFixture(c.id) })
                })
        );
    }
    const png = Buffer.from(
        '89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4890000000d49444154789c63000100000005000158a3b62a0000000049454e44ae426082',
        'hex'
    );
    await page.route('**/api/v1/files/**', (route) =>
        route.fulfill({ status: 200, contentType: 'image/png', body: png })
    );
}

test('reader chapter select lists every chapter with the manga-detail-style label', async ({
    page
}) => {
    await mockReaderApis(page);
    await page.goto(`/manga/${mangaId}/chapter/${chapter2Id}`);

    const select = page.getByTestId('reader-chapter-select');
    await expect(select).toBeVisible();

    // The current chapter is preselected.
    await expect(select).toHaveValue(chapter2Id);

    // Each chapter rendered via the shared `chapterLabel` helper — the
    // chapter title, or "Chapter N" when the title is null — in ascending
    // number order, matching the prev/next sort and the manga-detail list.
    const labels = await select.locator('option').allTextContents();
    expect(labels.map((l) => l.trim())).toEqual([
        'Somewhere, Not Here',
        'Chapter 2',
        'Sword Dance'
    ]);
});

test('choosing a chapter from the select navigates to that chapter', async ({ page }) => {
    await mockReaderApis(page);
    await page.goto(`/manga/${mangaId}/chapter/${chapter1Id}`);

    await expect(page.getByTestId('reader-chapter-select')).toHaveValue(chapter1Id);

    await page.getByTestId('reader-chapter-select').selectOption(chapter3Id);

    await expect(page).toHaveURL(
        new RegExp(`/manga/${mangaId}/chapter/${chapter3Id}$`)
    );
    await expect(page.getByTestId('reader-chapter-select')).toHaveValue(chapter3Id);
});
