<?php
require_once 'vendor/autoload.php';
require_once 'db/config.php';

use Dompdf\Dompdf;
use Dompdf\Options;

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die('Invalid training session ID');
}

$session_id = $_GET['id'];

// Fetch session data from the database
$stmt = db()->prepare("SELECT * FROM training_sessions WHERE id = ?");
$stmt->execute([$session_id]);
$session = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$session) {
    die('Training session not found');
}

// Fetch associated drills
$stmt = db()->prepare("SELECT d.title, d.description, d.image_path, c.name as category_name FROM drills d JOIN training_session_drills tsd ON d.id = tsd.drill_id LEFT JOIN categories c ON d.category_id = c.id WHERE tsd.training_session_id = ?");
$stmt->execute([$session_id]);
$drills = $stmt->fetchAll(PDO::FETCH_ASSOC);

// HTML content for the PDF
$html = '
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Training Session Report</title>
    <style>
        @page {
            margin: 30px;
        }
        body {
            font-family: 'Helvetica', sans-serif;
            color: #333;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
        }
        .header img {
            max-width: 150px;
            margin-bottom: 10px;
        }
        .container {
            padding: 20px;
            border: 1px solid #eee;
            border-radius: 10px;
        }
        h1, h2 {
            color: #007bff;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }
        h1 {
            margin-top: 0;
        }
        h2 {
            margin-top: 30px;
        }
        p {
            line-height: 1.6;
            margin: 0 0 15px;
        }
        strong {
            color: #555;
        }
        .drill {
            border-bottom: 1px solid #ccc;
            padding-bottom: 15px;
            margin-bottom: 15px;
        }
        .drill:last-child {
            border-bottom: none;
        }
        .footer {
            text-align: center;
            margin-top: 30px;
            font-size: 0.9em;
            color: #777;
        }
        .drill-image {
            text-align: center;
            margin-top: 20px;
        }
        .drill-image img {
            max-width: 100%;
            height: auto;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div class="header">
        <?php
            $logo_path = __DIR__ . '/assets/images/logo.svg';
            if (file_exists($logo_path)) {
                $logo_data = base64_encode(file_get_contents($logo_path));
                $logo_mime = mime_content_type($logo_path);
                $logo_src = 'data:' . $logo_mime . ';base64,' . $logo_data;
            } else {
                $logo_src = '';
            }
        ?>
        <?php if ($logo_src): ?>
            <img src="<?php echo $logo_src; ?>" alt="Logo">
        <?php endif; ?>
        <h2>Training Session Report</h2>
    </div>
    <div class="container">
        <h1><?php echo htmlspecialchars($session['title']); ?></h1>
        <p><strong>Date:</strong> <?php echo htmlspecialchars($session['date']); ?></p>
        <p><strong>Notes:</strong></p>
        <div><?php echo nl2br(htmlspecialchars($session['notes'])); ?></div>
        
        <h2>Drills</h2>
        <?php foreach ($drills as $drill): ?>
            <div class="drill">
                <h3><?php echo htmlspecialchars($drill['title']); ?></h3>
                <p><strong>Category:</strong> <?php echo htmlspecialchars($drill['category_name']); ?></p>
                <p><strong>Description:</strong></p>
                <div><?php echo nl2br(htmlspecialchars($drill['description'])); ?></div>
                <?php if (!empty($drill['image_path'])):
                    $image_path = __DIR__ . '/' . $drill['image_path'];
                    if (file_exists($image_path)) {
                        $image_data = base64_encode(file_get_contents($image_path));
                        $image_mime = mime_content_type($image_path);
                        $image_src = 'data:' . $image_mime . ';base64,' . $image_data;
                    } else {
                        $image_src = '';
                    }
                ?>
                <?php if ($image_src): ?>
                    <div class="drill-image">
                        <img src="<?php echo $image_src; ?>" alt="Drill Image">
                    </div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
    <div class="footer">
        <p>Generated by Drillex on <?php echo date('Y-m-d H:i:s'); ?></p>
    </div>
</body>
</html>
';

$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("training_session_" . $session['id'] . ".pdf", ["Attachment" => false]);
