import { apiClient } from "./client";

export interface SaleListFilter {
  orderType?: string;
  poNo?: string;
  customer_objid?: string;
  productType?: string;
  nation?: string;
  search_partObjId?: string;
  serialNo?: string;
  shippingStatus?: string;
  orderDateFrom?: string;
  orderDateTo?: string;
  shippingDateFrom?: string;
  shippingDateTo?: string;
  salesStatus?: string;
  salesDeadlineFrom?: string;
  salesDeadlineTo?: string;
}

export interface SaleListRow {
  project_no: string;
  contract_no: string | null;
  order_type: string | null;
  order_date: string | null;
  po_no: string | null;
  request_date: string | null;
  customer_objid: string | null;
  customer: string | null;
  product_type: string | null;
  contract_item_objid: string;
  seq: number;
  part_objid: string | null;
  product_no: string | null;
  product_name: string | null;
  order_quantity: string | null;
  sales_quantity: number | null;
  remaining_quantity: number | null;
  sale_no: number | null;
  sales_unit_price: number | null;
  sales_supply_price: number | null;
  sales_vat: number | null;
  sales_total_amount: number | null;
  sales_currency: string | null;
  sales_exchange_rate: number | null;
  shipping_date: string | null;
  shipping_method: string | null;
  shipping_order_status: string | null;
  manager_user_id: string | null;
  incoterms: string | null;
  has_split_shipment: boolean | null;
  order_status: string | null;
  sales_status: string;
  production_status: string | null;
  payment_type: string | null;
  payment_type_name: string | null;
  nation: string | null;
  nation_name: string | null;
  product_type_name: string | null;
  receipt_date: string | null;
  customer_request: string | null;
  manager_name: string | null;
  cu01_cnt: number | null;
  serial_no: string | null;
  // 다이얼로그 표시용 (wace 운영판 1:1)
  order_type_name: string | null;
  contract_currency_name: string | null;
  exchange_rate: string | null;
  return_reason_name: string | null;
}

export interface RevenueListRow {
  project_no: string;
  contract_no: string | null;
  order_type: string | null;
  order_date: string | null;
  po_no: string | null;
  customer_objid: string | null;
  customer: string | null;
  product_type: string | null;
  nation: string | null;
  log_id: number;
  target_objid: string;
  parent_sale_no: number | null;
  sales_quantity: number | null;
  sales_unit_price: number | null;
  sales_supply_price: number | null;
  sales_vat: number | null;
  sales_total_amount: number | null;
  sales_currency: string | null;
  sales_exchange_rate: number | null;
  shipping_date: string | null;
  shipping_method: string | null;
  serial_no: string | null;
  split_serial_no: string | null;
  sales_deadline_date: string | null;
  tax_type: string | null;
  tax_invoice_date: string | null;
  export_decl_no: string | null;
  loading_date: string | null;
  sales_slip_date: string | null;
  sales_slip_menu_sq: number | null;
  remark: string | null;
  receipt_date: string | null;
  payment_type: string | null;
  payment_type_name: string | null;
  request_date: string | null;
  customer_request: string | null;
  order_status: string | null;
  order_status_name: string | null;
  manager_name: string | null;
  incoterms: string | null;
  cu01_cnt: number | null;
  // 다이얼로그 표시용 (wace 운영판 1:1)
  order_type_name: string | null;
  product_type_name: string | null;
  nation_name: string | null;
  product_no: string | null;
  product_name: string | null;
  contract_currency_name: string | null;
  exchange_rate: string | null;
  return_reason_name: string | null;
}

export interface SaleRegisterBody {
  project_no: string;
  shipping_order_status?: string;
  serial_no?: string;
  sales_quantity?: number;
  sales_unit_price?: number;
  sales_supply_price?: number;
  sales_vat?: number;
  sales_total_amount?: number;
  sales_currency?: string;
  sales_exchange_rate?: number;
  shipping_date?: string;
  shipping_method?: string;
  manager_user_id?: string;
  incoterms?: string;
  has_split_shipment?: boolean;
}

export interface DeadlineInfoBody {
  log_id?: number;
  parent_sale_no: number;
  target_objid: string;
  sales_deadline_date?: string;
  tax_type?: string;
  tax_invoice_date?: string;
  export_decl_no?: string;
  loading_date?: string;
  sales_slip_date?: string;
  sales_slip_menu_sq?: number;
  remark?: string;
}

export const salesSaleApi = {
  async saleList(filter: SaleListFilter = {}) {
    const res = await apiClient.get("/sales/sale/list", { params: filter });
    return (res.data?.data ?? []) as SaleListRow[];
  },
  async registerSale(body: SaleRegisterBody) {
    return (await apiClient.post("/sales/sale", body)).data?.data;
  },
  async deleteSale(projectNo: string) {
    return (await apiClient.delete(`/sales/sale/${projectNo}`)).data;
  },

  async revenueList(filter: SaleListFilter = {}) {
    const res = await apiClient.get("/sales/revenue/list", { params: filter });
    return (res.data?.data ?? []) as RevenueListRow[];
  },
  async saveDeadlineInfo(body: DeadlineInfoBody) {
    return (await apiClient.post("/sales/revenue/deadline-info", body)).data?.data;
  },
  async confirmSalesDeadline(logIds: number[]) {
    return (await apiClient.post("/sales/revenue/deadline-confirm", { logIds })).data;
  },
};
