/**
 * @ai-summary User settings facade for fuel-logs feature
 * @ai-context Reads user preferences (unit system, currency, timezone) from app DB
 */

import { UserPreferencesRepository } from '../../../core/user-preferences/data/user-preferences.repository';
import pool from '../../../core/config/database';
import { UnitSystem } from '../../../core/user-preferences/user-preferences.types';

export interface UserSettings {
  unitSystem: UnitSystem;
  currencyCode: string;
  timeZone: string;
}

export class UserSettingsService {
  private static repo = new UserPreferencesRepository(pool);

  static async getUserSettings(userId: string): Promise<UserSettings> {
    const existing = await this.repo.findByUserId(userId);
    if (existing) {
      return {
        unitSystem: existing.unitSystem,
        currencyCode: existing.currencyCode || 'USD',
        timeZone: existing.timeZone || 'UTC',
      };
    }
    // Upsert with sensible defaults if missing
    const created = await this.repo.upsert({ userId, unitSystem: 'imperial' });
    return {
      unitSystem: created.unitSystem,
      currencyCode: created.currencyCode || 'USD',
      timeZone: created.timeZone || 'UTC',
    };
  }
}

