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

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

	return centrawarungJson({
		success: true,
		name: 'CentraWarung API',
		version: '1.0',
		read_only: true,
		authentication: {
			type: 'api_key',
			headers: ['Authorization: Bearer <CENTRAWARUNG_API_KEY>', 'X-API-Key: <CENTRAWARUNG_API_KEY>']
		},
		endpoints: [
			{
				method: 'GET',
				path: '/api/centrawarung/warung',
				description: 'Daftar warung (paginated)',
				filters: ['q', 'kota', 'kategori', 'member_id', 'is_aktif', 'is_publik', 'page', 'limit']
			},
			{
				method: 'GET',
				path: '/api/centrawarung/warung/{id}',
				description: 'Detail warung berdasarkan ID'
			},
			{
				method: 'GET',
				path: '/api/centrawarung/backoffice-user',
				description: 'Daftar user backoffice tanpa password',
				filters: ['q', 'role', 'is_aktif', 'page', 'limit']
			},
			{
				method: 'GET',
				path: '/api/centrawarung/backoffice-user/{id}',
				description: 'Detail user backoffice berdasarkan ID'
			},
			{
				method: 'GET',
				path: '/api/centrawarung/ref-kategori-warung',
				description: 'Daftar referensi kategori warung',
				filters: ['is_aktif', 'page', 'limit']
			},
			{
				method: 'GET',
				path: '/api/centrawarung/ref-kategori-warung/{id}',
				description: 'Detail kategori warung berdasarkan ID'
			},
			{
				method: 'GET',
				path: '/api/centrawarung/tbl-kodepos',
				description: 'Daftar data kodepos',
				filters: ['provinsi', 'kabupaten', 'kecamatan', 'kodepos', 'q', 'page', 'limit']
			},
			{
				method: 'GET',
				path: '/api/centrawarung/tbl-kodepos/{id}',
				description: 'Detail kodepos berdasarkan ID'
			},
			{
				method: 'GET',
				path: '/api/centrawarung/provinsi',
				description: 'Daftar provinsi dari tbl_kodepos'
			},
			{
				method: 'GET',
				path: '/api/centrawarung/kota',
				description: 'Daftar kota/kabupaten berdasarkan provinsi',
				filters: ['provinsi']
			},
			{
				method: 'GET',
				path: '/api/centrawarung/kecamatan',
				description: 'Daftar kecamatan berdasarkan provinsi dan kabupaten',
				filters: ['provinsi', 'kabupaten', 'kota']
			},
			{
				method: 'GET',
				path: '/api/centrawarung/kelurahan',
				description: 'Daftar kelurahan dan kodepos berdasarkan wilayah',
				filters: ['provinsi', 'kabupaten', 'kota', 'kecamatan']
			}
		]
	});
};

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