export type AppraisalStatus = 'Pending Self-Assessment' | 'Submitted' | 'Pending HOD Approval' | 'Approved by HOD' | 'Completed';

export type CompanyGoal = {
  id: string;
  kra: string;
  title: string;
  description: string;
  target: string;
  fiscalYear: string;
};

export type DepartmentGoal = {
  id: string;
  department: string;
  title: string;
  description: string;
  alignedToCompanyGoalId: string;
  fiscalYear: string;
};

export type GoalRating = {
  goalId: string;
  selfRating: number;
  selfComment: string;
  hodRating?: number;
  hodComment?: string;
};

export type Appraisal = {
  id: string;
  employeeId: string;
  name: string;
  dept: string;
  role: string;
  employeeType: 'Employee' | 'Intern';
  hodName: string;
  period: string;
  status: AppraisalStatus;
  overallRating: number;
  avatar: string;
  createdDate: string;
  submittedDate?: string;
  hodApprovedDate?: string;
  companyGoalRatings: GoalRating[];
  deptGoalRatings: GoalRating[];
  selfAssessmentComments?: string;
  hodComments?: string;
  pipStatus?: 'Active' | 'Completed' | 'None';
};

export const COMPANY_GOALS: CompanyGoal[] = [
  {
    id: 'CG1',
    kra: 'Business Development Services',
    title: 'Reach 6,000 beneficiaries with business development services',
    description: 'Provide business development services to at least 6,000 beneficiaries, including 7 benefits/year, 8 banking partnerships, 9 coordination activities, and 10 certifications.',
    target: '6,000 beneficiaries, 7 benefits, 8 banking, 9 coordination, 10 certification',
    fiscalYear: '2025-2026',
  },
  {
    id: 'CG2',
    kra: 'Youth Skills Training',
    title: 'Train 10,870 youth online using digital tools',
    description: 'Deliver skills training to 10,870 youth through online platforms and digital tools.',
    target: '10,870 youth trained online',
    fiscalYear: '2025-2026',
  },
  {
    id: 'CG3',
    kra: 'Certificate Issuance',
    title: 'Issue certificates online and in paper format',
    description: 'Develop and implement a dual-mode certificate issuance system (online and paper) for all trained beneficiaries.',
    target: 'Online and paper certificates issued within 30 days of training completion',
    fiscalYear: '2025-2026',
  },
  {
    id: 'CG4',
    kra: 'Fund Visibility Enhancement',
    title: 'Execute KSH 1.84M media and visibility budget',
    description: 'Implement a comprehensive media and communications strategy to enhance Uwezo Fund visibility across Kenya.',
    target: 'KSH 1.84M media budget fully utilized, 40% increase in visibility',
    fiscalYear: '2025-2026',
  },
  {
    id: 'CG5',
    kra: 'Board Capacity Building',
    title: 'Build capacity of 44 board members',
    description: 'Conduct capacity building programs for all 44 board members to enhance governance and oversight.',
    target: '44 board members trained',
    fiscalYear: '2025-2026',
  },
  {
    id: 'CG6',
    kra: 'Monitoring & Evaluation',
    title: 'Conduct quarterly donor visits and M&E reporting',
    description: 'Facilitate quarterly donor visits and produce timely M&E reports to demonstrate programme impact.',
    target: '4 quarterly donor visits, M&E reports submitted on time',
    fiscalYear: '2025-2026',
  },
];

