import { BaseApproval, BaseMetadata } from '@/types/api/api-general';
import { BaseLocation } from '@/types/api/master-data/location';
import { BaseSupplier } from '@/types/api/master-data/supplier';
import { BaseNonstock } from '@/types/api/master-data/nonstock';

export type BaseExpense = {
  id: number;
  reference_number: string;
  po_number: string | null;
  category: 'BOP' | 'NON-BOP';
  documents?: {
    id: number;
    name: string;
    path: string;
  }[];
  realization_docs?: {
    id: number;
    path: string;
  }[];
  transaction_date: string;
  realization_date?: string;
  grand_total: number;
  location: BaseLocation;
  supplier: BaseSupplier;
  kandangs: {
    id: number;
    kandang_id: number;
    name: string;
    pengajuans?: {
      id: number;
      expense_id: number;
      kandang_id: number;
      nonstock_id: number;
      qty: number;
      price: number;
      notes?: string;
      nonstock: Pick<BaseNonstock, 'id' | 'name' | 'flags'>;
      created_at: string;
    }[];
    realisasi?: {
      id: number;
      expense_nonstock_id: number;
      qty: number;
      price: number;
      notes?: string;
      nonstock: Pick<BaseNonstock, 'id' | 'name' | 'flags'>;
      created_at: string;
    }[];
  }[];
  total_pengajuan: number;
  total_realisasi: number;
  latest_approval: BaseApproval;
  is_paid: boolean;
};

export type Expense = BaseMetadata & BaseExpense;

export type CreateExpensePayload = {
  category: 'BOP' | 'NON-BOP';
  location_id: number;
  transaction_date: string;
  supplier_id: number;
  documents: File[];
  expense_nonstocks: {
    kandang_id?: number;
    cost_items: {
      nonstock_id: number;
      quantity: number;
      price: number;
      notes: string;
    }[];
  }[];
};

export type UpdateExpensePayload = {
  location_id: number;
  category: 'BOP' | 'NON-BOP';
  transaction_date: string;
  supplier_id: number;
  documents: File[];
  expense_nonstocks: {
    kandang_id?: number;
    cost_items: {
      nonstock_id: number;
      quantity: number;
      price: number;
      notes: string;
    }[];
  }[];
};

export type CreateExpenseRealizationPayload = {
  realization_date: string;
  documents: File[];
  realizations: {
    expense_nonstock_id: number;
    qty: number;
    price: number;
    notes: string;
  }[];
};

export type UpdateExpenseRealizationPayload = CreateExpenseRealizationPayload;

export type ExpenseBulkApprovalStatus =
  | 'HEAD_AREA'
  | 'UNIT_VICE_PRESIDENT'
  | 'FINANCE'
  | 'REALISASI';

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