import { IDepartment, EntityType } from '../types/CalendarTypes';
import { IApiRepository } from './IApiRepository';

interface RawDepartmentData {
  id: string;
  name: string;
  resourceIds: string[];
  syncStatus?: string;
  [key: string]: unknown;
}

/**
 * MockDepartmentRepository - Loads department data from local JSON file
 */
export class MockDepartmentRepository implements IApiRepository<IDepartment> {
  public readonly entityType: EntityType = 'Department';
  private readonly dataUrl = 'data/mock-departments.json';

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

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

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

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

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

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

  private processDepartmentData(data: RawDepartmentData[]): IDepartment[] {
    return data.map((dept): IDepartment => ({
      id: dept.id,
      name: dept.name,
      resourceIds: dept.resourceIds,
      syncStatus: 'synced' as const
    }));
  }
}
