import { describe, expect, it, vi, beforeEach } from 'vitest';
import { load } from './+page.server';

vi.mock('$lib/shared/api.server', () => ({
	createApiClient: vi.fn(),
	extractErrorCode: (e: unknown) => (e as { code?: string } | undefined)?.code
}));

import { createApiClient } from '$lib/shared/api.server';

const mockFetch = vi.fn() as unknown as typeof fetch;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockLocals = { user: { groups: [{ permissions: ['READ_ALL'] }] } } as any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockLocalsWriter = { user: { groups: [{ permissions: ['WRITE_ALL'] }] } } as any;

beforeEach(() => vi.clearAllMocks());

// ─── happy path ───────────────────────────────────────────────────────────────

describe('person detail load — happy path', () => {
	it('returns person, sentDocuments, and receivedDocuments on success', async () => {
		vi.mocked(createApiClient).mockReturnValue({
			GET: vi
				.fn()
				.mockResolvedValueOnce({
					response: { ok: true, status: 200 },
					data: { id: 'p1', firstName: 'Hans', lastName: 'Müller' }
				})
				.mockResolvedValueOnce({ response: { ok: true }, data: [{ id: 'd1', title: 'Brief' }] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
		} as ReturnType<typeof createApiClient>);

		const result = await load({
			params: { id: 'p1' },
			fetch: mockFetch,
			request: new Request('http://localhost/persons/p1'),
			url: new URL('http://localhost/persons/p1'),
			locals: mockLocals
		});

		expect(result.person.firstName).toBe('Hans');
		expect(result.sentDocuments).toHaveLength(1);
		expect(result.receivedDocuments).toEqual([]);
	});

	it('returns canWrite=true when user has WRITE_ALL', async () => {
		vi.mocked(createApiClient).mockReturnValue({
			GET: vi
				.fn()
				.mockResolvedValueOnce({
					response: { ok: true, status: 200 },
					data: { id: 'p1', firstName: 'Anna', lastName: 'Schmidt' }
				})
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
		} as ReturnType<typeof createApiClient>);

		const result = await load({
			params: { id: 'p1' },
			fetch: mockFetch,
			request: new Request('http://localhost/persons/p1'),
			url: new URL('http://localhost/persons/p1'),
			locals: mockLocalsWriter
		});

		expect(result.canWrite).toBe(true);
	});

	it('returns empty arrays when sent/received document APIs fail', async () => {
		vi.mocked(createApiClient).mockReturnValue({
			GET: vi
				.fn()
				.mockResolvedValueOnce({
					response: { ok: true, status: 200 },
					data: { id: 'p1', firstName: 'Anna', lastName: 'Schmidt' }
				})
				.mockResolvedValueOnce({ response: { ok: false }, data: null })
				.mockResolvedValueOnce({ response: { ok: false }, data: null })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
		} as ReturnType<typeof createApiClient>);

		const result = await load({
			params: { id: 'p1' },
			fetch: mockFetch,
			request: new Request('http://localhost/persons/p1'),
			url: new URL('http://localhost/persons/p1'),
			locals: mockLocals
		});

		expect(result.sentDocuments).toEqual([]);
		expect(result.receivedDocuments).toEqual([]);
	});
});

// ─── error paths ──────────────────────────────────────────────────────────────

describe('person detail load — error paths', () => {
	it('throws 404 when person does not exist', async () => {
		vi.mocked(createApiClient).mockReturnValue({
			GET: vi
				.fn()
				.mockResolvedValueOnce({ response: { ok: false, status: 404 }, error: null })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
		} as ReturnType<typeof createApiClient>);

		await expect(
			load({
				params: { id: 'missing' },
				fetch: mockFetch,
				request: new Request('http://localhost/persons/p1'),
				url: new URL('http://localhost/persons/p1'),
				locals: mockLocals
			})
		).rejects.toMatchObject({
			status: 404
		});
	});

	it('throws 403 when person is not accessible', async () => {
		vi.mocked(createApiClient).mockReturnValue({
			GET: vi
				.fn()
				.mockResolvedValueOnce({ response: { ok: false, status: 403 }, error: null })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
				.mockResolvedValueOnce({ response: { ok: true }, data: [] })
		} as ReturnType<typeof createApiClient>);

		await expect(
			load({
				params: { id: 'forbidden' },
				fetch: mockFetch,
				request: new Request('http://localhost/persons/p1'),
				url: new URL('http://localhost/persons/p1'),
				locals: mockLocals
			})
		).rejects.toMatchObject({
			status: 403
		});
	});
});
