import { redirect } from '@sveltejs/kit';
import { createApiClient } from '$lib/shared/api.server';
import type { components } from '$lib/generated/api';

type StatsDTO = components['schemas']['StatsDTO'];
type TranscriptionQueueItemDTO = components['schemas']['TranscriptionQueueItemDTO'];
type TranscriptionWeeklyStatsDTO = components['schemas']['TranscriptionWeeklyStatsDTO'];
type DashboardResumeDTO = components['schemas']['DashboardResumeDTO'];
type DashboardPulseDTO = components['schemas']['DashboardPulseDTO'];
type ActivityFeedItemDTO = components['schemas']['ActivityFeedItemDTO'];
type IncompleteDocumentDTO = components['schemas']['IncompleteDocumentDTO'];

export async function load({ fetch }) {
	const api = createApiClient(fetch);

	try {
		const personsResult = await api.GET('/api/persons');

		if (personsResult.response.status === 401) {
			throw redirect(302, '/login');
		}

		const [
			statsResult,
			resumeResult,
			pulseResult,
			activityResult,
			segmentationResult,
			transcriptionResult,
			readyResult,
			weeklyStatsResult,
			incompleteResult,
			incompleteCountResult
		] = await Promise.allSettled([
			api.GET('/api/stats'),
			api.GET('/api/dashboard/resume'),
			api.GET('/api/dashboard/pulse'),
			api.GET('/api/dashboard/activity', { params: { query: { limit: 7 } } }),
			api.GET('/api/transcription/segmentation-queue'),
			api.GET('/api/transcription/transcription-queue'),
			api.GET('/api/transcription/ready-to-read'),
			api.GET('/api/transcription/weekly-stats'),
			api.GET('/api/documents/incomplete', { params: { query: { size: 5 } } }),
			api.GET('/api/documents/incomplete-count')
		]);

		let stats: StatsDTO | null = null;
		let resumeDoc: DashboardResumeDTO | null = null;
		let pulse: DashboardPulseDTO | null = null;
		let activityFeed: ActivityFeedItemDTO[] = [];
		let segmentationDocs: TranscriptionQueueItemDTO[] = [];
		let transcriptionDocs: TranscriptionQueueItemDTO[] = [];
		let readyDocs: TranscriptionQueueItemDTO[] = [];
		let weeklyStats: TranscriptionWeeklyStatsDTO | null = null;
		let incompleteDocs: IncompleteDocumentDTO[] = [];
		let incompleteTotal = 0;

		if (statsResult.status === 'fulfilled' && statsResult.value.response.ok) {
			stats = statsResult.value.data ?? null;
		}
		if (resumeResult.status === 'fulfilled' && resumeResult.value.response.ok) {
			resumeDoc = (resumeResult.value.data as DashboardResumeDTO) ?? null;
		}
		if (pulseResult.status === 'fulfilled' && pulseResult.value.response.ok) {
			pulse = (pulseResult.value.data as DashboardPulseDTO) ?? null;
		}
		if (activityResult.status === 'fulfilled' && activityResult.value.response.ok) {
			activityFeed = (activityResult.value.data as ActivityFeedItemDTO[]) ?? [];
		}
		if (segmentationResult.status === 'fulfilled' && segmentationResult.value.response.ok) {
			segmentationDocs = (segmentationResult.value.data ?? []) as TranscriptionQueueItemDTO[];
		}
		if (transcriptionResult.status === 'fulfilled' && transcriptionResult.value.response.ok) {
			transcriptionDocs = (transcriptionResult.value.data ?? []) as TranscriptionQueueItemDTO[];
		}
		if (readyResult.status === 'fulfilled' && readyResult.value.response.ok) {
			readyDocs = (readyResult.value.data ?? []) as TranscriptionQueueItemDTO[];
		}
		if (weeklyStatsResult.status === 'fulfilled' && weeklyStatsResult.value.response.ok) {
			weeklyStats = weeklyStatsResult.value.data ?? null;
		}
		if (incompleteResult.status === 'fulfilled' && incompleteResult.value.response.ok) {
			incompleteDocs = (incompleteResult.value.data ?? []) as IncompleteDocumentDTO[];
		}
		if (incompleteCountResult.status === 'fulfilled' && incompleteCountResult.value.response.ok) {
			incompleteTotal = (incompleteCountResult.value.data?.count as number | undefined) ?? 0;
		}

		return {
			stats,
			resumeDoc,
			pulse,
			activityFeed,
			segmentationDocs,
			transcriptionDocs,
			readyDocs,
			weeklyStats,
			incompleteDocs,
			incompleteTotal,
			error: null as string | null
		};
	} catch (e) {
		if ((e as { status?: number }).status) throw e;
		console.error('Error loading data:', e);
		return {
			stats: null,
			resumeDoc: null,
			pulse: null,
			activityFeed: [],
			segmentationDocs: [],
			transcriptionDocs: [],
			readyDocs: [],
			weeklyStats: null,
			incompleteDocs: [] as IncompleteDocumentDTO[],
			incompleteTotal: 0,
			error: 'Daten konnten nicht geladen werden.' as string | null
		};
	}
}
