<?php
/**
 * ComptaFlow - Actions PDF
 * Gere les endpoints de telechargement/visualisation PDF
 */

namespace ComptaFlowPro\Admin\Actions;

use ComptaFlowPro\Services\PDFGenerator;

defined('ABSPATH') || exit;

class PDFActions {

    /**
     * Initialiser les hooks
     */
    public static function boot(): void {
        add_action('admin_post_comptaflow_download_pdf', [self::class, 'handleDownload']);
        add_action('admin_post_comptaflow_view_pdf', [self::class, 'handleView']);
    }

    /**
     * Gerer le telechargement PDF
     */
    public static function handleDownload(): void {
        $type = isset($_GET['type']) ? sanitize_text_field(wp_unslash($_GET['type'])) : '';
        $id = isset($_GET['id']) ? absint($_GET['id']) : 0;

        if (empty($type) || empty($id)) {
            wp_die('Parametres manquants.');
        }

        PDFGenerator::download($type, $id);
    }

    /**
     * Gerer la visualisation PDF
     */
    public static function handleView(): void {
        $type = isset($_GET['type']) ? sanitize_text_field(wp_unslash($_GET['type'])) : '';
        $id = isset($_GET['id']) ? absint($_GET['id']) : 0;

        if (empty($type) || empty($id)) {
            wp_die('Parametres manquants.');
        }

        PDFGenerator::view($type, $id);
    }
}
