// Time rule types for shift scheduling
export type TimeRuleType = 'strict' | 'flexible';
export type ShiftTimeRuleType = 'strict' | 'flexible' | 'inherit';

export interface ShiftAssignment {
  assignment_id: number;
  user_id: number;
  user_name: string;
  user_avatar: string;
  user_photourl?: string | null;
  role_id?: number;
  role_name?: string;
  role_color?: string;
  status: 'assigned' | 'accepted' | 'declined' | 'completed';
  is_unassigned?: boolean;
  planner_notes?: string | null;
  is_lead?: boolean;
}

export interface Shift {
  zs_id: number;
  zs_template_id?: number;
  zs_ber_id?: number; // Department ID
  zs_date: string; // YYYY-MM-DD
  zs_start_time: string; // HH:mm
  zs_end_time: string; // HH:mm
  // Time rules (override template settings)
  zs_time_rule_type?: ShiftTimeRuleType;
  zs_early_clock_in_buffer_minutes?: number | null;
  zs_auto_clock_out_enabled?: boolean | null;
  zs_name?: string;
  zs_notes?: string;
  zs_status: 'planned' | 'confirmed' | 'completed' | 'cancelled';
  zs_color?: string;
  zs_active?: boolean;
  display_name: string;
  display_color: string;
  assigned_count: number;
  duration_hours?: number;
  day_of_week?: number; // 0=Monday, 6=Sunday
  assigned_users: ShiftAssignment[];
  template?: ShiftTemplate;
  department?: Department;
  // Effective time rules (computed from shift + template)
  effective_time_rules?: {
    time_rule_type: TimeRuleType;
    early_clock_in_buffer_minutes: number;
    auto_clock_out_enabled: boolean;
    auto_clock_out_time: string | null; // ISO8601
  };
  created_at: string;
  updated_at: string;
}

export interface TemplateScheduleEntry {
  zsts_id?: number;
  zsts_day: number; // 0=Mon, 1=Tue ... 6=Sun
  zsts_start_time: string; // HH:mm
  zsts_end_time: string; // HH:mm
  zsts_label?: string | null;
  zsts_color?: string | null;
  zsts_min_workers?: number | null;
  zsts_max_workers?: number | null;
  zsts_sort_order?: number;
  day_name?: string;
}

export interface ShiftTemplate {
  zst_id: number;
  zst_name: string;
  zst_description?: string;
  zst_start_time: string;
  zst_end_time: string;
  // Time rules
  zst_time_rule_type?: TimeRuleType;
  zst_early_clock_in_buffer_minutes?: number;
  zst_auto_clock_out_enabled?: boolean;
  zst_day_mask: number;
  zst_min_workers: number;
  zst_max_workers: number;
  zst_must_have_roles?: RoleRequirement[];
  zst_nice_to_have_roles?: RoleRequirement[];
  zst_color: string;
  zst_active: boolean;
  zst_holiday_mode?: boolean;
  zst_parent_template_id?: number | null;
  // Season dates
  zst_valid_from?: string | null; // YYYY-MM-DD
  zst_valid_to?: string | null; // YYYY-MM-DD
  days_list: string[];
  must_have_roles_details?: RoleRequirementDetail[];
  nice_to_have_roles_details?: RoleRequirementDetail[];
  is_global?: boolean;
  is_advanced?: boolean;
  department_ids?: number[];
  department_names?: string[];
  holidays_list?: HolidayInfo[];
  holiday_ids?: number[];
  schedules_list?: TemplateScheduleEntry[];
  child_templates?: ShiftTemplate[]; // API returns snake_case
  parent_template?: ShiftTemplate;   // API returns snake_case
  child_templates_count?: number;
  parent_template_name?: string;
}

export interface HolidayInfo {
  id: number;
  name: string;
  date: string; // Formatted like "01.01" or "Easter +1"
  bundesland?: string | null;
  calculation_type: 'fixed' | 'easter_relative';
}

export interface RoleRequirement {
  role_id: number;
  count: number;
}

export interface RoleRequirementDetail {
  role_id: number;
  count: number;
  role: {
    id: number;
    name: string;
    color: string;
  } | null;
}

