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

// The homepage "Continue reading" shelf: visible with a per-manga new-chapter
// badge when the user has reading history, and absent for anonymous visitors
// (the read-progress fetch 401s → empty → hidden).

const emptyMangas = { items: [], page: { limit: 50, offset: 0, total: 0 } };

async function mockCommon(page: Page) {
    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/genres*', (route) =>
        route.fulfill({ status: 200, contentType: 'application/json', body: '[]' })
    );
    await page.route('**/api/v1/mangas*', (route) =>
        route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify(emptyMangas)
        })
    );
}

test('shows the continue-reading shelf with a new-chapter badge when signed in', async ({ page }) => {
    await mockCommon(page);
    await page.route('**/api/v1/auth/me', (route) =>
        route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({ user: { id: 'u1', username: 'reader', is_admin: false } })
        })
    );
    await page.route('**/api/v1/me/read-progress*', (route) =>
        route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({
                items: [
                    {
                        manga_id: 'm1',
                        manga_title: 'Berserk',
                        manga_cover_image_path: null,
                        chapter_id: 'c1',
                        chapter_number: 3,
                        chapter_page_count: 20,
                        page: 4,
                        updated_at: '2026-01-01T00:00:00Z',
                        new_chapters_count: 2
                    }
                ],
                page: { limit: 50, offset: 0, total: 1 }
            })
        })
    );

    await page.goto('/');

    const card = page.getByTestId('continue-card-m1');
    await expect(card).toBeVisible();
    await expect(card).toHaveAttribute('href', '/manga/m1/chapter/c1?page=4');
    await expect(page.getByTestId('continue-new-badge-m1')).toContainText('2');
});

test('keeps finished series off the shelf but shows in-progress ones', async ({ page }) => {
    await mockCommon(page);
    await page.route('**/api/v1/auth/me', (route) =>
        route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ user: { id: 'u1', username: 'reader', is_admin: false } }) })
    );
    await page.route('**/api/v1/me/read-progress*', (route) =>
        route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({
                items: [
                    // Finished: last page of the latest chapter, nothing new.
                    {
                        manga_id: 'done', manga_title: 'Finished', manga_cover_image_path: null,
                        chapter_id: 'dc', chapter_number: 5, chapter_page_count: 10, page: 10,
                        updated_at: '2026-01-02T00:00:00Z', new_chapters_count: 0
                    },
                    // In progress: mid-chapter.
                    {
                        manga_id: 'wip', manga_title: 'Ongoing', manga_cover_image_path: null,
                        chapter_id: 'wc', chapter_number: 2, chapter_page_count: 10, page: 3,
                        updated_at: '2026-01-01T00:00:00Z', new_chapters_count: 0
                    }
                ],
                page: { limit: 50, offset: 0, total: 2 }
            })
        })
    );

    await page.goto('/');
    await expect(page.getByTestId('continue-card-wip')).toBeVisible();
    await expect(page.getByTestId('continue-card-done')).toHaveCount(0);
});

test('hides the continue-reading shelf for anonymous visitors', async ({ page }) => {
    await mockCommon(page);
    await page.route('**/api/v1/auth/me', (route) =>
        route.fulfill({
            status: 401,
            contentType: 'application/json',
            body: JSON.stringify({ error: { code: 'unauthenticated', message: 'no' } })
        })
    );
    await page.route('**/api/v1/me/read-progress*', (route) =>
        route.fulfill({
            status: 401,
            contentType: 'application/json',
            body: JSON.stringify({ error: { code: 'unauthenticated', message: 'no' } })
        })
    );

    await page.goto('/');
    // The catalogue heading confirms the page rendered before asserting absence.
    await expect(page.getByRole('heading', { name: 'Mangas' })).toBeVisible();
    await expect(page.getByTestId('continue-shelf')).toHaveCount(0);
});
