import { api } from '$lib/api';

export interface AppSettingsRequest {
	tenant_id?: number | null;
	company_id?: number | null;
	settings: Record<string, any>;
}

export const appSettingsApi = {
	/**
	 * Resolves settings merging Global -> Tenant -> Company hierarchy
	 */
	async getResolved(tenantId: number, companyId: number): Promise<Record<string, any>> {
		const res = await api.get<Record<string, any>>(`/v1/a76/app-settings/resolved?tenant_id=${tenantId}&company_id=${companyId}`);
		if (res.error) throw new Error(res.error);
		return res.data || {};
	},

	/**
	 * Upserts an override at a specific level
	 */
	async upsert(payload: AppSettingsRequest): Promise<any> {
		const res = await api.post<any>(`/v1/a76/app-settings/upsert`, payload);
		if (res.error) throw new Error(res.error);
		return res.data;
	}
};
