import { describe, expect, it, vi } from 'vitest';
import { render } from 'vitest-browser-svelte';
import { m } from '$lib/paraglide/messages.js';
import ImportStatusCard from './ImportStatusCard.svelte';
import type { ImportStatus } from './types.js';

const makeStatus = (overrides: Partial<ImportStatus> = {}): ImportStatus => ({
	state: 'IDLE',
	statusCode: 'IMPORT_IDLE',
	processed: 0,
	skipped: 0,
	skippedFiles: [],
	startedAt: null,
	...overrides
});

describe('ImportStatusCard', () => {
	it('shows spinner while state is RUNNING', async () => {
		const { getByTestId } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({ state: 'RUNNING', statusCode: 'IMPORT_RUNNING', processed: 3 }),
				ontrigger: () => {}
			}
		});

		await expect.element(getByTestId('spinner')).toBeInTheDocument();
	});

	it('shows no processed count while RUNNING (spinner only, no misleading 0)', async () => {
		// The whole document load commits in one transaction, so a live count would sit at 0
		// until the end. Show just the spinner + "running" label instead of a stuck "0".
		const { getByTestId } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({ state: 'RUNNING', statusCode: 'IMPORT_RUNNING', processed: 0 }),
				ontrigger: () => {}
			}
		});

		await expect.element(getByTestId('processed-count')).not.toBeInTheDocument();
	});

	it('shows processed count while DONE', async () => {
		const { getByText } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({ state: 'DONE', statusCode: 'IMPORT_DONE', processed: 42 }),
				ontrigger: () => {}
			}
		});

		await expect.element(getByText('42')).toBeVisible();
	});

	it('shows no-spreadsheet message when statusCode is IMPORT_FAILED_NO_SPREADSHEET', async () => {
		const { getByText } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({
					state: 'FAILED',
					statusCode: 'IMPORT_FAILED_NO_SPREADSHEET'
				}),
				ontrigger: () => {}
			}
		});

		await expect.element(getByText(m.admin_system_import_failed_no_spreadsheet())).toBeVisible();
	});

	it('shows internal error message when statusCode is IMPORT_FAILED_INTERNAL', async () => {
		const { getByText } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({ state: 'FAILED', statusCode: 'IMPORT_FAILED_INTERNAL' }),
				ontrigger: () => {}
			}
		});

		await expect.element(getByText(m.admin_system_import_failed_internal())).toBeVisible();
	});

	it('shows idle text when importStatus is non-null and state is IDLE', async () => {
		const { getByText } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({ state: 'IDLE', statusCode: 'IMPORT_IDLE' }),
				ontrigger: () => {}
			}
		});

		await expect.element(getByText(m.admin_system_import_status_idle())).toBeVisible();
	});

	it('shows no spinner when importStatus is null', async () => {
		const { getByTestId } = render(ImportStatusCard, {
			props: { importStatus: null, ontrigger: () => {} }
		});

		await expect.element(getByTestId('spinner')).not.toBeInTheDocument();
	});

	it('calls ontrigger when retry button is clicked in DONE state', async () => {
		const ontrigger = vi.fn();
		const { getByRole } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({ state: 'DONE', statusCode: 'IMPORT_DONE', processed: 5 }),
				ontrigger
			}
		});

		await getByRole('button').click();
		expect(ontrigger).toHaveBeenCalledOnce();
	});

	it('calls ontrigger when retry button is clicked in FAILED state', async () => {
		const ontrigger = vi.fn();
		const { getByRole } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({ state: 'FAILED', statusCode: 'IMPORT_FAILED_INTERNAL' }),
				ontrigger
			}
		});

		await getByRole('button').click();
		expect(ontrigger).toHaveBeenCalledOnce();
	});

	it('calls ontrigger when start button is clicked in IDLE state', async () => {
		const ontrigger = vi.fn();
		const { getByRole } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({ state: 'IDLE', statusCode: 'IMPORT_IDLE' }),
				ontrigger
			}
		});

		await getByRole('button').click();
		expect(ontrigger).toHaveBeenCalledOnce();
	});

	it('shows skipped count when DONE and skipped > 0', async () => {
		const { getByTestId } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({
					state: 'DONE',
					statusCode: 'IMPORT_DONE',
					processed: 10,
					skipped: 3,
					skippedFiles: [
						{ filename: 'fake.pdf', reason: 'INVALID_PDF_SIGNATURE' },
						{ filename: 'other.pdf', reason: 'INVALID_PDF_SIGNATURE' },
						{ filename: 'tiny.pdf', reason: 'INVALID_PDF_SIGNATURE' }
					]
				}),
				ontrigger: () => {}
			}
		});

		await expect.element(getByTestId('skipped-count')).toHaveTextContent('3');
	});

	it('shows skipped filenames in collapsible list when DONE and skipped > 0', async () => {
		const { getByText } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({
					state: 'DONE',
					statusCode: 'IMPORT_DONE',
					processed: 5,
					skipped: 1,
					skippedFiles: [{ filename: 'fake.pdf', reason: 'INVALID_PDF_SIGNATURE' }]
				}),
				ontrigger: () => {}
			}
		});

		await expect.element(getByText('fake.pdf')).toBeInTheDocument();
	});

	it('does not show skipped section when DONE and skipped is 0', async () => {
		const { getByTestId } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({ state: 'DONE', statusCode: 'IMPORT_DONE', processed: 5 }),
				ontrigger: () => {}
			}
		});

		await expect.element(getByTestId('skipped-count')).not.toBeInTheDocument();
	});

	it('does not show skipped section when RUNNING even with skipped > 0', async () => {
		const { getByTestId } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({
					state: 'RUNNING',
					statusCode: 'IMPORT_RUNNING',
					processed: 5,
					skipped: 2,
					skippedFiles: [
						{ filename: 'a.pdf', reason: 'INVALID_PDF_SIGNATURE' },
						{ filename: 'b.pdf', reason: 'INVALID_PDF_SIGNATURE' }
					]
				}),
				ontrigger: () => {}
			}
		});

		await expect.element(getByTestId('skipped-count')).not.toBeInTheDocument();
	});

	it('does not show skipped section when FAILED even with skipped > 0', async () => {
		const { getByTestId } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({
					state: 'FAILED',
					statusCode: 'IMPORT_FAILED_INTERNAL',
					skipped: 1,
					skippedFiles: [{ filename: 'bad.pdf', reason: 'INVALID_PDF_SIGNATURE' }]
				}),
				ontrigger: () => {}
			}
		});

		await expect.element(getByTestId('skipped-count')).not.toBeInTheDocument();
	});

	it('shows raw reason code for unknown skip reasons', async () => {
		const { getByText } = render(ImportStatusCard, {
			props: {
				importStatus: makeStatus({
					state: 'DONE',
					statusCode: 'IMPORT_DONE',
					processed: 1,
					skipped: 1,
					skippedFiles: [{ filename: 'odd.pdf', reason: 'SOME_FUTURE_CODE' }]
				}),
				ontrigger: () => {}
			}
		});

		await expect.element(getByText('SOME_FUTURE_CODE', { exact: false })).toBeInTheDocument();
	});
});
