use crate::prelude::*;

enum ActivityType {
	CreatedItem,
}
struct Activity {
	activity_type: ActivityType,
	item_xid: String,
	created_timestamp: String,
}

pub async fn view(req: Request, ctx: RequestCtx) -> HandlerResult<String> {
	let activities: Vec<_> = ctx
		.dbs
		.send(|conn| {
			let mut stmt = conn.prepare(
				r#"
				select activity_json_blob from activity
				order by json_extract(activity_json_blob, '$.created_timestamp')
				limit 20
				"#,
			)?;

			Ok(stmt.query_map([], |r| r.get::<_, String>(0))?.collect())
		})
		.await?;

	todo!()
	/*
	db.prepare(`
	`).values<[string]>().map(([blob]) => JSON.parse(blob));

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

/*
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 }));
}

*/