export const DEPARTMENT_GOALS: DepartmentGoal[] = [
  {
    id: 'DG-IT-1',
    department: 'ICT',
    title: 'Digitize the training management system',
    description: 'Develop and deploy a fully digital training management system to support online training delivery.',
    alignedToCompanyGoalId: 'CG2',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-IT-2',
    department: 'ICT',
    title: 'Reduce system downtime to below 0.5%',
    description: 'Maintain system uptime of 99.5% or above across all critical platforms.',
    alignedToCompanyGoalId: 'CG2',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-HR-1',
    department: 'HR',
    title: 'Reduce staff turnover by 15%',
    description: 'Implement retention strategies to reduce voluntary staff turnover by at least 15% compared to previous year.',
    alignedToCompanyGoalId: 'CG1',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-HR-2',
    department: 'HR',
    title: 'Support completion of 10,870 training records',
    description: 'Coordinate HR support for the training of 10,870 youth, including staff deployment and logistics.',
    alignedToCompanyGoalId: 'CG2',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-HR-3',
    department: 'HR',
    title: 'Issue certificates within 30 days of training',
    description: 'Ensure all training certificates are processed and issued within 30 days of training completion.',
    alignedToCompanyGoalId: 'CG3',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-FIN-1',
    department: 'Finance',
    title: 'Process all payments within 30 days',
    description: 'Ensure all approved payments are processed and disbursed within 30 days of approval.',
    alignedToCompanyGoalId: 'CG1',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-FIN-2',
    department: 'Finance',
    title: 'Submit donor reports on time',
    description: 'Prepare and submit all donor financial reports by the stipulated deadlines.',
    alignedToCompanyGoalId: 'CG6',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-FIN-3',
    department: 'Finance',
    title: 'Maintain budget variance below 5%',
    description: 'Monitor and control expenditure to ensure budget variance does not exceed 5% in any quarter.',
    alignedToCompanyGoalId: 'CG1',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-OPS-1',
    department: 'Operations',
    title: 'Coordinate 7 beneficiary events',
    description: 'Plan and execute 7 beneficiary engagement events across the country.',
    alignedToCompanyGoalId: 'CG1',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-OPS-2',
    department: 'Operations',
    title: 'Manage 8 banking partnerships',
    description: 'Maintain and grow relationships with 8 banking partners for fund disbursement.',
    alignedToCompanyGoalId: 'CG1',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-OPS-3',
    department: 'Operations',
    title: 'Execute 9 coordination activities',
    description: 'Coordinate 9 inter-departmental and stakeholder coordination activities.',
    alignedToCompanyGoalId: 'CG1',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-COMM-1',
    department: 'Communications',
    title: 'Execute KSH 1.84M media budget',
    description: 'Plan and execute all media and communications activities within the KSH 1.84M budget.',
    alignedToCompanyGoalId: 'CG4',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-COMM-2',
    department: 'Communications',
    title: 'Increase fund visibility by 40%',
    description: 'Achieve a 40% increase in Uwezo Fund visibility through media, social media, and events.',
    alignedToCompanyGoalId: 'CG4',
    fiscalYear: '2025-2026',
  },
  {
    id: 'DG-ME-1',
    department: 'M&E',
    title: 'Conduct quarterly donor visits',
    description: 'Facilitate and document 4 quarterly donor visits with full M&E reports.',
    alignedToCompanyGoalId: 'CG6',
    fiscalYear: '2025-2026',
  },
];

export const RATING_LABELS: Record<number, string> = {
  1: 'Needs Improvement',
  2: 'Meets Some Expectations',
  3: 'Meets Expectations',
  4: 'Exceeds Expectations',
  5: 'Outstanding',
};

export const RATING_COLORS: Record<number, string> = {
  1: 'text-red-600',
  2: 'text-orange-500',
  3: 'text-yellow-600',
  4: 'text-blue-600',
  5: 'text-green-600',
};

export const RATING_BG: Record<number, string> = {
  1: 'bg-red-100 text-red-700',
  2: 'bg-orange-100 text-orange-700',
  3: 'bg-yellow-100 text-yellow-700',
  4: 'bg-blue-100 text-blue-700',
  5: 'bg-green-100 text-green-700',
};

