// =============================================================================
// 🧑‍💼 EMPLOYEE V2 TYPE DEFINITIONS
// =============================================================================
// Модульная типизация для системы управления сотрудниками V2
// Следует паттернам MODULAR_ARCHITECTURE_PATTERN.md

// =============================================================================
// БАЗОВЫЕ ТИПЫ И ENUMS
// =============================================================================

export enum EmployeeStatus {
  ACTIVE = 'ACTIVE',
  VACATION = 'VACATION',
  SICK = 'SICK', 
  FIRED = 'FIRED'
}

export enum ScheduleStatus {
  WORK = 'WORK',
  WEEKEND = 'WEEKEND',
  VACATION = 'VACATION',
  SICK = 'SICK',
  ABSENT = 'ABSENT'
}

// =============================================================================
// СТРУКТУРИРОВАННЫЕ ИНТЕРФЕЙСЫ
// =============================================================================

// Личная информация
export interface PersonalInfo {
  firstName: string
  lastName: string
  middleName?: string
  fullName: string // computed
  birthDate?: Date
  avatar?: string
}

// Паспортные данные
export interface DocumentsInfo {
  passportPhoto?: string
  passportSeries?: string
  passportNumber?: string
  passportIssued?: string
  passportDate?: Date
}

// Контактная информация
export interface ContactInfo {
  phone: string
  email?: string
  telegram?: string
  whatsapp?: string
  address?: string
  emergencyContact?: string
  emergencyPhone?: string
}

// Рабочая информация
export interface WorkInfo {
  position: string
  department?: string
  hireDate: Date
  salary?: number
  status: EmployeeStatus
}

// Метаданные
export interface Metadata {
  createdAt: Date
  updatedAt: Date
}

// =============================================================================
// ОСНОВНЫЕ СУЩНОСТИ V2
// =============================================================================

export interface EmployeeV2 {
  id: string
  personalInfo: PersonalInfo
  documentsInfo: DocumentsInfo
  contactInfo: ContactInfo
  workInfo: WorkInfo
  organizationId: string
  organization?: OrganizationInfo
  scheduleRecords?: EmployeeScheduleV2[]
  metadata: Metadata
}

export interface EmployeeScheduleV2 {
  id: string
  employeeId: string
  date: Date
  status: ScheduleStatus
  hoursWorked?: number
  overtimeHours?: number
  notes?: string
  metadata: Metadata
}

export interface OrganizationInfo {
  id: string
  name: string
  fullName?: string
  type: string
}

// =============================================================================
// ЗАПРОСЫ И МУТАЦИИ
// =============================================================================

// Фильтры для запросов
export interface EmployeesFilterInput {
  status?: EmployeeStatus[]
  department?: string
  search?: string
  dateFrom?: Date
  dateTo?: Date
  // Пагинация
  page?: number
  limit?: number
  sortBy?: EmployeeSortField
  sortOrder?: 'ASC' | 'DESC'
}

export enum EmployeeSortField {
  NAME = 'name',
  POSITION = 'position', 
  HIRE_DATE = 'hireDate',
  STATUS = 'status',
  CREATED_AT = 'createdAt'
}

// Ответ с пагинацией
export interface EmployeesResponse {
  items: EmployeeV2[]
  pagination: PaginationInfo
  stats?: EmployeeStats
}

export interface PaginationInfo {
  total: number
  page: number
  limit: number
  totalPages: number
}

export interface EmployeeStats {
  total: number
  active: number
  vacation: number
  sick: number
  fired: number
  averageSalary?: number
}

// =============================================================================
// ФОРМЫ И ВАЛИДАЦИЯ
// =============================================================================

export interface CreateEmployeeInput {
  personalInfo: Omit<PersonalInfo, 'fullName'>
  documentsInfo?: DocumentsInfo
  contactInfo: ContactInfo
  workInfo: Omit<WorkInfo, 'status'> // status по умолчанию ACTIVE
}

export interface UpdateEmployeeInput {
  personalInfo?: Partial<Omit<PersonalInfo, 'fullName'>>
  documentsInfo?: Partial<DocumentsInfo>
  contactInfo?: Partial<ContactInfo>
  workInfo?: Partial<WorkInfo>
}

export interface UpdateScheduleInput {
  employeeId: string
  date: Date
  status: ScheduleStatus
  hoursWorked?: number
  overtimeHours?: number
  notes?: string
}

// =============================================================================
// ОТВЕТЫ МУТАЦИЙ
// =============================================================================

export interface EmployeeMutationResponse {
  success: boolean
  message?: string
  employee?: EmployeeV2
  errors?: ValidationError[]
}

export interface ValidationError {
  field: string
  message: string
}

// =============================================================================
// UI КОМПОНЕНТЫ PROPS
// =============================================================================

export interface EmployeeListProps {
  employees: EmployeeV2[]
  loading?: boolean
  onEdit?: (employee: EmployeeV2) => void
  onDelete?: (id: string) => void
  onViewSchedule?: (employee: EmployeeV2) => void
}

export interface EmployeeFormProps {
  employee?: EmployeeV2 // для редактирования
  onSubmit: (data: CreateEmployeeInput | UpdateEmployeeInput) => Promise<void>
  onCancel: () => void
  loading?: boolean
}

export interface EmployeeScheduleProps {
  employee: EmployeeV2
  year: number
  month: number
  scheduleRecords: EmployeeScheduleV2[]
  onDayClick?: (date: Date, status: ScheduleStatus) => void
  onUpdate?: (data: UpdateScheduleInput) => Promise<void>
  loading?: boolean
}

export interface EmployeeFiltersProps {
  filters: EmployeesFilterInput
  onChange: (filters: EmployeesFilterInput) => void
  onReset?: () => void
}

// =============================================================================
// ХУКИ ТИПЫ
// =============================================================================

export interface UseEmployeeDataReturn {
  employees: EmployeeV2[]
  pagination: PaginationInfo | null
  stats: EmployeeStats | null
  loading: boolean
  error: Error | null
  refetch: () => void
}

export interface UseEmployeeCRUDReturn {
  createEmployee: (data: CreateEmployeeInput) => Promise<EmployeeMutationResponse>
  updateEmployee: (id: string, data: UpdateEmployeeInput) => Promise<EmployeeMutationResponse>
  deleteEmployee: (id: string) => Promise<boolean>
  loading: boolean
}

export interface UseEmployeeScheduleReturn {
  schedule: EmployeeScheduleV2[]
  loading: boolean
  error: Error | null
  updateDay: (data: UpdateScheduleInput) => Promise<void>
  refetch: () => void
}

// =============================================================================
// УТИЛИТЫ И ХЕЛПЕРЫ
// =============================================================================

export type EmployeeExportFormat = 'csv' | 'excel' | 'pdf'

export interface EmployeeExportOptions {
  format: EmployeeExportFormat
  fields?: (keyof EmployeeV2)[]
  includeSchedule?: boolean
  dateRange?: {
    from: Date
    to: Date
  }
}

// =============================================================================
// REALTIME СОБЫТИЯ
// =============================================================================

export interface EmployeeRealtimeEvent {
  type: 'employee:created' | 'employee:updated' | 'employee:deleted' | 'schedule:updated'
  payload: {
    employeeId: string
    organizationId: string
    data?: EmployeeV2 | EmployeeScheduleV2
  }
}