import { VITE_BACKEND_URL } from '$env/static/private';
import { endpoints } from '$lib/utils/api';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ fetch }) => {
	try {
		const [banner, celebrity, newArival, trending, testimonial] = await Promise.all([
			fetch(`${VITE_BACKEND_URL}${endpoints.category.banner}`, {
				method: 'GET',
				headers: {
					'Content-Type': 'application/json',
					Accept: 'application/json'
				}
			}),
			fetch(`${VITE_BACKEND_URL}${endpoints.product.celebrity}`, {
				method: 'GET',
				headers: {
					'Content-Type': 'application/json',
					Accept: 'application/json'
				}
			}),
			fetch(`${VITE_BACKEND_URL}${endpoints.product.newArrival}`, {
				method: 'GET',
				headers: {
					'Content-Type': 'application/json',
					Accept: 'application/json'
				}
			}),
			fetch(`${VITE_BACKEND_URL}${endpoints.product.trending}`, {
				method: 'GET',
				headers: {
					'Content-Type': 'application/json',
					Accept: 'application/json'
				}
			}),

			fetch(`${VITE_BACKEND_URL}${endpoints.testimonial.show}`, {
				method: 'GET',
				headers: {
					'Content-Type': 'application/json',
					Accept: 'application/json'
				}
			})
		]);

		if (!banner.ok) {
			return {
				status: 'error',
				message: 'Fetched failed!'
			};
		}

		return {
			banner: banner.json(),
			// celebrity: celebrity.json(),
			newArrival: newArival.json(),
			trending: trending.json(),
			testimonial: testimonial.json()
		};
	} catch (err) {
		return {
			status: 'error',
			message: err instanceof Error ? err?.message : 'Unknown error'
		};
	}
};
