import { Request, Response } from 'express';
import * as platformService from '../services/platform.service.js';
import { logChange } from '../services/audit.service.js';
import { ApiResponse } from '../types/index.js';
import { pickPlatformUpdate } from '../utils/sanitize.js';

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

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

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

export async function updatePlatform(req: Request, res: Response): Promise<void> {
  try {
    // Pentest R110: Mass-Assignment-Whitelist (nur name/contactInfo/isActive).
    const platform = await platformService.updatePlatform(parseInt(req.params.id), pickPlatformUpdate(req.body));
    await logChange({
      req, action: 'UPDATE', resourceType: 'Platform',
      resourceId: platform.id.toString(),
      label: `Vertriebsplattform ${platform.name} aktualisiert`,
    });
    res.json({ success: true, data: platform } as ApiResponse);
  } catch (error) {
    res.status(400).json({
      success: false,
      error: error instanceof Error ? error.message : 'Fehler beim Aktualisieren der Vertriebsplattform',
    } as ApiResponse);
  }
}

export async function deletePlatform(req: Request, res: Response): Promise<void> {
  try {
    const platformId = parseInt(req.params.id);
    const platform = await platformService.getPlatformById(platformId);
    await platformService.deletePlatform(platformId);
    await logChange({
      req, action: 'DELETE', resourceType: 'Platform',
      resourceId: platformId.toString(),
      label: `Vertriebsplattform ${platform?.name || platformId} gelöscht`,
    });
    res.json({ success: true, message: 'Vertriebsplattform gelöscht' } as ApiResponse);
  } catch (error) {
    res.status(400).json({
      success: false,
      error: error instanceof Error ? error.message : 'Fehler beim Löschen der Vertriebsplattform',
    } as ApiResponse);
  }
}
