import { Kandang } from '@/types/api/master-data/kandang';
import { Product } from '@type/api/master-data/product';
import { Customer } from '@type/api/master-data/customer';
import { BaseMetadata } from '@/types/api/api-general';
import { Kandang } from '@/types/api/master-data/kandang';
import { Product } from '@type/api/master-data/product';
import { Customer } from '@type/api/master-data/customer';
import { BaseMetadata } from '@/types/api/api-general';
import { ProjectFlock } from '@/types/api/production/project-flock';
import { BaseUom } from '@/types/api/master-data/uom';

export type BaseSales = {
  id: number;
  realization_date: string;
  age: number;
  week: number;
  do_number: string;
  product: Product;
  customer: Customer;
  qty: number;
  weight: number;
  avg_weight: number;
  sales_price: number;
  total_sales_price: number;
  actual_price: number;
  total_actual_price: number;
  kandang: Kandang;
};

export type ClosingSalesSummary = {
  total_sales_price: number;
  avg_sales_price: number;
  total_actual_price: number;
  avg_actual_price: number;
};

export type BaseClosingSales = {
  project_type: string;
  flock_id: number;
  period: number;
  sales: BaseSales[];
  summary: ClosingSalesSummary;
};

export type BaseClosing = {
  id: number;
  location_id: number;
  location_name: string;
  project_category: 'GROWING' | 'LAYING';
  project_type?: 'GROWING' | 'LAYING'; // berubah dari BE?
  period: number;
  closing_date?: string;
  shed_label: string;
  shed_count: number;
  project_status: 'Pengajuan' | 'Aktif' | 'Selesai';
};

export type Closing = BaseMetadata & BaseClosing;

export type BaseClosingGeneralInformation = BaseClosing & {
  flock_id: number;
  period: number;
  project_type: 'GROWING' | 'LAYING';
  population: number;
  active_house_count: number;
  sales_payment_status: string;
  project_status: 'Pengajuan' | 'Aktif' | 'Selesai';
  closing_status: string;
  project_flock: ProjectFlock;
};

export type ClosingGeneralInformation = BaseMetadata &
  BaseClosingGeneralInformation;

export type ClosingIncomingSapronak = {
  id: number;
  date: string;
  reference_number: string;
  transaction_type: string;
  product_name: string;
  product_category: string;
  product_sub_category: string;
  source_warehouse: string;
  destination_warehouse: string;
  quantity: number;
  unit: string;
  formatted_quantity: string;
  notes: string;
};

export type ClosingIncomingSapronakSummary = {
  category: string;
  total_qty: number;
  uom: BaseUom;
};

export type ClosingOutgoingSapronak = ClosingIncomingSapronak;

export type ClosingOutgoingSapronakSummary = ClosingIncomingSapronakSummary;

export type ClosingProductionData = {
  purchase: {
    initial_population: number;
    claim_culling: number;
    final_population: number;
    feed_in: number;
    feed_used: number;
  };
  sales: {
    chicken: {
      sales_population: number;
      sales_weight: number;
      avg_weight: number;
      avg_selling_price: number;
    };
    egg?: {
      egg_pieces: number;
      egg_mass: number;
      avg_egg_weight: number;
      avg_selling_price: number;
    };
  };
  performance: {
    depletion: number;
    age_day: number;
    mor_std: number;
    mor_act: number;
    mor_diff: number;
    awg_act: number;
    awg_std: number;
    feed_intake: number;
    feed_intake_std: number;
    fcr_act: number;
    fcr_std: number;
    fcr_diff: number;
    hen_day_act?: number;
    hen_day_std?: number;
    egg_mass?: number;
    egg_mass_std?: number;
    egg_weight?: number;
    egg_weight_std?: number;
    hen_housed_act?: number;
    hen_housed_std?: number;
  };
};

// ====== PERHITUNGAN SAPRONAK ======

export type RowSapronakCalculation = {
  id: number;
  date: string;
  reference_number: string;
  qty_in: number;
  qty_out: number;
  qty_used: number;
  description: string;
  product_category: string;
  unit_price: number;
  total_amount: number;
  notes: string;
};

export type TotalSapronakCalculation = {
  label: string;
  qty_in: number;
  qty_out: number;
  qty_used: number;
  avg_unit_price: number;
  total_amount: number;
};

export type ClosingSapronakCalculationItem = {
  rows: RowSapronakCalculation[];
  total: TotalSapronakCalculation;
};

export type ClosingSapronakCalculation = {
  doc: ClosingSapronakCalculationItem;
  ovk: ClosingSapronakCalculationItem;
  pakan: ClosingSapronakCalculationItem;
};

// ====== OVERHEAD ======
export type ClosingOverhead = {
  overheads: Overhead[];
  total: OverheadTotal;
};

export type Overhead = {
  item_name: string;
  uom_name: string;
  budget_quantity: number;
  budget_unit_price: number;
  budget_total_amount: number;
  actual_date: string;
  actual_quantity: number;
  actual_unit_price: number;
  actual_total_amount: number;
  cost_per_bird: number;
};

export type OverheadTotal = {
  budget_quantity: number;
  budget_total_amount: number;
  actual_quantity: number;
  actual_total_amount: number;
  cost_per_bird: number;
};

export type ClosingSales = BaseMetadata & BaseClosingSales;

// ====== FINANCE ======
export interface ClosingFinance {
  hpp: ClosingFinanceHpp;
  profit_loss: ClosingFinanceProfitLoss;
}

export interface ClosingFinanceHpp {
  items: HppItem[];
  summary: HppSummary;
}

export interface HppItem {
  id: number;
  category: string;
  code: string;
  label: string;
  budgeting: HppPurchaseDataAmount;
  realization: HppPurchaseDataAmount;
}

export interface HppSummary {
  label: string;
  budgeting: HppPurchaseDataAmount;
  realization: HppPurchaseDataAmount;
  egg_budgeting: HppPurchaseDataAmount;
  egg_realization: HppPurchaseDataAmount;
}

export interface HppPurchaseDataAmount {
  rp_per_bird: number;
  rp_per_kg: number;
  amount: number;
}

export interface ClosingFinanceProfitLoss {
  items: ProfitLossItem[];
  summary: ProfitLossSummary;
}

export interface ProfitLossItem {
  code: string;
  label: string;
  type: string;
  rp_per_bird: number;
  rp_per_kg: number;
  amount: number;
}

export interface ProfitLossSummary {
  gross_profit: ProfitLossAmount;
  sub_total: ProfitLossAmount;
  net_profit: ProfitLossAmount;
}

export interface ProfitLossAmount {
  rp_per_bird: number;
  rp_per_kg: number;
  amount: number;
}

export type BaseExpeditionCost = {
  id: number;
  expedition_vendor_name: string;
  hpp_amount: number;
};

export type BaseHppExpedition = {
  expedition_costs: BaseExpeditionCost[];
  total_hpp_amount: number;
};

export type ClosingHppExpedition = BaseMetadata & BaseHppExpedition;
