import { t as validateIdentifier } from "./validate-VPnKoIzW.mjs";
import { t as ContentRepository } from "./content-DhgEth4k.mjs";
import { t as MediaRepository } from "./media-BrXIaMFN.mjs";
import { t as UserRepository } from "./user-BjAOvU3Z.mjs";
import { sql } from "kysely";

//#region src/api/handlers/dashboard.ts
/**
* Dashboard stats handler
*
* Returns summary data for the admin dashboard in a single request:
* collection content counts, media count, user count, and recent
* content across all collections.
*/
/**
* Fetch dashboard statistics.
*
* Queries are intentionally lightweight — counts use indexed columns,
* and recent items are capped at 10.
*/
async function handleDashboardStats(db) {
	try {
		const collections = await db.selectFrom("_emdash_collections").select(["slug", "label"]).orderBy("slug", "asc").execute();
		const contentRepo = new ContentRepository(db);
		const collectionStats = await Promise.all(collections.map(async (col) => {
			const stats = await contentRepo.getStats(col.slug);
			return {
				slug: col.slug,
				label: col.label,
				total: stats.total,
				published: stats.published,
				draft: stats.draft
			};
		}));
		const mediaRepo = new MediaRepository(db);
		const userRepo = new UserRepository(db);
		const [mediaCount, userCount] = await Promise.all([mediaRepo.count(), userRepo.count()]);
		return {
			success: true,
			data: {
				collections: collectionStats,
				mediaCount,
				userCount,
				recentItems: await fetchRecentItems(db, collections)
			}
		};
	} catch (error) {
		console.error("Dashboard stats error:", error);
		return {
			success: false,
			error: {
				code: "DASHBOARD_STATS_ERROR",
				message: "Failed to load dashboard statistics"
			}
		};
	}
}
/**
* Fetch the 10 most recently updated items across all collections.
*
* Uses UNION ALL over each ec_* table. The query is safe because
* collection slugs come from the system table and are validated.
*
* `title` is not a standard column — it's a user-defined field. We query
* `_emdash_fields` to discover which collections have one and fall back
* to `slug` (which is always present) otherwise.
*/
async function fetchRecentItems(db, collections) {
	if (collections.length === 0) return [];
	const titleFields = await db.selectFrom("_emdash_fields as f").innerJoin("_emdash_collections as c", "c.id", "f.collection_id").select(["c.slug as collection_slug"]).where("f.slug", "=", "title").execute();
	const collectionsWithTitle = new Set(titleFields.map((r) => r.collection_slug));
	return (await Promise.all(collections.map(async (col) => {
		validateIdentifier(col.slug);
		const table = `ec_${col.slug}`;
		const titleExpr = collectionsWithTitle.has(col.slug) ? sql`COALESCE(title, slug, id)` : sql`COALESCE(slug, id)`;
		return (await sql`
				SELECT
					id,
					${sql.lit(col.slug)} AS collection,
					${sql.lit(col.label)} AS collection_label,
					${titleExpr} AS title,
					slug,
					status,
					updated_at,
					author_id
				FROM ${sql.ref(table)}
				WHERE deleted_at IS NULL
				ORDER BY updated_at DESC
				LIMIT 10
			`.execute(db)).rows;
	}))).flat().toSorted((a, b) => a.updated_at < b.updated_at ? 1 : a.updated_at > b.updated_at ? -1 : 0).slice(0, 10).map((row) => ({
		id: row.id,
		collection: row.collection,
		collectionLabel: row.collection_label,
		title: row.title,
		slug: row.slug,
		status: row.status,
		updatedAt: row.updated_at,
		authorId: row.author_id
	}));
}

//#endregion
export { handleDashboardStats as t };
//# sourceMappingURL=dashboard-EvEKF5en.mjs.map