<?php
require_once 'config/config.php';
require_once 'includes/functions.php';
startSecureSession();

$pageTitle = 'Certificate Authentication & Verification';
$metaDesc = "Verify the authenticity of your Rig N Dig certificates, Material Test Reports (MTR), and Certificates of Conformance. Enter your serial number for instant validation.";
$pageKeywords = "Verify Certificate, MTR Validation, Industrial Certification Pakistan, API Authentication, Rig N Dig Authenticator";
include 'includes/header.php';

$search = isset($_GET['sn']) ? sanitize($_GET['sn']) : '';
$cert = null;
$error = '';

if ($search) {
    $cert = Database::fetchOne("
        SELECT c.*, i.invoice_number 
        FROM certificates c 
        LEFT JOIN invoices i ON c.invoice_id = i.id 
        WHERE c.serial_number = ? OR c.id = ?
    ", [$search, (int)$search]);

    if (!$cert) {
        $error = "No authentic certificate found for ID/Serial: <strong>" . e($search) . "</strong>. Please contact our technical desk if you believe this is an error.";
    }
}
?>

<div class="relative overflow-hidden pt-20 pb-32">
    <!-- Dynamic BG -->
    <div class="absolute top-0 left-0 w-full h-[600px] bg-gradient-to-b from-navy-800 to-navy-900 -z-10"></div>
    <div class="absolute top-20 left-1/2 -translate-x-1/2 w-[800px] h-[400px] bg-orange-600/5 rounded-full blur-[120px] -z-10"></div>

    <div class="max-w-4xl mx-auto px-4">
        <div class="text-center mb-16 fade-in">
            <div class="section-tag mx-auto mb-4">🛡️ Technical Integrity</div>
            <h1 class="text-5xl font-black text-white italic tracking-tighter uppercase mb-6 leading-none">
                Certificate <span class="text-orange-500">Authenticator</span>
            </h1>
            <p class="text-gray-400 max-w-xl mx-auto text-lg">
                Ensure the technical integrity of your industrial assets. Enter the Serial Number or Certificate ID found on your official document.
            </p>
        </div>

        <!-- Search Bar -->
        <div class="card-dark p-2 rounded-2xl mb-12 shadow-2xl shadow-black/50 fade-in">
            <form method="GET" class="flex flex-col sm:flex-row gap-2">
                <input type="text" name="sn" value="<?= e($search) ?>" 
                       placeholder="Enter Serial Number (e.g. SN-88291-Q)" 
                       class="flex-1 bg-transparent border-none text-white px-6 py-4 focus:ring-0 text-lg font-medium" required>
                <button type="submit" class="btn-primary px-10 py-4 font-black uppercase tracking-widest text-sm">Verify Now</button>
            </form>
        </div>

        <?php if ($error): ?>
            <div class="bg-red-900/20 border border-red-500/30 p-8 rounded-3xl text-center fade-in">
                <div class="text-5xl mb-4">⚠️</div>
                <h3 class="text-xl font-bold text-white mb-2 uppercase tracking-tight">Authentication Failed</h3>
                <p class="text-gray-400 text-sm leading-relaxed"><?= $error ?></p>
            </div>
        <?php elseif ($cert): ?>
            <div class="glass-panel p-8 sm:p-12 rounded-[40px] border-orange-500/20 relative overflow-hidden fade-in">
                <!-- Watermark -->
                <div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 text-[150px] font-black text-white/[0.02] pointer-events-none select-none uppercase tracking-tighter">AUTHENTIC</div>
                
                <div class="relative z-10">
                    <div class="flex flex-col sm:flex-row justify-between items-start gap-6 border-b border-white/10 pb-8 mb-8">
                        <div>
                            <div class="text-xs font-black text-orange-500 uppercase tracking-[0.3em] mb-2">Verified Technical Asset</div>
                            <h2 class="text-3xl font-black text-white uppercase tracking-tighter"><?= e($cert['title']) ?></h2>
                        </div>
                        <div class="bg-green-500 text-navy-900 px-6 py-2 rounded-full font-black uppercase text-[10px] tracking-widest flex items-center gap-2">
                            <span class="text-lg leading-none">✓</span> Verified Genuine
                        </div>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-10">
                        <div class="space-y-6">
                            <div>
                                <div class="text-[10px] text-gray-500 font-bold uppercase tracking-widest mb-1">Serial / Heat Number</div>
                                <div class="text-xl font-mono text-white"><?= e($cert['serial_number']) ?></div>
                            </div>
                            <div>
                                <div class="text-[10px] text-gray-500 font-bold uppercase tracking-widest mb-1">Certification Type</div>
                                <div class="text-xl font-bold text-white"><?= e($cert['cert_type']) ?></div>
                            </div>
                        </div>
                        <div class="space-y-6">
                            <div>
                                <div class="text-[10px] text-gray-500 font-bold uppercase tracking-widest mb-1">Issue Date</div>
                                <div class="text-xl font-bold text-white"><?= date('F d, Y', strtotime($cert['created_at'])) ?></div>
                            </div>
                            <div>
                                <div class="text-[10px] text-gray-500 font-bold uppercase tracking-widest mb-1">Associated Invoice</div>
                                <div class="text-xl font-mono text-white">#<?= e($cert['invoice_number'] ?: 'OFF-CERT') ?></div>
                            </div>
                        </div>
                    </div>

                    <div class="mt-12 flex flex-col sm:flex-row items-center gap-4">
                        <a href="/uploads/<?= e($cert['file_path']) ?>" target="_blank" class="btn-primary px-8 py-4 font-black uppercase tracking-widest text-xs w-full sm:w-auto">Download Technical Records</a>
                        <span class="text-xs text-gray-500 italic">SHA-256 Digital Verification Hash: <?= md5($cert['id'] . APP_SECRET) ?></span>
                    </div>
                </div>
            </div>
        <?php elseif (!$search): ?>
            <!-- Instructions -->
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6 fade-in">
                <div class="card-dark p-6 text-center">
                    <div class="text-3xl mb-4">🔍</div>
                    <h4 class="text-sm font-bold text-white uppercase mb-2">Locate ID</h4>
                    <p class="text-[10px] text-gray-500 uppercase tracking-widest font-bold">Find the serial or certificate ID on the bottom right of your document.</p>
                </div>
                <div class="card-dark p-6 text-center border-orange-500/20">
                    <div class="text-3xl mb-4">⚡</div>
                    <h4 class="text-sm font-bold text-white uppercase mb-2">Instant Validation</h4>
                    <p class="text-[10px] text-gray-500 uppercase tracking-widest font-bold">Our database performs a real-time integrity check against original vault records.</p>
                </div>
                <div class="card-dark p-6 text-center">
                    <div class="text-3xl mb-4">📄</div>
                    <h4 class="text-sm font-bold text-white uppercase mb-2">Secure Access</h4>
                    <p class="text-[10px] text-gray-500 uppercase tracking-widest font-bold">Download official digital copies of Mill Test Reports (MTR) and CoCs.</p>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
