import { getConfigValue } from '@brojs/cli';
import dayjs from 'dayjs';

enum ArmEndpoints {
  ORDERS = '/arm/orders',
  MASTERS = '/arm/masters',
}

const armService = () => {
  const endpoint = getConfigValue('dry-wash.api');

  const fetchOrders = async ({ date }: { date: Date }) => {
    const startDate = dayjs(date).startOf('day').toISOString();
    const endDate = dayjs(date).endOf('day').toISOString();

    const response = await fetch(`${endpoint}${ArmEndpoints.ORDERS}`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ startDate, endDate }),
    });

    if (!response.ok) {
      throw new Error(`Failed to fetch orders: ${response.status}`);
    }

    return await response.json();
  };

  const fetchMasters = async () => {
    const response = await fetch(`${endpoint}${ArmEndpoints.MASTERS}`);

    if (!response.ok) {
      throw new Error(`Failed to fetch masters: ${response.status}`);
    }

    return await response.json();
  };

  const addMaster = async ({
    name,
    phone,
  }: {
    name: string;
    phone: string;
  }) => {
    const response = await fetch(`${endpoint}${ArmEndpoints.MASTERS}`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ name, phone }),
    });

    if (!response.ok) {
      throw new Error(`Failed to fetch masters: ${response.status}`);
    }

    return await response.json();
  };

  const deleteMaster = async ({ id }: { id: string }) => {
    const response = await fetch(`${endpoint}${ArmEndpoints.MASTERS}/${id}`, {
      method: 'DELETE',
    });

    if (!response.ok) {
      throw new Error(`Failed to fetch masters: ${response.status}`);
    }

    return await response.json();
  };

  const updateOrders = async ({
    id,
    status,
    notes,
    masterId,
  }: {
    id: string;
    status?: string;
    notes?: string;
    masterId?: string;
  }) => {
    const body = JSON.stringify({ status, notes, masterId });

    const response = await fetch(`${endpoint}/orders/${id}`, {
      method: 'PATCH',
      headers: {
        'Content-Type': 'application/json',
      },
      body,
    });

    if (!response.ok) {
      throw new Error(`Failed to fetch update masters: ${response.status}`);
    }

    return await response.json();
  };

  const updateMaster = async ({
    id,
    name,
    phone,
  }: {
    id: string;
    name?: string;
    phone?: string;
  }) => {
    const body = JSON.stringify({ name, phone });

    const response = await fetch(`${endpoint}${ArmEndpoints.MASTERS}/${id}`, {
      method: 'PATCH',
      headers: {
        'Content-Type': 'application/json',
      },
      body,
    });

    if (!response.ok) {
      throw new Error(`Failed to fetch update masters: ${response.status}`);
    }

    return await response.json();
  };

  return {
    fetchOrders,
    fetchMasters,
    addMaster,
    deleteMaster,
    updateMaster,
    updateOrders,
  };
};

export { armService, ArmEndpoints };
