/**
 * Settings + Groups + Themes RPC handlers.
 */

import type { SettingsDb } from "../settings-db.ts";

export function createSettingsHandlers(settingsDb: SettingsDb) {
  return {
    "settings.get": ({ key }: { key: string }) => {
      try {
        return { value: settingsDb.getSetting(key) };
      } catch (err) {
        console.error("[settings.get]", err);
        return { value: null };
      }
    },

    "settings.set": ({ key, value }: { key: string; value: string }) => {
      try {
        settingsDb.setSetting(key, value);
        return { ok: true };
      } catch (err) {
        console.error("[settings.set]", err);
        return { ok: false };
      }
    },

    "settings.getAll": () => {
      try {
        return { settings: settingsDb.getAll() };
      } catch (err) {
        console.error("[settings.getAll]", err);
        return { settings: {} };
      }
    },

    "settings.getProjects": () => {
      try {
        const projects = settingsDb.listProjects().map((p: Record<string, unknown>) => ({
          id: p.id,
          config: JSON.stringify(p),
        }));
        return { projects };
      } catch (err) {
        console.error("[settings.getProjects]", err);
        return { projects: [] };
      }
    },

    "settings.setProject": ({ id, config }: { id: string; config: string }) => {
      try {
        const parsed = JSON.parse(config);
        settingsDb.setProject(id, { id, ...parsed });
        return { ok: true };
      } catch (err) {
        console.error("[settings.setProject]", err);
        return { ok: false };
      }
    },

    "settings.deleteProject": ({ id }: { id: string }) => {
      try {
        settingsDb.deleteProject(id);
        return { ok: true };
      } catch (err) {
        console.error("[settings.deleteProject]", err);
        return { ok: false };
      }
    },

    // Groups
    "groups.list": () => {
      try {
        return { groups: settingsDb.listGroups() };
      } catch (err) {
        console.error("[groups.list]", err);
        return { groups: [] };
      }
    },

    "groups.create": ({ id, name, icon, position }: { id: string; name: string; icon: string; position: number }) => {
      try {
        settingsDb.createGroup(id, name, icon, position);
        return { ok: true };
      } catch (err) {
        console.error("[groups.create]", err);
        return { ok: false };
      }
    },

    "groups.delete": ({ id }: { id: string }) => {
      try {
        settingsDb.deleteGroup(id);
        return { ok: true };
      } catch (err) {
        console.error("[groups.delete]", err);
        return { ok: false };
      }
    },

    // Custom themes
    "themes.getCustom": () => {
      try {
        return { themes: settingsDb.getCustomThemes() };
      } catch (err) {
        console.error("[themes.getCustom]", err);
        return { themes: [] };
      }
    },

    "themes.saveCustom": ({ id, name, palette }: { id: string; name: string; palette: Record<string, string> }) => {
      try {
        settingsDb.saveCustomTheme(id, name, palette);
        return { ok: true };
      } catch (err) {
        console.error("[themes.saveCustom]", err);
        return { ok: false };
      }
    },

    "themes.deleteCustom": ({ id }: { id: string }) => {
      try {
        settingsDb.deleteCustomTheme(id);
        return { ok: true };
      } catch (err) {
        console.error("[themes.deleteCustom]", err);
        return { ok: false };
      }
    },
  };
}
