import { IResource, ResourceType, EntityType } from '../types/CalendarTypes';
import { IApiRepository } from './IApiRepository';
import { IWeekSchedule } from '../types/ScheduleTypes';

interface RawResourceData {
  id: string;
  name: string;
  displayName: string;
  type: string;
  avatarUrl?: string;
  color?: string;
  isActive?: boolean;
  defaultSchedule?: IWeekSchedule;
  metadata?: Record<string, unknown>;
}

/**
 * MockResourceRepository - Loads resource data from local JSON file
 */
export class MockResourceRepository implements IApiRepository<IResource> {
  public readonly entityType: EntityType = 'Resource';
  private readonly dataUrl = 'data/mock-resources.json';

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

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

      const rawData: RawResourceData[] = await response.json();
      return this.processResourceData(rawData);
    } catch (error) {
      console.error('Failed to load resource data:', error);
      throw error;
    }
  }

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

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

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

  private processResourceData(data: RawResourceData[]): IResource[] {
    return data.map((resource): IResource => ({
      id: resource.id,
      name: resource.name,
      displayName: resource.displayName,
      type: resource.type as ResourceType,
      avatarUrl: resource.avatarUrl,
      color: resource.color,
      isActive: resource.isActive,
      defaultSchedule: resource.defaultSchedule,
      metadata: resource.metadata,
      syncStatus: 'synced' as const
    }));
  }
}