export interface User {
  usr_id: number;
  usr_Vorname: string; // Note: Capital letters match backend
  usr_Name: string; // Note: Capital letters match backend
  usr_email?: string;
  full_name: string;
  initials: string;
  photourl?: string | null;
  departments?: string[];
  department_string?: string;
  roles?: UserRole[];
  available?: boolean;
  rest_violation?: boolean;
  on_sick_leave?: boolean;
  on_vacation?: boolean;
  // Hours tracking
  planned_hours?: number;
  required_hours_per_month?: number;
  required_hours_per_week?: number;
  allow_overtime?: boolean;
  team_ids?: number[];
  conflict?: {
    type?: 'rest_time' | 'sick_leave' | 'vacation';
    shift?: {
      id: number;
      date: string;
      name: string;
      start_time: string;
      end_time: string;
      end_datetime: string;
    };
    rest_hours?: number;
    required_hours?: number;
    message: string;
  };
}

export interface UserRole {
  role_id: number;
  role_name: string;
  role_color: string;
}

export interface Role {
  zr_id: number;
  zr_name: string;
  zr_description?: string;
  zr_color: string;
  zr_active: boolean;
}

export interface WeekInfo {
  year: number;
  week: number;
  startDate: Date;
  endDate: Date;
  days: DayInfo[];
}

export interface DayInfo {
  date: Date;
  dayName: string;
  dayOfMonth: number;
  month: string;
  isToday: boolean;
  dateString: string; // YYYY-MM-DD
}

export interface Department {
  ber_id: number;
  ber_name: string;
  ber_bundesland?: string | null;
  bundesland_name?: string | null;
}

export interface Group {
  usg_id: number;
  usg_name: string;
  usg_dep: number;
}

export interface Break {
  id: number;
  start_time: string;
  end_time: string | null;
  duration_minutes: number | null;
  is_active: boolean;
  method?: string;
}

export interface PunctualityMetrics {
  late_minutes: number;
  early_minutes: number;
  early_departure_minutes: number;
  overtime_minutes: number;
  status: 'on_time' | 'late' | 'very_late' | 'early';
  shift_start_time: string;
  shift_end_time: string;
  actual_clock_in: string;
  actual_clock_out: string | null;
}

export interface WorkSession {
  zfws_id: number;
  zfws_assignment_id: number;
  zfws_client_uuid?: string;
  zfws_clock_in_time: string; // ISO8601
  zfws_clock_in_method: 'mobile' | 'web' | 'manual';
  zfws_clock_in_location_valid: boolean;
  zfws_clock_in_location_id?: number | null;
  zfws_clock_out_time?: string | null; // ISO8601
  zfws_clock_out_method?: 'mobile' | 'web' | 'manual' | 'auto' | null;
  zfws_clock_out_location_valid?: boolean;
  zfws_clock_out_location_id?: number | null;
  // Planned-window snapshot (Soll), used to clip paid time to the shift
  zfws_should_start_time?: string | null; // ISO8601
  zfws_should_end_time?: string | null; // ISO8601
  // Auto clock-out fields
  zfws_scheduled_auto_clock_out?: string | null; // ISO8601
  zfws_auto_clocked_out?: boolean;
  zfws_break_duration: number; // minutes
  zfws_is_on_break: boolean;
  zfws_notes?: string | null;
  // Computed
  worked_hours?: number | null;
}

// Clock-in response from API
export interface ClockInResponse {
  success: boolean;
  message: string;
  session: {
    sessionId: number;
    assignmentId: number;
    clockInTime: string; // ISO8601
    locationValid: boolean;
    distance?: number | null;
    scheduledAutoClockOut?: string | null; // ISO8601
  };
}

// Clock-in error for strict time rules
export interface ClockInTooEarlyError {
  success: false;
  error: 'clock_in_too_early';
  message: string;
  earliest_clock_in: string; // ISO8601
  shift_start: string; // ISO8601
  shift_name?: string;
  buffer_minutes: number;
}

// Shift template cascade (apply template edits to existing shifts)
export type ShiftCascadeScope = 'none' | 'future_unassigned' | 'future_all' | 'range';

export interface ShiftCascadeRange {
  from: string; // Y-m-d
  to: string; // Y-m-d
}

export interface ShiftCascadePreview {
  changed_fields: string[];
  future_unassigned_count: number;
  future_assigned_count: number;
  future_all_count: number;
  past_correctable_count: number;
  repriceable_sessions_count: number;
  skipped_manual_count: number;
  skipped_protected_count: number;
  default_range: ShiftCascadeRange | null;
}

export interface ShiftCascadeOutcome {
  scope: ShiftCascadeScope;
  updated_count: number;
  skipped_manual_count: number;
  skipped_protected_count: number;
  repriced_sessions_count: number;
}