export const ALL_EMPLOYEES = [
  { id: 'UF001', name: 'Jane Mwangi', dept: 'M&E', role: 'M&E Officer', position: 'M&E Officer', employeeType: 'Employee' as const, hod: 'Dr. Amina Osei', avatar: 'JM' },
  { id: 'UF002', name: 'Peter Otieno', dept: 'ICT', role: 'Systems Administrator', position: 'Systems Administrator', employeeType: 'Employee' as const, hod: 'Mr. Brian Omondi', avatar: 'PO' },
  { id: 'UF003', name: 'Mary Kamau', dept: 'Communications', role: 'Communications Officer', position: 'Communications Officer', employeeType: 'Employee' as const, hod: 'Mr. Robert Kariuki', avatar: 'MK' },
  { id: 'UF004', name: 'David Njoroge', dept: 'Operations', role: 'Transport Officer', position: 'Transport Officer', employeeType: 'Employee' as const, hod: 'Ms. Grace Wanjiku', avatar: 'DN' },
  { id: 'UF005', name: 'Grace Wanjiku', dept: 'Operations', role: 'Operations Officer', position: 'Operations Officer', employeeType: 'Employee' as const, hod: 'Ms. Grace Wanjiku', avatar: 'GW' },
  { id: 'UF006', name: 'Samuel Kipchoge', dept: 'Operations', role: 'Driver', position: 'Driver', employeeType: 'Employee' as const, hod: 'Ms. Grace Wanjiku', avatar: 'SK' },
  { id: 'UF007', name: 'Fatuma Hassan', dept: 'M&E', role: 'Data Analyst', position: 'Data Analyst', employeeType: 'Employee' as const, hod: 'Dr. Amina Osei', avatar: 'FH' },
  { id: 'UF008', name: 'John Mutua', dept: 'Finance', role: 'Senior Finance Manager', position: 'Senior Finance Manager', employeeType: 'Employee' as const, hod: 'Mr. John Mutua', avatar: 'JM' },
  { id: 'UF009', name: 'Alice Odhiambo', dept: 'ICT', role: 'Software Developer', position: 'Software Developer', employeeType: 'Employee' as const, hod: 'Mr. Brian Omondi', avatar: 'AO' },
  { id: 'UF010', name: 'Robert Kariuki', dept: 'Communications', role: 'PR Specialist', position: 'PR Specialist', employeeType: 'Employee' as const, hod: 'Mr. Robert Kariuki', avatar: 'RK' },
  { id: 'UF011', name: 'Esther Njeri', dept: 'HR', role: 'HR Officer', position: 'HR Officer', employeeType: 'Employee' as const, hod: 'Ms. Esther Njeri', avatar: 'EN' },
  { id: 'UF012', name: 'Brian Omondi', dept: 'ICT', role: 'Head of ICT', position: 'Head of ICT', employeeType: 'Employee' as const, hod: 'Mr. Brian Omondi', avatar: 'BO' },
  { id: 'UF013', name: 'Kevin Maina', dept: 'Finance', role: 'Finance Intern', position: 'Finance Intern', employeeType: 'Intern' as const, hod: 'Mr. John Mutua', avatar: 'KM' },
  { id: 'UF014', name: 'Amina Osei', dept: 'M&E', role: 'Head of M&E', position: 'Head of M&E', employeeType: 'Employee' as const, hod: 'Dr. Amina Osei', avatar: 'AO' },
];

export const getDeptGoals = (dept: string): DepartmentGoal[] =>
  DEPARTMENT_GOALS.filter(g => g.department === dept);

