import { BaseApiService } from '@/services/api/base';
import { httpClient } from '@/services/http/client';
import { formatDate } from '@/lib/helper';
import { BaseApiResponse } from '@/types/api/api-general';
import { DebtSupplier } from '@/types/api/report/debt-supplier';

export class DebtSupplierApiService extends BaseApiService<
  DebtSupplier,
  unknown,
  unknown
> {
  constructor(basePath: string) {
    super(basePath);
  }

  async exportToExcelGeneral(
    supplier_ids?: string,
    filter_by?: string,
    start_date?: string,
    end_date?: string
  ) {
    const params = new URLSearchParams();

    if (supplier_ids) params.set('supplier_ids', supplier_ids);
    if (filter_by) params.set('filter_by', filter_by);
    if (start_date) params.set('start_date', start_date);
    if (end_date) params.set('end_date', end_date);
    params.set('export', 'excel-all');
    params.set('page', '1');
    params.set('limit', '99999999999');

    const queryString = `?${params.toString()}`;

    const res = await httpClient<Blob>(
      `${this.basePath.replace(/\/$/, '')}/debt-supplier${queryString}`,
      {
        method: 'GET',
        responseType: 'blob',
      }
    );

    const url = window.URL.createObjectURL(new Blob([res]));
    const link = document.createElement('a');
    link.href = url;

    const fileName = `laporan-hutang-supplier-general-${formatDate(Date.now(), 'DD-MM-YYYY')}.xlsx`;
    link.setAttribute('download', fileName);

    document.body.appendChild(link);
    link.click();
    link.remove();
  }

  async exportToExcelSupplierPerSheet(
    supplier_ids?: string,
    filter_by?: string,
    start_date?: string,
    end_date?: string
  ) {
    const params = new URLSearchParams();

    if (supplier_ids) params.set('supplier_ids', supplier_ids);
    if (filter_by) params.set('filter_by', filter_by);
    if (start_date) params.set('start_date', start_date);
    if (end_date) params.set('end_date', end_date);
    params.set('export', 'excel');
    params.set('page', '1');
    params.set('limit', '99999999999');

    const queryString = `?${params.toString()}`;

    const res = await httpClient<Blob>(
      `${this.basePath.replace(/\/$/, '')}/debt-supplier${queryString}`,
      {
        method: 'GET',
        responseType: 'blob',
      }
    );

    const url = window.URL.createObjectURL(new Blob([res]));
    const link = document.createElement('a');
    link.href = url;

    const fileName = `laporan-hutang-supplier-per-sheet-${formatDate(Date.now(), 'DD-MM-YYYY')}.xlsx`;
    link.setAttribute('download', fileName);

    document.body.appendChild(link);
    link.click();
    link.remove();
  }

  async getDebtSupplierReport(
    supplier_ids?: string,
    filter_by?: string,
    start_date?: string,
    end_date?: string,
    page?: number,
    limit?: number
  ): Promise<BaseApiResponse<DebtSupplier[]> | undefined> {
    return await this.customRequest<BaseApiResponse<DebtSupplier[]>>(
      `debt-supplier`,
      {
        method: 'GET',
        params: {
          supplier_ids: supplier_ids,
          filter_by: filter_by,
          start_date: start_date,
          end_date: end_date,
          page: page,
          limit: limit,
        },
      }
    );
  }
}

export const DebtSupplierApi = new DebtSupplierApiService('reports');
