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

interface RawCustomerData {
  id: string;
  name: string;
  phone: string;
  email?: string;
  metadata?: Record<string, unknown>;
  [key: string]: unknown;
}

/**
 * MockCustomerRepository - Loads customer data from local JSON file
 */
export class MockCustomerRepository implements IApiRepository<ICustomer> {
  public readonly entityType: EntityType = 'Customer';
  private readonly dataUrl = 'data/mock-customers.json';

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

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

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

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

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

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

  private processCustomerData(data: RawCustomerData[]): ICustomer[] {
    return data.map((customer): ICustomer => ({
      id: customer.id,
      name: customer.name,
      phone: customer.phone,
      email: customer.email,
      metadata: customer.metadata,
      syncStatus: 'synced' as const
    }));
  }
}
