import axios from 'axios';

import { BaseApiService } from '@/services/api/base';
import {
  Closing,
  ClosingFinance,
  ClosingGeneralInformation,
  ClosingIncomingSapronak,
  ClosingOutgoingSapronak,
  ClosingOverhead,
  ClosingSapronakCalculation,
  ClosingProductionData,
  ClosingHppExpedition,
  ClosingIncomingSapronakSummary,
  ClosingOutgoingSapronakSummary,
} from '@/types/api/closing';
import { BaseApiResponse } from '@/types/api/api-general';
import { httpClient, httpClientFetcher } from '@/services/http/client';
import { ClosingSales } from '@/types/api/closing';
export class ClosingApiService extends BaseApiService<Closing, null, null> {
  constructor(basePath: string) {
    super(basePath);
  }

  async getPenjualan(
    id: number
  ): Promise<BaseApiResponse<ClosingSales> | undefined> {
    try {
      const getPenjualanPath = `${this.basePath}/${id}/penjualan`;
      const getPenjualanRes =
        await httpClient<BaseApiResponse<ClosingSales>>(getPenjualanPath);

      return getPenjualanRes;
    } catch (error) {
      if (axios.isAxiosError<BaseApiResponse<ClosingSales>>(error)) {
        return error.response?.data;
      }
      return undefined;
    }
  }

  async getPenjualanByKandang(
    closingId: number,
    kandangId: number
  ): Promise<BaseApiResponse<ClosingSales> | undefined> {
    try {
      const path = `${this.basePath}/${closingId}/${kandangId}/penjualan`;
      return await httpClient<BaseApiResponse<ClosingSales>>(path, {
        method: 'GET',
      });
    } catch (error) {
      if (axios.isAxiosError<BaseApiResponse<ClosingSales>>(error)) {
        return error.response?.data;
      }
      return undefined;
    }
  }

  async getAllIncomingSapronakFetcher(
    endpoint: string
  ): Promise<BaseApiResponse<ClosingIncomingSapronak[]>> {
    return await httpClientFetcher<BaseApiResponse<ClosingIncomingSapronak[]>>(
      endpoint
    );
  }

  async getAllIncomingSapronakSummaryFetcher(
    endpoint: string
  ): Promise<BaseApiResponse<ClosingIncomingSapronakSummary[]>> {
    return await httpClientFetcher<
      BaseApiResponse<ClosingIncomingSapronakSummary[]>
    >(endpoint);
  }

  async getAllOutgoingSapronakFetcher(
    endpoint: string
  ): Promise<BaseApiResponse<ClosingOutgoingSapronak[]>> {
    return await httpClientFetcher<BaseApiResponse<ClosingOutgoingSapronak[]>>(
      endpoint
    );
  }

  async getAllOutgoingSapronakSummaryFetcher(
    endpoint: string
  ): Promise<BaseApiResponse<ClosingOutgoingSapronakSummary[]>> {
    return await httpClientFetcher<
      BaseApiResponse<ClosingOutgoingSapronakSummary[]>
    >(endpoint);
  }

  async getGeneralInfo(
    id: number
  ): Promise<BaseApiResponse<ClosingGeneralInformation> | undefined> {
    try {
      const getGeneralInfoPath = `${this.basePath}/${id}`;
      const getGeneralInfoRes =
        await httpClient<BaseApiResponse<ClosingGeneralInformation>>(
          getGeneralInfoPath
        );
      return getGeneralInfoRes;
    } catch (error) {
      if (
        axios.isAxiosError<BaseApiResponse<ClosingGeneralInformation>>(error)
      ) {
        return error.response?.data;
      }
      return undefined;
    }
  }

  async getProductionData(
    id: number,
    kandangId?: number
  ): Promise<BaseApiResponse<ClosingProductionData> | undefined> {
    try {
      const getProductionDataPath = `${this.basePath}/${id}/production-data?kandang_id=${kandangId ? `${kandangId}` : ''}`;
      const getProductionDataRes = await httpClient<
        BaseApiResponse<ClosingProductionData>
      >(getProductionDataPath);

      return getProductionDataRes;
    } catch (error) {
      if (axios.isAxiosError<BaseApiResponse<ClosingProductionData>>(error)) {
        return error.response?.data;
      }
      return undefined;
    }
  }

  async getPerhitunganSapronak(
    id: number,
    projectKandangId?: number
  ): Promise<BaseApiResponse<ClosingSapronakCalculation> | undefined> {
    try {
      const path = `${this.basePath}/${id}${projectKandangId ? `/${projectKandangId}` : ''}/perhitungan_sapronak`;
      return await httpClient<BaseApiResponse<ClosingSapronakCalculation>>(
        path,
        {
          method: 'GET',
        }
      );
    } catch (error) {
      if (
        axios.isAxiosError<BaseApiResponse<ClosingSapronakCalculation>>(error)
      ) {
        return error.response?.data;
      }
      return undefined;
    }
  }

  async getOverhead(
    id: number,
    kandangId?: number
  ): Promise<BaseApiResponse<ClosingOverhead> | undefined> {
    try {
      const path = `${this.basePath}/${id}${kandangId ? `/${kandangId}` : ''}/overhead`;
      return await httpClient<BaseApiResponse<ClosingOverhead>>(path, {
        method: 'GET',
      });
    } catch (error) {
      if (axios.isAxiosError<BaseApiResponse<ClosingOverhead>>(error)) {
        return error.response?.data;
      }
      return undefined;
    }
  }

  async getFinance(
    id: number,
    kandangId?: number
  ): Promise<BaseApiResponse<ClosingFinance> | undefined> {
    try {
      const path = `${this.basePath}/${id}${kandangId ? `/${kandangId}` : ''}/keuangan`;
      return await httpClient<BaseApiResponse<ClosingFinance>>(path, {
        method: 'GET',
      });
    } catch (error) {
      if (axios.isAxiosError<BaseApiResponse<ClosingFinance>>(error)) {
        return error.response?.data;
      }
      return undefined;
    }
  }

  async getHppEkspedisi(
    id: number
  ): Promise<BaseApiResponse<ClosingHppExpedition> | undefined> {
    try {
      const getHppEkspedisiPath = `${this.basePath}/${id}/expedition-hpp`;
      const getHppEkspedisiRes =
        await httpClient<BaseApiResponse<ClosingHppExpedition>>(
          getHppEkspedisiPath
        );
      return getHppEkspedisiRes;
    } catch (error) {
      if (axios.isAxiosError<BaseApiResponse<ClosingHppExpedition>>(error)) {
        return error.response?.data;
      }
      return undefined;
    }
  }

  async getHppEkspedisiByKandang(
    closingId: number,
    kandangId: number
  ): Promise<BaseApiResponse<ClosingHppExpedition> | undefined> {
    try {
      const path = `${this.basePath}/${closingId}/${kandangId}/expedition-hpp`;
      return await httpClient<BaseApiResponse<ClosingHppExpedition>>(path, {
        method: 'GET',
      });
    } catch (error) {
      if (axios.isAxiosError<BaseApiResponse<ClosingHppExpedition>>(error)) {
        return error.response?.data;
      }
      return undefined;
    }
  }
}

export const ClosingApi = new ClosingApiService('/closings');
