import {
  BaseApproval,
  BaseMetadata,
  CreatedUser,
} from '@/types/api/api-general';
import { Supplier } from '@/types/api/master-data/supplier';
import { Warehouse } from '@/types/api/master-data/warehouse';
import { Product } from '@/types/api/master-data/product';
import { ProductWarehouse } from '@/types/api/inventory/product-warehouse';
import { Area } from '@/types/api/master-data/area';
import { Location } from '@/types/api/master-data/location';
import { Uom } from '@/types/api/master-data/uom';

export type PurchaseItemProduct = {
  id: number;
  name: string;
  flags?: string[];
  ProductPrice?: number;
  SellingPrice?: number;
  uom: Uom;
  product_category?:
    | {
        id: number;
        name: string;
        code: string;
      }
    | string;
  suppliers?: Supplier[];
};

export type PurchaseItem = {
  id: number;
  product_id: number;
  warehouse: Warehouse;
  product: PurchaseItemProduct | Product;
  product_warehouse: ProductWarehouse;
  quantity: number;
  qty: number;
  sub_qty: number;
  total_qty: number;
  total_used: number;
  price: number;
  total_price: number;
  received_date?: string | null;
  travel_number?: string | null;
  travel_number_docs?: string | null;
  travel_document_path?: string | null;
  vehicle_number?: string | null;
  expedition_vendor_id?: number | null;
  expedition_vendor_name?: string | null;
  received_qty?: number | null;
  transport_per_item?: number | null;
  has_chickin?: boolean;
  expedition_vendor?: {
    id?: number;
    name?: string;
    alias?: string;
    category?: string;
  } | null;
  expedition_qty?: number;
  expedition_total?: number;
};

export type BasePurchase = {
  id: number;
  pr_number: string;
  po_number: string;
  po_document_path?: string | null;
  po_date: string;
  supplier: Supplier;
  credit_term?: number;
  due_date: string;
  received_date?: string | null;
  notes?: string | null;
  deleted_at?: string | null;
  created_by: number;
  area?: Area;
  location?: Location;
  warehouse?: Warehouse;
  items?: PurchaseItem[];
  latest_approval?: BaseApproval;
  requester_name?: string;
  po_expedition?: { id: number; refrence: string }[];
  created_user?: CreatedUser;
  products?: PurchaseItemProduct[];
  products_total?: number;
  expedition_total?: number;
  grand_total_all?: number;
};

export type Purchase = BaseMetadata & BasePurchase;

export type CreatePurchaseRequestPayload = {
  supplier_id: number;
  credit_term: number;
  notes?: string | null;
  items?: {
    warehouse_id: number;
    product_id: number;
    qty: number;
  }[];
};

export type CreateStaffApprovalRequestPayload = {
  action: 'APPROVED' | 'REJECTED';
  notes?: string | null;
  items?: {
    purchase_item_id: number;
    qty: number;
    price: number;
    total_price: number;
  }[];
};

export type UpdateStaffApprovalRequestPayload = {
  action: 'APPROVED' | 'REJECTED';
  notes?: string | null;
  items?: Array<{
    purchase_item_id?: number;
    product_id?: number;
    warehouse_id?: number;
    qty: number;
    price: number;
    total_price: number;
  }>;
};

export type CreateManagerApprovalRequestPayload = {
  action: 'APPROVED' | 'REJECTED';
  notes?: string | null;
  po_date?: string | null;
};

export type CreateAcceptApprovalRequestPayload = {
  action: 'APPROVED' | 'REJECTED';
  notes?: string | null;
  items?: {
    purchase_item_id: number;
    received_date: string;
    travel_number: string;
    vehicle_number?: string | null;
    expedition_vendor_id?: number | null;
    received_qty: number;
    transport_per_item?: number | null;
  }[];
  travel_documents?: File[] | null;
};

export type DeletePurchaseRequestItemPayload = {
  item_ids: number[];
};

export type UpdatePurchaseRequestPayload = CreatePurchaseRequestPayload;

export type PurchaseFilter = {
  poDate: string;
  start_date?: string;
  end_date?: string;
  filterBy?: { label: string; value: string };
  category: string[];
  category_labels?: { label: string; value: number }[];
  status: string[];
  supplier_id?: number;
  supplier_label?: string;
  area_id?: number;
  area_label?: string;
  location_id?: number;
  location_label?: string;
  project_flock_id?: number;
  project_flock_label?: string;
  project_flock_kandang_id?: number;
  project_flock_kandang_label?: string;
};
