import { RequestCtx } from '@/backend/routes.ts';

import { View } from '@/backend/templates/index.ts';
import { html } from '@atelier/responses.ts';
import { arrayIsEmpty } from '@atelier/array.ts';

type ActivityType = 'created_item';
type Activity = {
	activityType: ActivityType;
	itemXid: string;
	createdTimestamp: string;
};

function Activity(props: { activity: Activity }) {}
function ActivityList(props: { activities: Activity[] }) {
	if (arrayIsEmpty(props.activities)) {
		return 'No activities yet!';
	}

	const activities = props.activities.map((activity) => Activity({ activity }));
	return `
	<ul>

	</ul>
	`;
}

export function viewHome(req: Request, { db }: RequestCtx) {
	const recentActivities: Activity[] = db.prepare(`
	select activity_json_blob from activity
	order by json_extract(activity_json_blob, '$.createdTimestamp')
	limit 20
	`).values<[string]>().map(([blob]) => JSON.parse(blob));

	const body = `
		<h1>Recent Activity</h1>
		${ActivityList({ activities: recentActivities })}
	`;
	return html(View({ title: 'Home', body }));
}
