import { c as createServerFn } from "./createServerFn-CIHAFgYl.mjs";
import { t as requireSupabaseAuth } from "./auth-middleware-tiFBA43n.mjs";
import { a as objectType, i as numberType, n as enumType, o as stringType, t as booleanType } from "../_libs/zod.mjs";
import { t as createServerRpc } from "./createServerRpc-B90ckaqP.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/admin.functions-7IoMXH8f.js
/** If no admin exists yet, grant admin role to the current signed-in user. */
var bootstrapAdminIfNone_createServerFn_handler = createServerRpc({
	id: "4ace4570f991c075b5a867d81119d2f45921163af898eb5e7e5fc80a3d7b8e98",
	name: "bootstrapAdminIfNone",
	filename: "src/lib/admin.functions.ts"
}, (opts) => bootstrapAdminIfNone.__executeServer(opts));
var bootstrapAdminIfNone = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]).handler(bootstrapAdminIfNone_createServerFn_handler, async ({ context }) => {
	const { data, error } = await context.supabase.rpc("bootstrap_admin_if_none");
	if (error) throw error;
	return {
		granted: !!data,
		reason: data ? null : "already_exists"
	};
});
var isCurrentUserAdmin_createServerFn_handler = createServerRpc({
	id: "a45e00ad593665a6a088648f7fc61e2ccc9c10c96e673c43a1b3b060a647427c",
	name: "isCurrentUserAdmin",
	filename: "src/lib/admin.functions.ts"
}, (opts) => isCurrentUserAdmin.__executeServer(opts));
var isCurrentUserAdmin = createServerFn({ method: "GET" }).middleware([requireSupabaseAuth]).handler(isCurrentUserAdmin_createServerFn_handler, async ({ context }) => {
	const { data } = await context.supabase.from("user_roles").select("role").eq("user_id", context.userId).eq("role", "admin").maybeSingle();
	return { isAdmin: !!data };
});
var updateServiceInput = objectType({
	id: stringType().uuid(),
	price: numberType().nullable(),
	duration_minutes: numberType().int().nullable(),
	active: booleanType().optional()
});
var updateService_createServerFn_handler = createServerRpc({
	id: "f794c7dd65be4ac125cb831bfc785502112fd4fdd11439dba579bf1be06cedee",
	name: "updateService",
	filename: "src/lib/admin.functions.ts"
}, (opts) => updateService.__executeServer(opts));
var updateService = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]).inputValidator((d) => updateServiceInput.parse(d)).handler(updateService_createServerFn_handler, async ({ data, context }) => {
	const { error } = await context.supabase.from("services").update({
		price: data.price,
		duration_minutes: data.duration_minutes,
		active: data.active ?? true
	}).eq("id", data.id);
	if (error) throw error;
	return { ok: true };
});
var setStatusInput = objectType({
	id: stringType().uuid(),
	status: enumType([
		"scheduled",
		"completed",
		"cancelled",
		"no_show"
	])
});
var setAppointmentStatus_createServerFn_handler = createServerRpc({
	id: "48c24c547c97a5aad30561af9b7aef26828854e5b44aca5eff77d783954ec677",
	name: "setAppointmentStatus",
	filename: "src/lib/admin.functions.ts"
}, (opts) => setAppointmentStatus.__executeServer(opts));
var setAppointmentStatus = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]).inputValidator((d) => setStatusInput.parse(d)).handler(setAppointmentStatus_createServerFn_handler, async ({ data, context }) => {
	const { error } = await context.supabase.from("appointments").update({ status: data.status }).eq("id", data.id);
	if (error) throw error;
	return { ok: true };
});
var addBlockInput = objectType({
	providerId: stringType().uuid(),
	startTime: stringType(),
	endTime: stringType(),
	reason: stringType().max(200).optional().nullable()
});
var addBlockedTime_createServerFn_handler = createServerRpc({
	id: "c4cd22d504e1738dc016662e8e21329284c5ff9140ec42dfac61b3fb498cb7bb",
	name: "addBlockedTime",
	filename: "src/lib/admin.functions.ts"
}, (opts) => addBlockedTime.__executeServer(opts));
var addBlockedTime = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]).inputValidator((d) => addBlockInput.parse(d)).handler(addBlockedTime_createServerFn_handler, async ({ data, context }) => {
	const { error } = await context.supabase.from("blocked_times").insert({
		provider_id: data.providerId,
		start_time: data.startTime,
		end_time: data.endTime,
		reason: data.reason ?? null
	});
	if (error) throw error;
	return { ok: true };
});
var TIME_RE = /^\d{2}:\d{2}$/;
var addAvailabilityInput = objectType({
	providerId: stringType().uuid(),
	date: stringType().regex(/^\d{4}-\d{2}-\d{2}$/),
	startTime: stringType().regex(TIME_RE),
	endTime: stringType().regex(TIME_RE)
});
var addAvailability_createServerFn_handler = createServerRpc({
	id: "02666ce88815bf1e5d6434665392e5d9b3e21037eff361349270bc822222f26f",
	name: "addAvailability",
	filename: "src/lib/admin.functions.ts"
}, (opts) => addAvailability.__executeServer(opts));
var addAvailability = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]).inputValidator((d) => addAvailabilityInput.parse(d)).handler(addAvailability_createServerFn_handler, async ({ data, context }) => {
	if (data.endTime <= data.startTime) throw new Error("Kraj mora biti posle početka");
	const { error } = await context.supabase.from("availability").insert({
		provider_id: data.providerId,
		date: data.date,
		start_time: data.startTime,
		end_time: data.endTime
	});
	if (error) throw error;
	return { ok: true };
});
var deleteAvailabilityInput = objectType({ id: stringType().uuid() });
var deleteAvailability_createServerFn_handler = createServerRpc({
	id: "e224b0438c173aac6a36788a6aeaf80642baad92223f33cd8e2671499e6a8b68",
	name: "deleteAvailability",
	filename: "src/lib/admin.functions.ts"
}, (opts) => deleteAvailability.__executeServer(opts));
var deleteAvailability = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]).inputValidator((d) => deleteAvailabilityInput.parse(d)).handler(deleteAvailability_createServerFn_handler, async ({ data, context }) => {
	const { error } = await context.supabase.from("availability").delete().eq("id", data.id);
	if (error) throw error;
	return { ok: true };
});
var settingInput = objectType({
	key: stringType().min(1),
	value: stringType()
});
var upsertSetting_createServerFn_handler = createServerRpc({
	id: "9ec7f3a618f0bbb27900e9a7a04c06a55fad47bdfb9b546adea1038e8572761f",
	name: "upsertSetting",
	filename: "src/lib/admin.functions.ts"
}, (opts) => upsertSetting.__executeServer(opts));
var upsertSetting = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]).inputValidator((d) => settingInput.parse(d)).handler(upsertSetting_createServerFn_handler, async ({ data, context }) => {
	const { error } = await context.supabase.from("site_settings").upsert({
		key: data.key,
		value: data.value,
		updated_at: (/* @__PURE__ */ new Date()).toISOString()
	}, { onConflict: "key" });
	if (error) throw error;
	return { ok: true };
});
var manualApptInput = objectType({
	providerId: stringType().uuid(),
	serviceId: stringType().uuid(),
	startTime: stringType(),
	clientName: stringType().min(1).max(100),
	clientPhone: stringType().min(1).max(30),
	note: stringType().max(500).optional().nullable()
});
var adminCreateAppointment_createServerFn_handler = createServerRpc({
	id: "7d21fc5777ec7133c2f3f295ae307a75db3300be962f7ff1689c178f7f77a9ba",
	name: "adminCreateAppointment",
	filename: "src/lib/admin.functions.ts"
}, (opts) => adminCreateAppointment.__executeServer(opts));
var adminCreateAppointment = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]).inputValidator((d) => manualApptInput.parse(d)).handler(adminCreateAppointment_createServerFn_handler, async ({ data, context }) => {
	const { data: svc } = await context.supabase.from("services").select("duration_minutes").eq("id", data.serviceId).maybeSingle();
	const duration = svc?.duration_minutes ?? 30;
	const start = new Date(data.startTime);
	const end = new Date(start.getTime() + duration * 6e4);
	const { error } = await context.supabase.from("appointments").insert({
		provider_id: data.providerId,
		service_id: data.serviceId,
		client_name: data.clientName,
		client_phone: data.clientPhone,
		note: data.note ?? null,
		start_time: start.toISOString(),
		end_time: end.toISOString(),
		status: "scheduled"
	});
	if (error) throw error;
	return { ok: true };
});
//#endregion
export { addAvailability_createServerFn_handler, addBlockedTime_createServerFn_handler, adminCreateAppointment_createServerFn_handler, bootstrapAdminIfNone_createServerFn_handler, deleteAvailability_createServerFn_handler, isCurrentUserAdmin_createServerFn_handler, setAppointmentStatus_createServerFn_handler, updateService_createServerFn_handler, upsertSetting_createServerFn_handler };
