import { BaseMetadata } from '@/types/api/api-general';
import { BaseApproval } from '@/types/api/approval/approval';

// ==================== CHART DATA TYPES ====================
export type WeightDistributionRange = {
  range: string;
  min_weight: number;
  max_weight: number;
  bird_count: number;
  is_ideal_range: boolean;
  ideal_range?: string;
  outside_range?: string;
};

export type IdealRange = {
  min_weight: number;
  max_weight: number;
  total_ideal_birds: number;
};

export type StatisticsData = {
  min_weight: number;
  max_weight: number;
  average_weight: number;
  total_birds_measured: number;
};

export type WeekBarChartData = {
  has_data: boolean;
  weight_distribution: WeightDistributionRange[];
  ideal_range: IdealRange;
  statistics: StatisticsData;
};

export type BarChart = {
  current_week: number;
  all_weeks: Record<string, WeekBarChartData>;
};

export type AvailableWeek = {
  week: number;
  uniformity_percentage: number;
  ideal_count: number;
  outside_ideal_count: number;
  total_count: number;
  has_data: boolean;
};

export type WeekInfo = {
  total_weeks: number;
  weeks_with_data: number;
  current_week_index: number;
  has_prev_week: boolean;
  has_next_week: boolean;
};

export type GaugeChart = {
  current_week: number;
  available_weeks: AvailableWeek[];
  week_info: WeekInfo;
};

export type ChartData = {
  bar_chart: BarChart;
  gauge_chart: GaugeChart;
};

// ==================== GET ALL RESPONSE ====================
export type Uniformity = BaseMetadata & {
  id: number;
  project_flock_kandang_id: number;
  location_name: string;
  flock_name: string;
  kandang_name: string;
  file_name: string;
  applied_at: string;
  week: number;
  status: string;
  uniformity: number;
  cv: number;
  chick_qty_of_weight: number;
  uniform_qty: number;
  mean_up: number;
  mean_down: number;
  standard_mean_weight: number | null;
  standard_uniformity: number | null;
  created_at: string;
  chart_data?: ChartData;
  created_by: number;
  latest_approval?: BaseApproval;
};

// ==================== GET ONE RESPONSE ====================
export type UniformityInfoUmum = {
  tanggal: string;
  lokasi_farm: string;
  project_flock: string;
  kandang: string;
  file_name: string;
};

export type UniformitySampling = {
  chick_qty_of_weight: number;
  mean_weight: number;
  mean_down: number;
  mean_up: number;
};

export type UniformityResult = {
  uniform_qty: number;
  outside_qty: number;
  uniformity: number;
  cv: number;
};

export type UniformityDetailItem = {
  id: number;
  weight: number;
  range: 'Ideal' | 'Outside';
};

export type UniformityStandard = {
  mean_weight: number;
  uniformity: number;
} | null;

export type UniformityDetail = BaseMetadata & {
  id: number;
  info_umum: UniformityInfoUmum;
  sampling: UniformitySampling;
  result: UniformityResult;
  standard: UniformityStandard;
  uniformity_details?: UniformityDetailItem[];
  latest_approval?: BaseApproval;
};

// ==================== VERIFY RESPONSE ====================
export type VerifyUniformityResponse = {
  sampling: UniformitySampling;
  result: UniformityResult;
  uniformity_details: UniformityDetailItem[];
};

// ==================== PAYLOADS ====================
export type CreateUniformityPayload = {
  date: string;
  project_flock_kandang_id: number;
  document: File;
};

export type VerifyUniformityPayload = {
  document: File;
};

// ==================== OTHER TYPES ====================
export type BodyWeightData = {
  id: string;
  number: number;
  weight: number;
  status?: 'ideal' | 'outside';
};

export type DetailOptionType = OptionType & {
  id: string;
};
