import { BaseMetadata, Document } from '@/types/api/api-general';
import { BaseKandang } from '@/types/api/master-data/kandang';
import { Phase } from '@/types/api/daily-checklist/phase';
import { PhaseActivity } from '@/types/api/daily-checklist/phase-activity';
import { BaseArea } from '@/types/api/master-data/area';
import { BaseLocation } from '@/types/api/master-data/location';
import { BaseEmployee } from '@/types/api/master-data/employee';

export type BaseDailyChecklist = {
  id: number;
  name: string;
  status: string;
  category: string;
  date: string;
  empty_kandang?: {
    id: boolean;
    start_date: string;
    end_date: string;
  };
  kandang?: Pick<BaseKandang, 'id' | 'name' | 'status' | 'capacity'>;
  total_phase: number;
  total_activity: number;
  progress: number;
};

export type DailyChecklist = BaseMetadata & BaseDailyChecklist;

export type DetailDailyChecklist = BaseDailyChecklist & {
  reject_reason: string | null;
  phases: {
    id: number;
    phase_id: number;
    phase: Phase;
  }[];
  tasks: {
    id: number;
    checklist_id: number;
    phase_id: number;
    phase_activity_id: number;
    time_type: string;
    notes: string | null;
    phase: Phase;
    phase_activity: PhaseActivity;
    assignments: {
      employee: {
        id: number;
        name: string;
      };
      checked: boolean;
      note: string | null;
    }[];
  }[];
  assigned_employees: {
    id: number;
    name: string;
  }[];
  document_urls: Document[];
};

export type CreateDailyChecklistPayload = {
  date: string;
  kandang_id: number;
  category: string;
  status: string;
  empty_kandang: boolean;
  empty_kandang_end_date?: string | null;
};

export type UpdateDailyChecklistPayload = CreateDailyChecklistPayload;

export type PerformanceOverviewItem = {
  employee_id: number;
  employee_name: string;
  total_activity: number;
  activity_done: number;
  activity_left: number;
  kandang: Pick<BaseKandang, 'id' | 'name'>;
};

export type TrackingAbkItem = {
  employee_id: number;
  employee_name: string;
  kandang_id: number;
  kandang_name: string;
  total_activity: number;
  activity_done: number;
  activity_left: number;
  completion_rate: number;
  last_activity: string;
};

export type DailyChecklistSummary = {
  performance_overview: PerformanceOverviewItem[];
  tracking_abk: TrackingAbkItem[];
};

export type DailyChecklistReport = {
  area: Pick<BaseArea, 'id' | 'name'>;
  farm: Pick<BaseLocation, 'id' | 'name'>;
  kandang: Pick<BaseKandang, 'id' | 'name'>;
  abk: Pick<BaseEmployee, 'id' | 'name'>;
  phase: string;
  daily_activities: Record<string, number>;
  summary: {
    total_checklist: number;
    jumlah_hari_efektif: number;
    abk_percentage: number;
    kandang_percentage: number;
    kategori: {
      kurang: number;
      cukup: number;
      baik: number;
    };
  };
};
