import { BaseApiService } from '@/services/api/base';
import { BaseApiResponse } from '@/types/api/api-general';
import { CustomerPaymentReport } from '@/types/api/report/customer-payment';
import { httpClient } from '@/services/http/client';
import { formatDate } from '@/lib/helper';
import { BalanceMonitoringRow } from '@/types/api/report/balance-monitoring';

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

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

    if (customer_ids) params.set('customer_ids', customer_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', '9999999999');

    const res = await httpClient<Blob>(
      `${this.basePath}/customer-payment?${params.toString()}`,
      {
        method: 'GET',
        responseType: 'blob',
      }
    );

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

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

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

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

    if (customer_ids) params.set('customer_ids', customer_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', '9999999999');

    const res = await httpClient<Blob>(
      `${this.basePath}/customer-payment?${params.toString()}`,
      {
        method: 'GET',
        responseType: 'blob',
      }
    );

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

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

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

  async exportBalanceMonitoringToExcel(
    customer_ids?: string,
    sales_ids?: string,
    filter_by?: string,
    start_date?: string,
    end_date?: string
  ) {
    const params = new URLSearchParams();
    if (customer_ids) params.set('customer_ids', customer_ids);
    if (sales_ids) params.set('sales_ids', sales_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', '9999999999');

    const res = await httpClient<Blob>(
      `${this.basePath}/balance-monitoring?${params.toString()}`,
      { method: 'GET', responseType: 'blob' }
    );

    const url = window.URL.createObjectURL(new Blob([res]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute(
      'download',
      `laporan-balance-monitoring-${formatDate(Date.now(), 'DD-MM-YYYY')}.xlsx`
    );
    document.body.appendChild(link);
    link.click();
    link.remove();
  }

  async getBalanceMonitoringReport(params: {
    start_date?: string;
    end_date?: string;
    customer_ids?: string;
    sales_ids?: string;
    filter_by?: string;
    sort_by?: string;
    sort_order?: string;
    page?: number;
    limit?: number;
  }): Promise<BaseApiResponse<BalanceMonitoringRow[]> | undefined> {
    return await this.customRequest<BaseApiResponse<BalanceMonitoringRow[]>>(
      'balance-monitoring',
      { method: 'GET', params }
    );
  }

  async getCustomerPaymentReport(
    customer_ids?: string,
    // TODO: Uncomment when BE is ready
    // sales_id?: string,
    filter_by?: 'trans_date' | 'realization_date',
    start_date?: string,
    end_date?: string,
    page?: number,
    limit?: number
  ): Promise<BaseApiResponse<CustomerPaymentReport> | undefined> {
    return await this.customRequest<BaseApiResponse<CustomerPaymentReport>>(
      `customer-payment`,
      {
        method: 'GET',
        params: {
          customer_ids: customer_ids,
          // TODO: Uncomment when BE is ready
          // sales_id: sales_id,
          filter_by: filter_by,
          start_date: start_date,
          end_date: end_date,
          page: page,
          limit: limit,
        },
      }
    );
  }
}

export const FinanceApi = new FinanceApiService('reports');

// export const FinanceApi = new FinanceApiService(
//   'http://localhost:4010/api/reports/finance'
// );
