/**
 * Type definitions for HR module (Time Tracking & Absences)
 */

import type { User, Department, ApprovalStatus } from './index';

// =============================================================================
// TIME TRACKING TYPES
// =============================================================================

/** Time entry status */
export type TimeEntryStatus = 'CLOCKED_IN' | 'ON_BREAK' | 'CLOCKED_OUT';

/** Time entry type - must match TimeEntryType enum in Prisma schema */
export type TimeEntryType =
  | 'REGULAR'
  | 'OVERTIME'
  | 'HOLIDAY'
  | 'SICK'
  | 'VACATION'
  | 'CORRECTION';

/** Time entry for a single workday */
export interface TimeEntry {
  id: string;
  date: string;
  clockIn: string | null;
  clockOut: string | null;
  breakStart: string | null;
  breakEnd: string | null;
  totalBreakMinutes: number;
  totalWorkMinutes: number;
  overtimeMinutes: number;
  type: TimeEntryType;
  note: string | null;
  isApproved: boolean;
  createdAt: string;
  updatedAt: string;
  employee: User;
}

/** Current time tracking status */
export interface TimeTrackingStatus {
  status: TimeEntryStatus;
  currentEntry: TimeEntry | null;
  todayWorkMinutes: number;
  todayBreakMinutes: number;
  clockInTime: string | null;
  breakStartTime: string | null;
}

/** Monthly time summary */
export interface TimeSummary {
  employeeId: string;
  month: number;
  year: number;
  totalWorkHours: number;
  targetWorkHours: number;
  overtimeHours: number;
  balanceHours: number;
  workDays: number;
  sickDays: number;
  vacationDays: number;
  entries: TimeEntry[];
}

/** Time entry correction request */
export interface TimeEntryCorrectionDto {
  date: string;
  clockIn?: string;
  clockOut?: string;
  breakStart?: string;
  breakEnd?: string;
  note: string;
}

/** Time tracking query params */
export interface TimeTrackingQueryParams {
  employeeId?: string;
  dateFrom?: string;
  dateTo?: string;
  month?: number;
  year?: number;
  page?: number;
  limit?: number;
}

// =============================================================================
// ABSENCE TYPES
// =============================================================================

/** Absence type - must match AbsenceType enum in Prisma schema */
export type AbsenceType =
  | 'VACATION'
  | 'SICK'
  | 'SICK_CHILD'
  | 'SPECIAL_LEAVE'
  | 'UNPAID_LEAVE'
  | 'PARENTAL_LEAVE'
  | 'HOME_OFFICE'
  | 'BUSINESS_TRIP'
  | 'TRAINING'
  | 'COMPENSATION';

/** Absence request status */
export type AbsenceRequestStatus = 'PENDING' | 'APPROVED' | 'REJECTED' | 'CANCELLED';

/** Single absence entry */
export interface Absence {
  id: string;
  type: AbsenceType;
  startDate: string;
  endDate: string;
  days: number;
  halfDay: boolean;
  halfDayPeriod: 'MORNING' | 'AFTERNOON' | null;
  note: string | null;
  status: AbsenceRequestStatus;
  approvedAt: string | null;
  approvedBy: User | null;
  rejectedAt: string | null;
  rejectedBy: User | null;
  rejectionReason: string | null;
  createdAt: string;
  updatedAt: string;
  employee: User;
}

/** Vacation balance / quota */
export interface VacationBalance {
  employeeId: string;
  year: number;
  totalDays: number;
  usedDays: number;
  pendingDays: number;
  remainingDays: number;
  carriedOverDays: number;
  expiringDays: number;
  expirationDate: string | null;
}

/** Create absence request */
export interface CreateAbsenceDto {
  type: AbsenceType;
  startDate: string;
  endDate: string;
  halfDay?: boolean;
  halfDayPeriod?: 'MORNING' | 'AFTERNOON';
  note?: string;
}

