import httpClient from '@/shared/config/api/httpClient';
import { API_URLS } from '@/shared/config/api/URLs';
import { AxiosResponse } from 'axios';

export interface CartItem {
  id: string;
  cart_item: {
    id: number;
    product: {
      id: number;
      images: {
        id: number;
        image: string;
      }[];
      liked: boolean;
      meansurement: null | { id: number; name: string };
      inventory_id: null | string;
      product_id: string;
      code: string;
      name: string;
      short_name: string;
      weight_netto: null | string;
      weight_brutto: null | string;
      litr: null | string;
      box_type_code: null | string;
      box_quant: null | string;
      groups: number[];
      state: 'A' | 'P';
      barcodes: string | null;
      article_code: null | string;
      marketing_group_code: null | string;
      inventory_kinds: { id: number; name: string }[];
      sector_codes: { id: number; code: string }[];
      balance: number;
      prices: {
        id: number;
        price: string;
        price_type: {
          id: number;
          name: string;
          code: string;
        };
      }[];
    };
    quantity: number;
  }[];
}

export interface OrderCreateBody {
  order: {
    filial_code: string;
    delivery_date: string;
    room_code: string;
    robot_code: string;
    deal_time: string;
    status: string;
    sales_manager_code: string;
    person_code: string;
    currency_code: string;
    owner_person_code: string;
    note: string;
    order_products: {
      inventory_kind: string;
      product_code: string;
      on_balance: string;
      order_quant: number;
      price_type_code: string;
      product_price: string;
      warehouse_code: string;
    }[];
  }[];
}

export const cart_api = {
  async create(): Promise<AxiosResponse<{ cart_id: string }>> {
    const res = await httpClient.post(API_URLS.CartCrate);
    return res;
  },

  async cart_item(body: { product: string; quantity: number; cart: string }) {
    const res = await httpClient.post(API_URLS.CartItem, body);
    return res;
  },

  async get_cart_items(cart_id: string): Promise<AxiosResponse<CartItem>> {
    const res = await httpClient.get(`${API_URLS.CartItemList(cart_id)}`);
    return res;
  },

  async update_cart_item({
    body,
    cart_item_id,
  }: {
    body: { quantity: number };
    cart_item_id: string;
  }): Promise<AxiosResponse> {
    const res = await httpClient.patch(
      `${API_URLS.CartItemUpdate(cart_item_id)}`,
      body,
    );
    return res;
  },

  async delete_cart_item(cart_item_id: string): Promise<AxiosResponse> {
    const res = await httpClient.delete(
      `${API_URLS.CartItemDelete(cart_item_id)}`,
    );
    return res;
  },

  async createOrder(body: OrderCreateBody) {
    const res = await httpClient.post(`${API_URLS.CreateOrder}`, body);
    return res;
  },

  async clear_cart(id: number | string) {
    const res = await httpClient.get(API_URLS.CartClear(id));
    return res;
  },
};