export const initialAppraisals: Appraisal[] = [
  {
    id: 'AP001',
    employeeId: 'UF001',
    name: 'Jane Mwangi',
    dept: 'M&E',
    role: 'M&E Officer',
    employeeType: 'Employee',
    hodName: 'Dr. Amina Osei',
    period: 'Q1 2026',
    status: 'Approved by HOD',
    overallRating: 4.2,
    avatar: 'JM',
    createdDate: '2026-01-05',
    submittedDate: '2026-03-28',
    hodApprovedDate: '2026-04-02',
    companyGoalRatings: [
      { goalId: 'CG2', selfRating: 4, selfComment: 'Supported training data collection for 2,800 youth this quarter.', hodRating: 4, hodComment: 'Good contribution to training data.' },
      { goalId: 'CG6', selfRating: 5, selfComment: 'Facilitated all 4 donor visits with complete documentation.', hodRating: 5, hodComment: 'Excellent — all visits well documented.' },
    ],
    deptGoalRatings: [
      { goalId: 'DG-ME-1', selfRating: 5, selfComment: 'All 4 quarterly donor visits conducted and reported.', hodRating: 5, hodComment: 'Outstanding performance on donor visits.' },
    ],
    selfAssessmentComments: 'I am proud of my contribution to M&E this quarter. All donor visits were facilitated on time and reports submitted before deadlines.',
    hodComments: 'Jane has performed exceptionally well. Her M&E reports are thorough and timely. Recommend for recognition.',
    pipStatus: 'None',
  },
  {
    id: 'AP002',
    employeeId: 'UF002',
    name: 'Peter Otieno',
    dept: 'ICT',
    role: 'Systems Administrator',
    employeeType: 'Employee',
    hodName: 'Mr. Brian Omondi',
    period: 'Q1 2026',
    status: 'Approved by HOD',
    overallRating: 3.1,
    avatar: 'PO',
    createdDate: '2026-01-05',
    submittedDate: '2026-03-30',
    hodApprovedDate: '2026-04-05',
    companyGoalRatings: [
      { goalId: 'CG2', selfRating: 3, selfComment: 'Maintained training system with 98.8% uptime.', hodRating: 3, hodComment: 'Acceptable but needs improvement on response time.' },
    ],
    deptGoalRatings: [
      { goalId: 'DG-IT-1', selfRating: 3, selfComment: 'Contributed to digitization of training modules.', hodRating: 3, hodComment: 'Progress is slow. Needs to accelerate.' },
      { goalId: 'DG-IT-2', selfRating: 3, selfComment: 'System downtime was 1.2% — slightly above target.', hodRating: 3, hodComment: 'Did not meet the 0.5% target. Improvement needed.' },
    ],
    selfAssessmentComments: 'I maintained system stability but acknowledge the downtime target was not fully met. I will focus on proactive monitoring in Q2.',
    hodComments: 'Peter meets basic expectations but needs to improve on system reliability and proactive communication.',
    pipStatus: 'None',
  },
  {
    id: 'AP003',
    employeeId: 'UF007',
    name: 'Fatuma Hassan',
    dept: 'M&E',
    role: 'Data Analyst',
    employeeType: 'Employee',
    hodName: 'Dr. Amina Osei',
    period: 'Q1 2026',
    status: 'Pending HOD Approval',
    overallRating: 0,
    avatar: 'FH',
    createdDate: '2026-01-05',
    submittedDate: '2026-04-01',
    companyGoalRatings: [
      { goalId: 'CG2', selfRating: 4, selfComment: 'Built Power BI dashboard tracking 10,870 training target progress.' },
      { goalId: 'CG6', selfRating: 4, selfComment: 'Produced all 3 data quality reports for donor visits.' },
    ],
    deptGoalRatings: [
      { goalId: 'DG-ME-1', selfRating: 4, selfComment: 'Prepared data analysis reports for all 4 donor visits.' },
    ],
    selfAssessmentComments: 'I delivered all required data products on time. The Power BI dashboard has been well received by management.',
    pipStatus: 'None',
  },
  {
    id: 'AP004',
    employeeId: 'UF003',
    name: 'Mary Kamau',
    dept: 'Communications',
    role: 'Communications Officer',
    employeeType: 'Employee',
    hodName: 'Mr. Robert Kariuki',
    period: 'Q1 2026',
    status: 'Submitted',
    overallRating: 0,
    avatar: 'MK',
    createdDate: '2026-01-05',
    submittedDate: '2026-04-10',
    companyGoalRatings: [
      { goalId: 'CG4', selfRating: 4, selfComment: 'Executed Q1 media activities worth KSH 460,000 of the annual budget.' },
    ],
    deptGoalRatings: [
      { goalId: 'DG-COMM-1', selfRating: 4, selfComment: 'Q1 media budget of KSH 460K fully utilized.' },
      { goalId: 'DG-COMM-2', selfRating: 3, selfComment: 'Visibility increased by 22% in Q1 — on track for 40% annual target.' },
    ],
    selfAssessmentComments: 'Q1 communications activities were executed successfully. Social media engagement increased significantly.',
    pipStatus: 'None',
  },
  {
    id: 'AP005',
    employeeId: 'UF006',
    name: 'Samuel Kipchoge',
    dept: 'Operations',
    role: 'Driver',
    employeeType: 'Employee',
    hodName: 'Ms. Grace Wanjiku',
    period: 'Q1 2026',
    status: 'Approved by HOD',
    overallRating: 1.8,
    avatar: 'SK',
    createdDate: '2026-01-05',
    submittedDate: '2026-03-25',
    hodApprovedDate: '2026-04-01',
    companyGoalRatings: [
      { goalId: 'CG1', selfRating: 2, selfComment: 'Transported staff to 5 of 7 planned events.', hodRating: 2, hodComment: 'Missed 2 events due to vehicle issues and late reporting.' },
    ],
    deptGoalRatings: [
      { goalId: 'DG-OPS-1', selfRating: 2, selfComment: 'Supported 5 out of 7 events.', hodRating: 1, hodComment: 'Performance below expectations. Punctuality issues persist.' },
    ],
    selfAssessmentComments: 'I faced some challenges with vehicle maintenance this quarter but am committed to improving.',
    hodComments: 'Samuel continues to struggle with punctuality and reliability. A PIP has been initiated.',
    pipStatus: 'Active',
  },
  {
    id: 'AP006',
    employeeId: 'UF009',
    name: 'Alice Odhiambo',
    dept: 'ICT',
    role: 'Software Developer',
    employeeType: 'Employee',
    hodName: 'Mr. Brian Omondi',
    period: 'Q1 2026',
    status: 'Pending Self-Assessment',
    overallRating: 0,
    avatar: 'AO',
    createdDate: '2026-01-05',
    companyGoalRatings: [],
    deptGoalRatings: [],
    pipStatus: 'None',
  },
  {
    id: 'AP007',
    employeeId: 'UF013',
    name: 'Kevin Maina',
    dept: 'Finance',
    role: 'Finance Intern',
    employeeType: 'Intern',
    hodName: 'Mr. John Mutua',
    period: 'Q1 2026',
    status: 'Approved by HOD',
    overallRating: 3.5,
    avatar: 'KM',
    createdDate: '2026-01-05',
    submittedDate: '2026-03-29',
    hodApprovedDate: '2026-04-03',
    companyGoalRatings: [
      { goalId: 'CG1', selfRating: 3, selfComment: 'Assisted in processing 45 payment requests this quarter.', hodRating: 4, hodComment: 'Good work for an intern. Exceeded expectations.' },
    ],
    deptGoalRatings: [
      { goalId: 'DG-FIN-1', selfRating: 3, selfComment: 'Supported payment processing within 30-day target.', hodRating: 3, hodComment: 'Meets expectations for intern level.' },
    ],
    selfAssessmentComments: 'I have learned a lot this quarter and contributed to payment processing and donor report preparation.',
    hodComments: 'Kevin is a hardworking intern who shows great potential. Recommend for continued attachment.',
    pipStatus: 'None',
  },
  {
    id: 'AP008',
    employeeId: 'UF004',
    name: 'David Njoroge',
    dept: 'Operations',
    role: 'Transport Officer',
    employeeType: 'Employee',
    hodName: 'Ms. Grace Wanjiku',
    period: 'Q1 2026',
    status: 'Completed',
    overallRating: 4.5,
    avatar: 'DN',
    createdDate: '2026-01-05',
    submittedDate: '2026-03-20',
    hodApprovedDate: '2026-03-28',
    companyGoalRatings: [
      { goalId: 'CG1', selfRating: 5, selfComment: 'Coordinated transport for all 7 planned events successfully.', hodRating: 4, hodComment: 'Excellent coordination. Minor documentation gaps.' },
    ],
    deptGoalRatings: [
      { goalId: 'DG-OPS-1', selfRating: 5, selfComment: 'All 7 events supported with zero transport failures.', hodRating: 5, hodComment: 'Outstanding. All events covered on time.' },
      { goalId: 'DG-OPS-2', selfRating: 4, selfComment: 'Supported logistics for 6 of 8 banking partnership visits.', hodRating: 4, hodComment: 'Good performance.' },
    ],
    selfAssessmentComments: 'I am proud of the transport coordination this quarter. All events were covered without any major issues.',
    hodComments: 'David has shown excellent performance. His transport coordination has been a key enabler for our field activities.',
    pipStatus: 'None',
  },
];

