<?php
// backend/lib/pdf_generator.php
require_once('../vendor/autoload.php'); // DomPDF via Composer

use Dompdf\Dompdf;
use Dompdf\Options;

class PDFGenerator {
    private $dompdf;
    
    public function __construct() {
        $options = new Options();
        $options->set('defaultFont', 'Helvetica');
        $options->set('isHtml5ParserEnabled', true);
        $this->dompdf = new Dompdf($options);
    }
    
    public function generate($data, $template) {
        $html = $this->renderTemplate($data, $template);
        $this->dompdf->loadHtml($html);
        $this->dompdf->setPaper('A4', 'portrait');
        $this->dompdf->render();
        return $this->dompdf->output();
    }
    
    private function renderTemplate($data, $template) {
        $data = json_decode($data, true);
        $personal = $data['personal'] ?? [];
        
        $html = '<!DOCTYPE html>
        <html>
        <head>
            <meta charset="utf-8">
            <style>
                body { font-family: Helvetica, Arial, sans-serif; margin: 0; padding: 40px; line-height: 1.6; }
                .container { max-width: 800px; margin: 0 auto; }
                h1 { color: #2c3e50; margin-bottom: 5px; }
                h2 { color: #34495e; border-bottom: 2px solid #3498db; padding-bottom: 5px; margin-top: 20px; }
                .contact { color: #7f8c8d; margin-bottom: 20px; }
                .section { margin-bottom: 25px; }
                .job, .education { margin-bottom: 15px; }
                .job-title { font-weight: bold; }
                .date { color: #7f8c8d; font-size: 0.9em; }
            </style>
        </head>
        <body>
        <div class="container">
            <h1>' . htmlspecialchars($personal['name'] ?? '') . '</h1>
            <div class="contact">
                ' . htmlspecialchars($personal['title'] ?? '') . '<br>
                ' . htmlspecialchars($personal['email'] ?? '') . ' | ' . htmlspecialchars($personal['phone'] ?? '') . '
            </div>';
            
        if (!empty($data['summary'])) {
            $html .= '<div class="section">
                <h2>Professional Summary</h2>
                <p>' . htmlspecialchars($data['summary']) . '</p>
            </div>';
        }
        
        if (!empty($data['skills'])) {
            $html .= '<div class="section">
                <h2>Skills</h2>
                <p>' . htmlspecialchars(implode(' • ', $data['skills'])) . '</p>
            </div>';
        }
        
        if (!empty($data['experience'])) {
            $html .= '<div class="section"><h2>Experience</h2>';
            foreach ($data['experience'] as $job) {
                $html .= '<div class="job">
                    <div class="job-title">' . htmlspecialchars($job['position'] ?? '') . ' at ' . htmlspecialchars($job['company'] ?? '') . '</div>
                    <div class="date">' . htmlspecialchars($job['start_date'] ?? '') . ' - ' . htmlspecialchars($job['end_date'] ?? '') . '</div>
                    <p>' . htmlspecialchars($job['description'] ?? '') . '</p>
                </div>';
            }
            $html .= '</div>';
        }
        
        $html .= '</div></body></html>';
        return $html;
    }
}
?>