import api from './api';

const SETTING_KEYS = [
  'enrich_metadata',
  'enrich_cover_art',
  'enrich_genres',
  'enrich_lyrics',
  'enrich_artist_similarity',
  'enrich_audio_analysis',
] as const;

export type EnrichSettingKey = typeof SETTING_KEYS[number];

export interface EnrichSettings {
  enrich_metadata: boolean;
  enrich_cover_art: boolean;
  enrich_genres: boolean;
  enrich_lyrics: boolean;
  enrich_artist_similarity: boolean;
  enrich_audio_analysis: boolean;
}

const DEFAULTS: EnrichSettings = {
  enrich_metadata: true,
  enrich_cover_art: true,
  enrich_genres: true,
  enrich_lyrics: true,
  enrich_artist_similarity: true,
  enrich_audio_analysis: false,
};

export const settingsService = {
  async load(): Promise<EnrichSettings> {
    const res = await api.get<Record<string, string>>('/settings');
    const raw = res.data;
    const out = { ...DEFAULTS };
    for (const key of SETTING_KEYS) {
      if (raw[key] !== undefined) {
        (out as any)[key] = raw[key] === 'true';
      }
    }
    return out;
  },

  async update(key: EnrichSettingKey, value: boolean): Promise<void> {
    await api.put(`/settings/${key}`, { value: String(value) });
  },
};
