import { BaseApproval, BaseMetadata, User } from '@/types/api/api-general';
import { ProductWarehouse } from '@/types/api/inventory/product-warehouse';
import { Warehouse } from '@/types/api/master-data/warehouse';
import { Kandang } from '@/types/api/master-data/kandang';
import { Location } from '@/types/api/master-data/location';

export type ProductionStandard = {
  id: number;
  week: number;
  name: string;
  hen_day_std: number;
  hen_house_std: number;
  feed_intake_std: number;
  max_depletion_std: number;
  egg_mass_std: number;
  egg_weight_std: number;
};

export type FCR = {
  id: number;
  name: string;
  fcr_std: number;
};

export type ProjectFlock = {
  project_flock_kandang_id: number;
  flock_name: string;
  project_flock_category: 'GROWING' | 'LAYING';
  period: number;
  production_standart: ProductionStandard;
  fcr: FCR;
  total_chick_qty: number;
};

export type ProductionMetrics = {
  total_depletion_qty: number;
  cum_depletion_rate: number;
  cum_intake: number;
  fcr_value: number;
  hen_day?: number;
  hen_house?: number;
  feed_intake?: number;
  egg_mass?: number;
  egg_weight?: number;
};

export type BaseRecording = {
  id: number;
  project_flock: ProjectFlock;
  record_datetime: string;
  day: number;
  is_transition: boolean;
  is_laying: boolean;
  week: number;
  excess_days: number;
} & ProductionMetrics;

export type RecordingDepletion = {
  product_warehouse_id: number;
  source_product_warehouse_id?: number;
  qty: number;
  product_warehouse: ProductWarehouse;
};

export type RecordingStock = {
  product_warehouse_id: number;
  qty?: number;
  usage_amount?: number;
  pending_qty: number;
  total_price?: number;
  product_warehouse: ProductWarehouse;
};

export type RecordingEgg = {
  id: number;
  product_warehouse_id: number;
  qty: number;
  weight: number;
  product_warehouse: ProductWarehouse;
  gradings?: {
    grade: string;
    qty: number;
  }[];
};

export type GradingEgg = {
  id: number;
  recording_egg_id: number;
  qty: number;
  grade: string;
  created_by: User;
};

export type Recording = BaseMetadata &
  BaseRecording & {
    approval?: BaseApproval;
    created_user: User;
    warehouse?: Warehouse;
    kandang?: Kandang;
    location?: Location;
    product_category?: 'GROWING' | 'LAYING';
    depletions?: RecordingDepletion[];
    stocks?: RecordingStock[];
    eggs?: RecordingEgg[];
    grading_eggs?: GradingEgg[];
  };

export type NextDayRecording = {
  project_flock_kandang_id: number;
  next_day: number;
};

export type CreateGrowingRecordingPayload = {
  project_flock_kandang_id: number;
  record_date: string;
  stocks?: {
    product_warehouse_id: number;
    qty: number;
  }[];
  depletions?: {
    product_warehouse_id?: number;
    source_product_warehouse_id?: number;
    qty?: number;
    product_warehouse?: ProductWarehouse;
  }[];
};

export type CreateEggPayload = {
  product_warehouse_id?: number;
  qty?: number;
  weight?: number;
  product_warehouse?: ProductWarehouse;
};

export type CreateLayingRecordingPayload = CreateGrowingRecordingPayload & {
  eggs?: CreateEggPayload[];
};

export type CreateRecordingPayload =
  | CreateGrowingRecordingPayload
  | CreateLayingRecordingPayload;

export type UpdateGrowingRecordingPayload = CreateGrowingRecordingPayload;
export type UpdateLayingRecordingPayload = CreateLayingRecordingPayload;

export type UpdateRecordingPayload = CreateRecordingPayload;
