// #region MaintenanceApi [C:3] [TYPE Module] [SEMANTICS api, maintenance, banners, settings, events]
// @BRIEF API client for Maintenance Banner endpoints — start/end events, settings, dashboard banners.
// @LAYER API
// @RELATION DEPENDS_ON -> [ApiModule]
// @PRE All calls go through requestApi wrapper.
// @POST Returns typed API responses for maintenance lifecycle operations.
// @ADR-0007 Uses $effect(() => subscribe(...)) pattern, NOT fromStore + $derived.

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

const BASE = '/maintenance';

// #region startMaintenance [C:2] [TYPE Function] [SEMANTICS maintenance, start, event]
// @BRIEF Start a maintenance event.
// @PRE params contains at minimum { tables, start_time }.
// @POST Returns { task_id, maintenance_id, status }.
// @SIDE_EFFECT Creates a maintenance event on the backend.
// @RELATION DEPENDS_ON -> [ApiModule.requestApi]
export async function startMaintenance<T = unknown>(params: Record<string, unknown>): Promise<T> {
	return requestApi<T>(`${BASE}/start`, 'POST', params);
}
// #endregion startMaintenance

// #region endMaintenance [C:2] [TYPE Function] [SEMANTICS maintenance, end, event]
// @BRIEF End a specific maintenance event.
// @PRE maintenanceId is a non-empty string.
// @POST Returns { task_id, status }.
// @SIDE_EFFECT Closes the specified maintenance event on the backend.
// @RELATION DEPENDS_ON -> [ApiModule.requestApi]
export async function endMaintenance<T = unknown>(maintenanceId: string): Promise<T> {
	return requestApi<T>(`${BASE}/${maintenanceId}/end`, 'POST');
}
// #endregion endMaintenance

// #region endAllMaintenance [C:2] [TYPE Function] [SEMANTICS maintenance, end-all, events]
// @BRIEF End all active maintenance events.
// @POST Returns { task_id, status }.
// @SIDE_EFFECT Closes all active maintenance events on the backend.
// @RELATION DEPENDS_ON -> [ApiModule.requestApi]
export async function endAllMaintenance<T = unknown>(): Promise<T> {
	return requestApi<T>(`${BASE}/end-all`, 'POST');
}
// #endregion endAllMaintenance

// #region getMaintenanceSettings [C:2] [TYPE Function] [SEMANTICS maintenance, settings, get]
// @BRIEF Get current maintenance settings.
// @POST Returns MaintenanceSettingsResponse.
// @RELATION DEPENDS_ON -> [ApiModule.requestApi]
export async function getSettings<T = unknown>(): Promise<T> {
	return requestApi<T>(`${BASE}/settings`, 'GET');
}
// #endregion getMaintenanceSettings

// #region updateSettings [C:2] [TYPE Function] [SEMANTICS maintenance, settings, update]
// @BRIEF Update maintenance settings.
// @PRE settings is a valid MaintenanceSettings payload.
// @POST Returns updated MaintenanceSettingsResponse.
// @SIDE_EFFECT Persists settings changes on the backend.
// @RELATION DEPENDS_ON -> [ApiModule.requestApi]
export async function updateSettings<T = unknown>(settings: Record<string, unknown>): Promise<T> {
	return requestApi<T>(`${BASE}/settings`, 'PUT', settings);
}
// #endregion updateSettings

// #region listEvents [C:2] [TYPE Function] [SEMANTICS maintenance, events, list]
// @BRIEF List active and completed maintenance events.
// @POST Returns { active: [...], completed: [...] }.
// @RELATION DEPENDS_ON -> [ApiModule.requestApi]
export async function listEvents<T = unknown>(): Promise<T> {
	return requestApi<T>(`${BASE}/events`, 'GET');
}
// #endregion listEvents

// #region listDashboardBanners [C:2] [TYPE Function] [SEMANTICS maintenance, dashboard, banners]
// @BRIEF List dashboard banner states for all dashboards.
// @POST Returns array of { dashboard_id, active, events, start_time, end_time }.
// @RELATION DEPENDS_ON -> [ApiModule.requestApi]
export async function listDashboardBanners<T = unknown>(): Promise<T> {
	return requestApi<T>(`${BASE}/dashboard-banners`, 'GET');
}
// #endregion listDashboardBanners
// #endregion MaintenanceApi
