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 { Warehouse } from '@/types/api/master-data/warehouse';

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

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;
  marketing_type: string;
  convertion_unit: string;
  total_peti: number;
  weight_per_convertion: number;
  /** Umur minggu untuk AYAM_PULLET */
  week?: number;
};

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

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

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;

/**
 * Filter Data Types
 */
export type MarketingFilter = {
  product_ids: number[];
  product_names: string[];
  status: string;
  status_name: string;
  customer_id: number;
  customer_name: string;
  project_flock_id?: number;
  project_flock_name?: string;
  project_flock_kandang_id?: number;
  project_flock_kandang_name?: string;
  start_date?: string;
  end_date?: string;
  filter_by?: string;
  filter_by_name?: string;
  warehouse_id?: number;
  warehouse_name?: string;
};

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

export type BaseCreateMarketingProductPayload = {
  vehicle_number: string;
  warehouse_id?: string | number | undefined;
  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;
  marketing_type: string;
  convertion_unit?: 'PETI' | 'KG';
  /** Berat per peti (kg), hanya untuk TELUR + PETI */
  weight_per_convertion?: number;
  /** Umur minggu untuk AYAM_PULLET */
  week?: number;
};

/**
 * Payload Data Types Sales Order
 */

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

export type CreateSalesOrderProductPayload =
  BaseCreateMarketingProductPayload & {
    id?: number;
    warehouse?: Warehouse | undefined;
    kandang?: Warehouse | 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;

export type MarketingBulkApprovalStatus = 'SALES_ORDER' | 'DELIVERY';

export type BulkApproveMarketingPayload = {
  approvable_ids: number[];
  status: MarketingBulkApprovalStatus;
  date?: string;
  notes?: string;
};
