import { EntityType } from '../types/CalendarTypes';
import { ViewConfig } from '../core/ViewConfig';
import { IApiRepository } from './IApiRepository';

export class MockViewConfigRepository implements IApiRepository<ViewConfig> {
  public readonly entityType: EntityType = 'ViewConfig';
  private readonly dataUrl = 'data/viewconfigs.json';

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

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

      const rawData = await response.json();
      // Ensure syncStatus is set on each config
      const configs: ViewConfig[] = rawData.map((config: ViewConfig) => ({
        ...config,
        syncStatus: config.syncStatus || 'synced'
      }));
      return configs;
    } catch (error) {
      console.error('Failed to load viewconfigs:', error);
      throw error;
    }
  }

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

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

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