import { BaseApiService } from '@/services/api/base';
import { BaseApiResponse } from '@/types/api/api-general';
import {
  CreateProductWarehousePayload,
  ProductWarehouse,
  UpdateProductWarehousePayload,
} from '@/types/api/inventory/product-warehouse';
import {
  CreateMovementPayload,
  Movement,
} from '@/types/api/inventory/movement';
import {
  CreateInventoryAdjustmentPayload,
  InventoryAdjustment,
} from '@/types/api/inventory/adjustment';
import { InventoryProduct } from '@/types/api/inventory/product';

export const ProductWarehouseApi = new BaseApiService<
  ProductWarehouse,
  CreateProductWarehousePayload,
  UpdateProductWarehousePayload
>('/inventory/product-warehouses');

export class MovementApiService extends BaseApiService<
  Movement,
  CreateMovementPayload,
  unknown
> {
  constructor(basePath: string) {
    super(basePath);
  }

  async createMovement(
    payload: CreateMovementPayload
  ): Promise<BaseApiResponse<Movement> | undefined> {
    const formData = new FormData();

    // Append data as JSON string
    formData.append('data', JSON.stringify(payload.data));

    // Append documents if any
    if (payload.documents && payload.documents.length > 0) {
      payload.documents.forEach((file) => {
        formData.append('documents', file);
      });
    }

    return await this.customRequest<BaseApiResponse<Movement>>('', {
      method: 'POST',
      payload: formData as unknown as Record<string, unknown>,
    });
  }
}

export const MovementApi = new MovementApiService('/inventory/transfers');

export const InventoryAdjustmentApi = new BaseApiService<
  InventoryAdjustment,
  CreateInventoryAdjustmentPayload,
  unknown
>('/inventory/adjustments');

export const InventoryProductApi = new BaseApiService<
  InventoryProduct,
  unknown,
  unknown
>('/inventory/product-stocks');