export type PIP = {
  id: string;
  employeeId: string;
  employeeName: string;
  dept: string;
  hod: string;
  triggerRating: number;
  appraisalPeriod: string;
  startDate: string;
  endDate: string;
  duration: '30 days' | '60 days' | '90 days';
  improvementAreas: string[];
  actionPlan: string;
  successMetrics: string;
  status: 'Active' | 'Successful' | 'Extended' | 'Terminated';
  checkIns: { date: string; author: string; progress: number; notes: string }[];
};

export const initialPIPs: PIP[] = [
  {
    id: 'PIP-2026-001',
    employeeId: 'UF006',
    employeeName: 'Samuel Kipchoge',
    dept: 'Operations',
    hod: 'Ms. Grace Wanjiku',
    triggerRating: 1.8,
    appraisalPeriod: 'Q1 2026',
    startDate: '2026-04-10',
    endDate: '2026-07-10',
    duration: '90 days',
    improvementAreas: [
      'Punctuality and reliability — zero late departures',
      'Vehicle maintenance compliance — complete all checklists',
      'Communication with supervisor — daily check-ins',
    ],
    actionPlan: '1. Attend punctuality and professionalism training (Week 1-2)\n2. Complete vehicle maintenance certification (Month 1)\n3. Daily check-in with supervisor (ongoing)\n4. Zero unauthorized absences during PIP period\n5. Support all 7 planned events in Q2',
    successMetrics: 'Zero late departures in Month 3; 100% vehicle inspection compliance; All Q2 events supported; Positive supervisor check-in ratings',
    status: 'Active',
    checkIns: [
      { date: '2026-04-25', author: 'Ms. Grace Wanjiku', progress: 30, notes: 'Some improvement noted. 1 late arrival in first 2 weeks vs 5 in previous period. Vehicle checklists now being completed.' },
    ],
  },
];
