import { EntityType } from '../types/CalendarTypes';
import { TenantSetting } from '../types/SettingsTypes';
import { IApiRepository } from './IApiRepository';

/**
 * MockSettingsRepository - Loads tenant settings from local JSON file
 *
 * Settings are stored as separate records per section (workweek, grid, etc.).
 * The JSON file is already an array of TenantSetting records.
 */
export class MockSettingsRepository implements IApiRepository<TenantSetting> {
  public readonly entityType: EntityType = 'Settings';
  private readonly dataUrl = 'data/tenant-settings.json';

  public async fetchAll(): Promise<TenantSetting[]> {
    try {
      const response = await fetch(this.dataUrl);

      if (!response.ok) {
        throw new Error(`Failed to load tenant settings: ${response.status} ${response.statusText}`);
      }

      const settings: TenantSetting[] = await response.json();
      // Ensure syncStatus is set on each record
      return settings.map(s => ({
        ...s,
        syncStatus: s.syncStatus || 'synced'
      }));
    } catch (error) {
      console.error('Failed to load tenant settings:', error);
      throw error;
    }
  }

  public async sendCreate(_settings: TenantSetting): Promise<TenantSetting> {
    throw new Error('MockSettingsRepository does not support sendCreate. Mock data is read-only.');
  }

  public async sendUpdate(_id: string, _updates: Partial<TenantSetting>): Promise<TenantSetting> {
    throw new Error('MockSettingsRepository does not support sendUpdate. Mock data is read-only.');
  }

  public async sendDelete(_id: string): Promise<void> {
    throw new Error('MockSettingsRepository does not support sendDelete. Mock data is read-only.');
  }
}
