/**
 * App settings database queries.
 *
 * Single-row table queries for global app configuration.
 *
 * @module
 */
/**
 * Load the current app settings.
 *
 * @param deps - query dependencies
 * @returns the app settings row
 * @throws Error if the singleton `app_settings` row is missing (migration drift — should not occur in practice)
 */
export const query_app_settings_load = async (deps) => {
    const row = await deps.db.query_one(`SELECT open_signup, updated_at, updated_by FROM app_settings WHERE id = 1`);
    if (!row) {
        throw new Error('app_settings row not found — migration may not have run');
    }
    return row;
};
/**
 * Load the current app settings with resolved updater username.
 *
 * @param deps - query dependencies
 * @returns the app settings with `updated_by_username`
 * @throws Error if the singleton `app_settings` row is missing
 */
export const query_app_settings_load_with_username = async (deps) => {
    const row = await deps.db.query_one(`SELECT s.open_signup, s.updated_at, s.updated_by, act.name AS updated_by_username
		 FROM app_settings s
		 LEFT JOIN actor act ON act.id = s.updated_by
		 WHERE s.id = 1`);
    if (!row) {
        throw new Error('app_settings row not found — migration may not have run');
    }
    return row;
};
/**
 * Update app settings and return the updated row.
 *
 * @param deps - query dependencies
 * @param open_signup - new value for the open_signup toggle
 * @param actor_id - the actor making the change
 * @returns the updated app settings row
 * @mutates `app_settings` row - sets `open_signup`, `updated_at`, and `updated_by`
 * @throws Error if the singleton `app_settings` row is missing
 */
export const query_app_settings_update = async (deps, open_signup, actor_id) => {
    const row = await deps.db.query_one(`UPDATE app_settings SET open_signup = $1, updated_at = NOW(), updated_by = $2 WHERE id = 1 RETURNING open_signup, updated_at, updated_by`, [open_signup, actor_id]);
    if (!row) {
        throw new Error('app_settings row not found — migration may not have run');
    }
    return row;
};
