import { BaseApiService } from '@/services/api/base';
import { httpClient } from '@/services/http/client';
import { BaseApiResponse } from '@/types/api/api-general';
import {
  Marketing,
  CreateSalesOrderPayload,
  UpdateSalesOrderPayload,
  CreateDeliveryOrderPayload,
  UpdateDeliveryOrderPayload,
} from '@/types/api/marketing/marketing';

/**
 * 💡 Helper untuk membuat respons dummy
 * @param data Data yang akan dimasukkan ke dalam body respons
 */
const createDummyResponse = <T>(data: T): BaseApiResponse<T> => ({
  code: 200,
  status: 'success',
  message: 'Data retrieved successfully (MOCK)',
  data: data,
});

export class SalesOrderService extends BaseApiService<
  Marketing,
  CreateSalesOrderPayload,
  UpdateSalesOrderPayload
> {
  constructor(basePath: string = '/marketing') {
    super(basePath);
  }

  /**
   * Approve single marketing data
   */
  async singleApproval(
    id: number,
    action: 'APPROVED' | 'REJECTED',
    notes?: string
  ): Promise<BaseApiResponse<{ message: string }> | undefined> {
    try {
      const path = `${this.basePath}/approvals`;
      return await httpClient<BaseApiResponse<{ message: string }>>(path, {
        method: 'POST',
        body: {
          action: action,
          approvable_ids: [id],
          notes: notes || `${action} marketing ${id}`,
        },
      });
    } catch (error) {
      throw error;
    }
  }

  /**
   * Bulk approve
   */
  async bulkApprovals(
    ids: number[],
    action: 'APPROVED' | 'REJECTED',
    notes?: string
  ): Promise<BaseApiResponse<{ message: string }> | undefined> {
    try {
      const path = `${this.basePath}/approvals`;
      return await httpClient<BaseApiResponse<{ message: string }>>(path, {
        method: 'POST',
        body: {
          action: action,
          approvable_ids: ids,
          notes: notes || `${action} marketing ${ids.join(', ')}`,
        },
      });
    } catch (error) {
      throw error;
    }
  }

  /**
   * Delivery
   */
  async delivery(
    id: number,
    notes?: string
  ): Promise<BaseApiResponse<{ message: string }> | undefined> {
    try {
      const path = `${this.basePath}/approvals`;
      return await httpClient<BaseApiResponse<{ message: string }>>(path, {
        method: 'POST',
        body: {
          action: 'APPROVED',
          approvable_ids: [id],
          notes: notes || `Delivery marketing ${id}`,
        },
      });
    } catch (error) {
      throw error;
    }
  }
}

export const SalesOrderApi = new SalesOrderService('/marketing/sales-orders');
export const DeliveryOrderApi = new BaseApiService<
  Marketing,
  CreateDeliveryOrderPayload,
  UpdateDeliveryOrderPayload
>('/marketing/delivery-orders');
export const MarketingApi = new BaseApiService<Marketing, unknown, unknown>(
  '/marketing'
);
