// ─── Stock Receipt ───────────────────────────────────────────────

export interface ReceiptItem {
  id: number;
  inventory_receipt_id: number;
  product_id: number;
  product_name: string;
  product_code: string;
  product_variant_id: number | null;
  variant_name: string | null;
  unit_id: number;
  unit_name: string;
  unit_symbol: string;
  quantity: number;
  unit_cost: number;
  total_cost: number;
  batch_number: string | null;
  expiry_date: string | null;
  notes: string | null;
}

export interface ReceiptItemPayload {
  product_id: number;
  product_variant_id?: number | null;
  unit_id: number;
  quantity: number;
  unit_cost: number;
  batch_number?: string | null;
  expiry_date?: string | null;
  notes?: string | null;
}

export interface StockReceipt {
  id: number;
  company_id: number;
  warehouse_id: number;
  warehouse_name: string;
  warehouse_code: string;
  receipt_number: string;
  date: string;
  reference_type: 'purchase' | 'return' | 'adjustment' | 'opening' | 'other';
  reference_type_label: string;
  reference_id: number | null;
  partner_id: number | null;
  partner_name: string | null;
  status: 'draft' | 'approved' | 'cancelled';
  status_label: string;
  notes: string | null;
  notes_ar: string | null;
  total_quantity: number;
  total_cost: number;
  items: ReceiptItem[];
  created_by: number;
  created_by_name: string;
  approved_by: number | null;
  approved_at: string | null;
  cancelled_by: number | null;
  cancelled_at: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateStockReceipt {
  warehouse_id: number;
  date: string;
  reference_type: 'purchase' | 'return' | 'adjustment' | 'opening' | 'other';
  reference_id?: number | null;
  partner_id?: number | null;
  notes?: string | null;
  notes_ar?: string | null;
  items: ReceiptItemPayload[];
}

export interface UpdateStockReceipt {
  warehouse_id?: number;
  date?: string;
  reference_type?: 'purchase' | 'return' | 'adjustment' | 'opening' | 'other';
  reference_id?: number | null;
  partner_id?: number | null;
  notes?: string | null;
  notes_ar?: string | null;
  items?: ReceiptItemPayload[];
}

// ─── Stock Issue ─────────────────────────────────────────────────

export interface IssueItem {
  id: number;
  inventory_issue_id: number;
  product_id: number;
  product_name: string;
  product_code: string;
  product_variant_id: number | null;
  variant_name: string | null;
  unit_id: number;
  unit_name: string;
  unit_symbol: string;
  quantity: number;
  unit_cost: number;
  total_cost: number;
  batch_number: string | null;
  notes: string | null;
}

export interface IssueItemPayload {
  product_id: number;
  product_variant_id?: number | null;
  unit_id: number;
  quantity: number;
  unit_cost: number;
  batch_number?: string | null;
  expiry_date?: string | null;
  notes?: string | null;
}

export interface StockIssue {
  id: number;
  company_id: number;
  warehouse_id: number;
  warehouse_name: string;
  warehouse_code: string;
  issue_number: string;
  date: string;
  reference_type: 'sale' | 'consumption' | 'damage' | 'adjustment' | 'other';
  reference_type_label: string;
  reference_id: number | null;
  partner_id: number | null;
  partner_name: string | null;
  status: 'draft' | 'approved' | 'cancelled';
  status_label: string;
  notes: string | null;
  notes_ar: string | null;
  total_quantity: number;
  total_cost: number;
  items: IssueItem[];
  created_by: number;
  created_by_name: string;
  approved_by: number | null;
  approved_at: string | null;
  cancelled_by: number | null;
  cancelled_at: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateStockIssue {
  warehouse_id: number;
  date: string;
  reference_type: 'sale' | 'consumption' | 'damage' | 'adjustment' | 'other';
  reference_id?: number | null;
  partner_id?: number | null;
  notes?: string | null;
  notes_ar?: string | null;
  items: IssueItemPayload[];
}

export interface UpdateStockIssue {
  warehouse_id?: number;
  date?: string;
  reference_type?: 'sale' | 'consumption' | 'damage' | 'adjustment' | 'other';
  reference_id?: number | null;
  partner_id?: number | null;
  notes?: string | null;
  notes_ar?: string | null;
  items?: IssueItemPayload[];
}

// ─── Inventory Transfer ──────────────────────────────────────────

export interface TransferItem {
  id: number;
  inventory_transfer_id: number;
  product_id: number;
  product_variant_id: number | null;
  unit_id: number;
  requested_quantity: number;
  shipped_quantity: number;
  received_quantity: number;
  unit_cost: number;
  batch_number: string | null;
  notes: string | null;
  product: { id: number; name: string; code: string } | null;
  product_variant: { id: number; name: string; sku: string } | null;
  unit: { id: number; name: string; symbol: string } | null;
}

export interface TransferItemPayload {
  product_id: number;
  product_variant_id?: number | null;
  unit_id: number;
  requested_quantity: number;
  shipped_quantity?: number;
  received_quantity?: number;
  unit_cost: number;
  notes?: string | null;
}

export interface InventoryTransfer {
  id: number;
  company_id: number;
  from_warehouse_id: number;
  to_warehouse_id: number;
  from_warehouse: { id: number; name: string; code: string } | null;
  to_warehouse: { id: number; name: string; code: string } | null;
  transfer_number: string;
  date: string;
  expected_date: string | null;
  status: 'draft' | 'shipped' | 'received' | 'cancelled';
  status_label: string;
  notes: string | null;
  notes_ar: string | null;
  total_quantity: number;
  items: TransferItem[];
  requested_by: number | null;
  created_by: number;
  shipped_by: number | null;
  received_by: number | null;
  cancelled_by: number | null;
  shipped_at: string | null;
  received_at: string | null;
  cancelled_at: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateInventoryTransfer {
  from_warehouse_id: number;
  to_warehouse_id: number;
  date: string;
  notes?: string | null;
  notes_ar?: string | null;
  items: TransferItemPayload[];
}

export interface UpdateInventoryTransfer {
  from_warehouse_id?: number;
  to_warehouse_id?: number;
  date?: string;
  notes?: string | null;
  notes_ar?: string | null;
  items?: TransferItemPayload[];
}

// ─── Stock Balance ──────────────────────────────────────────────

export interface StockBalance {
  id: number;
  company_id: number;
  product_id: number;
  product_name: string;
  product_code: string;
  product_sku: string | null;
  product_category_id: number | null;
  product_variant_id: number | null;
  product_variant_name: string | null;
  warehouse_id: number;
  warehouse_name: string;
  warehouse_code: string;
  quantity: string;
  reserved_quantity: string | null;
  available_quantity: number;
  average_cost: string;
  total_value: string;
  last_receipt_date: string | null;
  last_issue_date: string | null;
  reorder_point: string;
  min_stock_level: string;
  max_stock_level: string;
  created_at: string;
  updated_at: string;
}

// ─── Stock Card ─────────────────────────────────────────────────

export interface StockCardEntry {
  id: number;
  company_id: number;
  product_id: number;
  product_name: string;
  product_code: string;
  product_variant_id: number | null;
  product_variant_name: string | null;
  warehouse_id: number;
  warehouse_name: string;
  warehouse_code: string;
  movement_type: string;
  movement_type_label: string;
  reference_type: string;
  reference_id: number | null;
  date: string;
  quantity_in: string;
  quantity_out: string;
  unit_cost: string;
  total_cost: string;
  balance_after: string;
  cost_after: string;
  notes: string | null;
  created_at: string;
}

// ─── Inventory Count ────────────────────────────────────────────

export interface CountItem {
  id: number;
  inventory_count_id: number;
  product_id: number;
  product_name: string;
  product_code: string;
  product_variant_id: number | null;
  variant_name: string | null;
  unit_id: number;
  unit_name: string;
  unit_symbol: string;
  system_quantity: number;
  counted_quantity: number;
  difference: number;
  unit_cost: number;
  notes: string | null;
}

export interface CountItemPayload {
  product_id: number;
  product_variant_id?: number | null;
  unit_id: number;
  counted_quantity: number;
  unit_cost?: number;
  notes?: string | null;
}

export interface InventoryCount {
  id: number;
  company_id: number;
  warehouse_id: number;
  warehouse_name: string;
  warehouse_code: string;
  count_number: string;
  date: string;
  status: 'draft' | 'approved' | 'cancelled';
  status_label: string;
  notes: string | null;
  notes_ar: string | null;
  items: CountItem[];
  created_by: number;
  created_by_name: string;
  approved_by: number | null;
  approved_at: string | null;
  cancelled_by: number | null;
  cancelled_at: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateInventoryCount {
  warehouse_id: number;
  date: string;
  notes?: string | null;
  notes_ar?: string | null;
  items: CountItemPayload[];
}

export interface UpdateInventoryCount {
  warehouse_id?: number;
  date?: string;
  notes?: string | null;
  notes_ar?: string | null;
  items?: CountItemPayload[];
}

// ─── Stock Adjustment ───────────────────────────────────────────

export interface AdjustmentItem {
  id: number;
  inventory_adjustment_id: number;
  product_id: number;
  product_name: string;
  product_code: string;
  product_variant_id: number | null;
  variant_name: string | null;
  unit_id: number;
  unit_name: string;
  unit_symbol: string;
  adjustment_type: 'increase' | 'decrease';
  adjustment_type_label: string;
  quantity: number;
  unit_cost: number;
  total_cost: number;
  reason: string | null;
  notes: string | null;
}

export interface AdjustmentItemPayload {
  product_id: number;
  product_variant_id?: number | null;
  unit_id: number;
  adjustment_type: 'increase' | 'decrease';
  quantity: number;
  unit_cost: number;
  reason?: string | null;
  notes?: string | null;
}

export interface StockAdjustment {
  id: number;
  company_id: number;
  warehouse_id: number;
  warehouse_name: string;
  warehouse_code: string;
  adjustment_number: string;
  date: string;
  adjustment_type: string;
  adjustment_type_label: string;
  status: 'draft' | 'approved' | 'cancelled';
  status_label: string;
  notes: string | null;
  notes_ar: string | null;
  total_quantity: number;
  total_cost: number;
  items: AdjustmentItem[];
  created_by: number;
  created_by_name: string;
  approved_by: number | null;
  approved_at: string | null;
  cancelled_by: number | null;
  cancelled_at: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateStockAdjustment {
  warehouse_id: number;
  date: string;
  adjustment_type: string;
  notes?: string | null;
  notes_ar?: string | null;
  items: AdjustmentItemPayload[];
}

export interface UpdateStockAdjustment {
  warehouse_id?: number;
  date?: string;
  adjustment_type?: string;
  notes?: string | null;
  notes_ar?: string | null;
  items?: AdjustmentItemPayload[];
}

// ─── Reorder Alert ──────────────────────────────────────────────

export interface ReorderAlert {
  id: number;
  product_id: number;
  product_name: string;
  product_code: string;
  product_sku: string | null;
  warehouse_id: number;
  warehouse_name: string;
  warehouse_code: string;
  current_quantity: string;
  min_stock_level: string;
  reorder_point: string;
  max_stock_level: string;
  suggested_order_quantity: string;
}

// ─── Opening Balance Inventory ──────────────────────────────────

export interface OpeningBalanceItem {
  id: number;
  opening_balance_id: number;
  product_id: number;
  product_name: string;
  product_code: string;
  product_variant_id: number | null;
  variant_name: string | null;
  unit_id: number;
  unit_name: string;
  unit_symbol: string;
  quantity: number;
  unit_cost: number;
  total_cost: number;
  batch_number: string | null;
  expiry_date: string | null;
  notes: string | null;
}

export interface OpeningBalanceItemPayload {
  product_id: number;
  product_variant_id?: number | null;
  unit_id: number;
  quantity: number;
  unit_cost: number;
  batch_number?: string | null;
  expiry_date?: string | null;
  notes?: string | null;
}

export interface OpeningBalanceInventory {
  id: number;
  company_id: number;
  warehouse_id: number;
  warehouse_name: string;
  warehouse_code: string;
  balance_number: string;
  date: string;
  status: 'draft' | 'approved' | 'cancelled';
  status_label: string;
  notes: string | null;
  notes_ar: string | null;
  total_quantity: number;
  total_cost: number;
  items: OpeningBalanceItem[];
  created_by: number;
  created_by_name: string;
  approved_by: number | null;
  approved_at: string | null;
  cancelled_by: number | null;
  cancelled_at: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateOpeningBalanceInventory {
  warehouse_id: number;
  date: string;
  notes?: string | null;
  notes_ar?: string | null;
  items: OpeningBalanceItemPayload[];
}

export interface UpdateOpeningBalanceInventory {
  warehouse_id?: number;
  date?: string;
  notes?: string | null;
  notes_ar?: string | null;
  items?: OpeningBalanceItemPayload[];
}
