import { Request, Response } from 'express';
import * as contractDurationService from '../services/contract-duration.service.js';
import { logChange } from '../services/audit.service.js';
import { ApiResponse } from '../types/index.js';
import { pickContractDurationUpdate } from '../utils/sanitize.js';

export async function getContractDurations(req: Request, res: Response): Promise<void> {
  try {
    const includeInactive = req.query.includeInactive === 'true';
    const durations = await contractDurationService.getAllContractDurations(includeInactive);
    res.json({ success: true, data: durations } as ApiResponse);
  } catch (error) {
    res.status(500).json({
      success: false,
      error: 'Fehler beim Laden der Laufzeiten',
    } as ApiResponse);
  }
}

export async function getContractDuration(req: Request, res: Response): Promise<void> {
  try {
    const duration = await contractDurationService.getContractDurationById(parseInt(req.params.id));
    if (!duration) {
      res.status(404).json({
        success: false,
        error: 'Laufzeit nicht gefunden',
      } as ApiResponse);
      return;
    }
    res.json({ success: true, data: duration } as ApiResponse);
  } catch (error) {
    res.status(500).json({
      success: false,
      error: 'Fehler beim Laden der Laufzeit',
    } as ApiResponse);
  }
}

export async function createContractDuration(req: Request, res: Response): Promise<void> {
  try {
    const duration = await contractDurationService.createContractDuration(req.body);
    await logChange({
      req, action: 'CREATE', resourceType: 'ContractDuration',
      resourceId: duration.id.toString(),
      label: `Laufzeit ${duration.description} angelegt`,
    });
    res.status(201).json({ success: true, data: duration } as ApiResponse);
  } catch (error) {
    res.status(400).json({
      success: false,
      error: error instanceof Error ? error.message : 'Fehler beim Erstellen der Laufzeit',
    } as ApiResponse);
  }
}

export async function updateContractDuration(req: Request, res: Response): Promise<void> {
  try {
    // Pentest R110: Mass-Assignment-Whitelist.
    const duration = await contractDurationService.updateContractDuration(parseInt(req.params.id), pickContractDurationUpdate(req.body));
    await logChange({
      req, action: 'UPDATE', resourceType: 'ContractDuration',
      resourceId: duration.id.toString(),
      label: `Laufzeit ${duration.description} aktualisiert`,
    });
    res.json({ success: true, data: duration } as ApiResponse);
  } catch (error) {
    res.status(400).json({
      success: false,
      error: error instanceof Error ? error.message : 'Fehler beim Aktualisieren der Laufzeit',
    } as ApiResponse);
  }
}

export async function deleteContractDuration(req: Request, res: Response): Promise<void> {
  try {
    const durationId = parseInt(req.params.id);
    const duration = await contractDurationService.getContractDurationById(durationId);
    await contractDurationService.deleteContractDuration(durationId);
    await logChange({
      req, action: 'DELETE', resourceType: 'ContractDuration',
      resourceId: durationId.toString(),
      label: `Laufzeit ${duration?.description || durationId} gelöscht`,
    });
    res.json({ success: true, message: 'Laufzeit gelöscht' } as ApiResponse);
  } catch (error) {
    res.status(400).json({
      success: false,
      error: error instanceof Error ? error.message : 'Fehler beim Löschen der Laufzeit',
    } as ApiResponse);
  }
}
