import { createEmployee as repoCreate, updateEmployee as repoUpdate, deleteEmployee as repoDelete, getAllWithDetails, getByIdWithDetails } from '../repositories/employeeRepository'
import { syncService } from '../services/syncService'
import { logSecurityAudit } from '../services/auditService'
import type { Request } from 'express'

export function listEmployeesUC() {
  return getAllWithDetails()
}

export function getEmployeeUC(id: string) {
  return getByIdWithDetails(id)
}

export async function createEmployeeUC(req: Request, body: any, actorUserId: string) {
  const result = repoCreate({
    firstName: body.firstName,
    lastName: body.lastName,
    employeeNumber: body.employeeNumber,
    photo: body.photo,
    position: body.position,
    officialTitle: body.officialTitle,
    department: body.department,
    email: body.email,
    phone: body.phone,
    mobile: body.mobile,
    office: body.office,
    availability: body.availability,
    clearance: body.clearance,
    skills: body.skills || [],
    languages: body.languages || [],
    specializations: body.specializations || [],
  }, actorUserId)

  logSecurityAudit('create', 'employees', result.id, actorUserId, req, 'medium')

  const now = new Date().toISOString()
  const newEmployee = {
    id: result.id,
    firstName: body.firstName,
    lastName: body.lastName,
    employeeNumber: body.employeeNumber || null,
    photo: body.photo || null,
    position: body.position || 'Teammitglied',
    officialTitle: body.officialTitle || null,
    department: body.department,
    email: body.email,
    phone: body.phone || 'Nicht angegeben',
    mobile: body.mobile || null,
    office: body.office || null,
    availability: body.availability || 'available',
    clearance: body.clearance,
    skills: body.skills || [],
    languages: body.languages || [],
    specializations: body.specializations || [],
    createdAt: now,
    updatedAt: now,
    createdBy: actorUserId
  }

  syncService.queueSync('employees', 'create', newEmployee).catch(() => {})

  return result
}

export async function updateEmployeeUC(req: Request, id: string, body: any, actorUserId: string) {
  repoUpdate(id, {
    firstName: body.firstName,
    lastName: body.lastName,
    position: body.position,
    officialTitle: body.officialTitle,
    department: body.department,
    email: body.email,
    phone: body.phone,
    mobile: body.mobile,
    office: body.office,
    availability: body.availability,
    clearance: body.clearance,
    skills: body.skills,
    languages: body.languages,
    specializations: body.specializations,
  }, actorUserId)

  logSecurityAudit('update', 'employees', id, actorUserId, req, 'medium')

  const updatedEmployee = {
    id,
    firstName: body.firstName,
    lastName: body.lastName,
    position: body.position,
    officialTitle: body.officialTitle,
    department: body.department,
    email: body.email,
    phone: body.phone,
    mobile: body.mobile || null,
    office: body.office || null,
    availability: body.availability,
    clearance: body.clearance,
    skills: body.skills,
    languages: body.languages,
    specializations: body.specializations,
    updatedAt: new Date().toISOString(),
    updatedBy: actorUserId
  }

  syncService.queueSync('employees', 'update', updatedEmployee).catch(() => {})
}

export async function deleteEmployeeUC(req: Request, id: string, actorUserId: string) {
  repoDelete(id)
  logSecurityAudit('delete', 'employees', id, actorUserId, req, 'high')
  syncService.queueSync('employees', 'delete', { id }).catch(() => {})
}

