import type { RequestHandler } from './$types';
import {
	centrawarungJson,
	centrawarungNotFound,
	centrawarungOptionsResponse,
	centrawarungServerError,
	getRefKategoriWarungByIdForCentrawarung,
	guardCentrawarungGet
} from '$lib/server/centrawarung-api';

export const GET: RequestHandler = async (event) => {
	const authError = await guardCentrawarungGet(event);
	if (authError) return authError;

	try {
		const id = Number(event.params.id);
		if (!Number.isFinite(id) || id <= 0) {
			return centrawarungNotFound('ID kategori tidak valid.');
		}

		const data = await getRefKategoriWarungByIdForCentrawarung(id);
		if (!data) return centrawarungNotFound('Kategori warung tidak ditemukan.');
		return centrawarungJson({ success: true, data });
	} catch {
		return centrawarungServerError('Gagal mengambil data kategori warung.');
	}
};

export const OPTIONS: RequestHandler = async () => centrawarungOptionsResponse();
