import { error } from "@sveltejs/kit";
import { createBobbinClient } from "$lib/api/client";
import { enrich, handleOf, miniDocOf, TYPE_MINIDOC } from "$lib/api/enrich";
import { INVALID_HANDLE, type MiniDoc } from "$lib/api/identity";
import {
	listIssueStates,
	type CommentListPage,
	type IssueRecord,
	type ReactionListPage,
	type RecordView,
	type ReactionRecord
} from "$lib/api/records";
import { didFromUri, rkeyFromUri } from "$lib/api/uri";
import { renderMarkup } from "$lib/markup";
import { buildCommentThreads, type ThreadInput } from "$lib/components/comment/comments";
import { buildReactions, type ReactionGroup } from "$lib/components/reaction/reactions";
import type { PageLoad } from "./$types";

export const load: PageLoad = async (event) => {
	const parent = await event.parent();
	const uri = decodeURIComponent(event.params.aturi);

	if (!uri.startsWith("at://")) error(404, "Issue not found");

	const ctx = createBobbinClient({ serviceUrl: parent.publicConfig.bobbinUrl, fetch: event.fetch });

	const issuePage = await enrich<RecordView<IssueRecord>>(ctx, {
		xrpc: "sh.tangled.repo.getIssue",
		params: { issue: uri },
		enrich: [{ source: "sh.tangled.repo.issue:.repo", type: TYPE_MINIDOC, targets: ["uri"] }]
	}).catch(() => null);
	if (!issuePage) error(404, "Issue not found");

	const record = issuePage.output;
	const authorDid = didFromUri(record.uri);
	const markupOpts = {
		repo: `${parent.repo.ownerHandle}/${parent.repo.name}`,
		ref: parent.repo.defaultBranch,
		host: event.url.host
	};

	const authorHandle = handleOf(issuePage.data, authorDid, "sh.tangled.repo.issue:.repo");
	const [states, comments] = await Promise.all([
		listIssueStates(ctx, record.uri, { limit: 1, order: "desc" }).catch(() => null),
		enrich<CommentListPage>(ctx, {
			xrpc: "sh.tangled.feed.listComments",
			params: { subject: record.uri, order: "asc", limit: 100 },
			enrich: [
				{
					source: "sh.tangled.feed.comment:.repo",
					type: TYPE_MINIDOC,
					targets: ["items[].uri"]
				}
			]
		}).catch(() => null)
	]);

	const latest = states?.items[0]?.value.state;
	const state = latest?.endsWith(".closed") ? ("closed" as const) : ("open" as const);
	const body = record.value.body ?? "";
	const bodyHtml = body ? await renderMarkup(body, markupOpts) : null;
	const viewerDid = parent.auth?.did;
	const commentItems = comments?.output.items ?? [];

	// reactions: one listReactions per subject (the issue itself + each comment).
	// bobbin has no batch-by-subject query and enrich can't break counts down by
	// emoji, so fan out and group client-side. reactor docs ride along in the
	// same enrich calls
	const subjects = [record.uri, ...commentItems.map((item) => item.uri)];
	const reactionPages = await Promise.all(
		subjects.map((subject) =>
			enrich<ReactionListPage>(ctx, {
				xrpc: "sh.tangled.feed.listReactions",
				params: { subject, order: "asc", limit: 100 },
				enrich: [
					{
						source: "sh.tangled.feed.reaction:.repo",
						type: TYPE_MINIDOC,
						targets: ["items[].uri"]
					}
				]
			}).catch(() => null)
		)
	);
	const reactionsBySubject = new Map<string, RecordView<ReactionRecord>[]>();
	subjects.forEach((subject, i) =>
		reactionsBySubject.set(subject, reactionPages[i]?.output.items ?? [])
	);

	const reactorDids = new Set<string>();
	for (const items of reactionsBySubject.values()) {
		for (const item of items) reactorDids.add(didFromUri(item.uri));
	}
	// a reactor's doc can land in any subject's sidecar
	const reactorDoc = (did: string): MiniDoc | undefined => {
		for (const page of reactionPages) {
			const doc = page && miniDocOf(page.data, did, "sh.tangled.feed.reaction:.repo");
			if (doc) return doc;
		}
		return undefined;
	};
	const reactorHandles = new Map(
		[...reactorDids].map((did) => [did, reactorDoc(did)?.handle ?? INVALID_HANDLE] as const)
	);
	const reactionsFor = (subject: string): ReactionGroup[] =>
		buildReactions(
			reactionsBySubject.get(subject) ?? [],
			viewerDid,
			(did) => reactorHandles.get(did) ?? INVALID_HANDLE
		);

	const threadInputs: ThreadInput[] = await Promise.all(
		commentItems.map(async (item): Promise<ThreadInput> => {
			const commentDid = didFromUri(item.uri);
			const commentBody = item.value.body?.text ?? "";
			const authorHandle = handleOf(
				comments?.data ?? {},
				commentDid,
				"sh.tangled.feed.comment:.repo"
			);
			const commentBodyHtml = commentBody ? await renderMarkup(commentBody, markupOpts) : null;
			return {
				comment: {
					uri: item.uri,
					cid: item.cid,
					rkey: rkeyFromUri(item.uri),
					authorDid: commentDid,
					authorHandle,
					createdAt: item.value.createdAt,
					body: commentBody,
					bodyHtml: commentBodyHtml,
					reactions: reactionsFor(item.uri)
				},
				replyTo: item.value.replyTo?.uri ?? null,
				replyToCid: item.value.replyTo?.cid
			};
		})
	);

	return {
		issue: {
			uri: record.uri,
			cid: record.cid,
			rkey: rkeyFromUri(record.uri),
			title: record.value.title,
			body,
			bodyHtml,
			state,
			authorDid,
			authorHandle,
			createdAt: record.value.createdAt,
			reactions: reactionsFor(record.uri)
		},
		comments: buildCommentThreads(threadInputs),
		markup: markupOpts
	};
};
