import { Customer } from '@/types/api/master-data/customer';
import {
  BaseApproval,
  BaseMetadata,
  CreatedUser,
} from '@/types/api/api-general';
import { ProductWarehouse } from '@/types/api/inventory/product-warehouse';
import { Kandang } from '@/types/api/master-data/kandang';
import { id } from 'react-day-picker/locale';
import { Warehouse } from '@/types/api/master-data/warehouse';

/**
 * Base Data Response
 */
export type BaseMarketing = {
  id: number;
  status?: string;
  so_number: string;
  so_date: string;
  customer: Customer;
  sales_person: CreatedUser;
  notes: string;
  latest_approval: BaseApproval;
  sales_order: BaseSalesOrder[];
  delivery_order: BaseDeliveryOrder[];
};

export type BaseSalesOrder = {
  id: number;
  marketing_id: number;
  product_warehouse_id: number;
  qty: number;
  unit_price: number;
  avg_weight: number;
  total_weight: number;
  total_price: number;
  product_warehouse: ProductWarehouse;
  vehicle_number: string;
};

export type BaseDeliveryOrder = {
  id: number;
  marketing_id: number;
  do_number: string;
  delivery_date: string;
  warehouse: Warehouse;
  deliveries: BaseDelivery[];
};

export type BaseDelivery = {
  product_warehouse: ProductWarehouse;
  qty: number;
  unit_price: number;
  total_weight: number;
  avg_weight: number;
  total_price: number;
  vehicle_number: string;
};

export type MarketingProduct = {
  id: number;
  qty: number;
  unit_price: number;
  avg_weight: number;
  total_weight: number;
  total_price: number;
  product_warehouse: ProductWarehouse;
  delivery_product?: MarketingDeliveryProducts;
};

export type MarketingDeliveryProducts = {
  id: number;
  qty: number;
  unit_price: number;
  avg_weight: number;
  total_weight: number;
  total_price: number;
  delivery_date: string | null;
  vehicle_number: string;
  do_number?: string | undefined; // Uncertain
};

export type Marketing = BaseMetadata & BaseMarketing;

/**
 * Base Data Payload
 */
export type BaseCreateMarketingPayload = {
  customer_id: number;
  sales_person_id: number;
  date: string;
  notes: string;
};

export type BaseCreateMarketingProductPayload = {
  vehicle_number: string;
  kandang_id: string | number | undefined;
  product_warehouse_id: string | number | undefined;
  unit_price: string | number | undefined;
  total_weight: string | number | undefined;
  qty: string | number | undefined;
  avg_weight: string | number | undefined;
  total_price: string | number | undefined;
};

/**
 * Payload Data Types Sales Order
 */

export type CreateSalesOrderPayload = BaseCreateMarketingPayload & {
  marketing_products: CreateSalesOrderProductPayload[];
};

export type CreateSalesOrderProductPayload =
  BaseCreateMarketingProductPayload & {
    id?: number;
    kandang?: Kandang | undefined;
    product_warehouse?: ProductWarehouse | undefined;
  };

export type CreateDeliveryOrderPayload = {
  marketing_id?: number;
  delivery_products: CreateDeliveryOrderProductPayload[];
};

export type CreateDeliveryOrderProductPayload =
  BaseCreateMarketingProductPayload & {
    id?: number;
    marketing_product_id: number;
    delivery_date: string;
  };

export type UpdateSalesOrderProductPayload = CreateSalesOrderProductPayload;

export type UpdateDeliveryOrderProductPayload =
  CreateDeliveryOrderProductPayload;

export type UpdateSalesOrderPayload = CreateSalesOrderPayload;

export type UpdateDeliveryOrderPayload = CreateDeliveryOrderPayload;