/** Absence query params */
export interface AbsenceQueryParams {
  employeeId?: string;
  departmentId?: string;
  type?: AbsenceType;
  status?: AbsenceRequestStatus;
  dateFrom?: string;
  dateTo?: string;
  page?: number;
  limit?: number;
}

/** Calendar entry for absence overview */
export interface AbsenceCalendarEntry {
  date: string;
  absences: Array<{
    id: string;
    type: AbsenceType;
    employee: {
      id: string;
      firstName: string;
      lastName: string;
      avatar: string | null;
    };
    isHalfDay: boolean;
  }>;
}

// =============================================================================
// HELPER TYPES & CONSTANTS
// =============================================================================

/** Absence type display info */
export const ABSENCE_TYPE_INFO: Record<
  AbsenceType,
  { labelKey: string; color: string; bgColor: string; icon: string }
> = {
  VACATION: {
    labelKey: 'vacation',
    color: 'text-blue-600',
    bgColor: 'bg-blue-100',
    icon: 'Palmtree',
  },
  SICK: {
    labelKey: 'sick',
    color: 'text-red-600',
    bgColor: 'bg-red-100',
    icon: 'Thermometer',
  },
  SICK_CHILD: {
    labelKey: 'sickChild',
    color: 'text-red-500',
    bgColor: 'bg-red-50',
    icon: 'Baby',
  },
  HOME_OFFICE: {
    labelKey: 'homeOffice',
    color: 'text-green-600',
    bgColor: 'bg-green-100',
    icon: 'Home',
  },
  SPECIAL_LEAVE: {
    labelKey: 'specialLeave',
    color: 'text-purple-600',
    bgColor: 'bg-purple-100',
    icon: 'Star',
  },
  UNPAID_LEAVE: {
    labelKey: 'unpaidLeave',
    color: 'text-orange-600',
    bgColor: 'bg-orange-100',
    icon: 'CalendarOff',
  },
  PARENTAL_LEAVE: {
    labelKey: 'parentalLeave',
    color: 'text-pink-600',
    bgColor: 'bg-pink-100',
    icon: 'Heart',
  },
  BUSINESS_TRIP: {
    labelKey: 'businessTrip',
    color: 'text-indigo-600',
    bgColor: 'bg-indigo-100',
    icon: 'Plane',
  },
  TRAINING: {
    labelKey: 'training',
    color: 'text-cyan-600',
    bgColor: 'bg-cyan-100',
    icon: 'GraduationCap',
  },
  COMPENSATION: {
    labelKey: 'compensation',
    color: 'text-teal-600',
    bgColor: 'bg-teal-100',
    icon: 'Clock',
  },
};

/** Absence status display info */
export const ABSENCE_STATUS_INFO: Record<
  AbsenceRequestStatus,
  { labelKey: string; color: string; bgColor: string }
> = {
  PENDING: {
    labelKey: 'pending',
    color: 'text-yellow-600',
    bgColor: 'bg-yellow-100',
  },
  APPROVED: {
    labelKey: 'approved',
    color: 'text-green-600',
    bgColor: 'bg-green-100',
  },
  REJECTED: {
    labelKey: 'rejected',
    color: 'text-red-600',
    bgColor: 'bg-red-100',
  },
  CANCELLED: {
    labelKey: 'cancelled',
    color: 'text-gray-600',
    bgColor: 'bg-gray-100',
  },
};

/** Time entry status display info */
export const TIME_STATUS_INFO: Record<
  TimeEntryStatus,
  { labelKey: string; color: string; bgColor: string }
> = {
  CLOCKED_IN: {
    labelKey: 'clockedIn',
    color: 'text-green-600',
    bgColor: 'bg-green-100',
  },
  ON_BREAK: {
    labelKey: 'onBreak',
    color: 'text-yellow-600',
    bgColor: 'bg-yellow-100',
  },
  CLOCKED_OUT: {
    labelKey: 'clockedOut',
    color: 'text-gray-600',
    bgColor: 'bg-gray-100',
  },
};
