<?php
// Key validation logic
$message = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $keyValue = $_POST['key_value'] ?? '';
        $deviceId = $_POST['device_id'] ?? '';

            $keysFile = './keys.json';
                $keys = file_exists($keysFile) ? json_decode(file_get_contents($keysFile), true) : [];

                    // Search for the key in the keys.json file
                        $foundKey = null;
                            foreach ($keys as &$key) {
                                    if ($key['key'] === $keyValue) {
                                                $foundKey = &$key;
                                                            break;
                                                                    }
                                                                        }

                                                                            if ($foundKey) {
                                                                                    $currentDate = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
                                                                                            $expiryDate = new DateTime($foundKey['expires'], new DateTimeZone('Asia/Kolkata'));

                                                                                                    if ($foundKey['status'] !== 'active') {
                                                                                                                $message = ['type' => 'error', 'text' => 'Key is inactive. Please contact @TANMAYPAUL21.'];
                                                                                                                        } elseif ($currentDate > $expiryDate) {
                                                                                                                                    $message = ['type' => 'error', 'text' => 'Key expired. BUY FROM @TANMAYPAUL21.'];
                                                                                                                                            } elseif (!empty($foundKey['device_id']) && $foundKey['device_id'] !== $deviceId) {
                                                                                                                                                        $message = ['type' => 'error', 'text' => 'Key is already bound to another device.'];
                                                                                                                                                                } else {
                                                                                                                                                                            // Valid key: Update the device_id if not already bound
                                                                                                                                                                                        if (empty($foundKey['device_id'])) {
                                                                                                                                                                                                        $foundKey['device_id'] = $deviceId;
                                                                                                                                                                                                                        file_put_contents($keysFile, json_encode($keys, JSON_PRETTY_PRINT));
                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                // Redirect to tanmay.html after successful login
                                                                                                                                                                                                                                                            header('Location: tanmay.html');
                                                                                                                                                                                                                                                                        exit;
                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                    } else {
                                                                                                                                                                                                                                                                                            $message = ['type' => 'error', 'text' => 'Invalid key. Please try again.'];
                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                ?>

                                                                                                                                                                                                                                                                                                <!DOCTYPE html>
                                                                                                                                                                                                                                                                                                <html lang="en">
                                                                                                                                                                                                                                                                                                <head>
                                                                                                                                                                                                                                                                                                    <meta charset="UTF-8">
                                                                                                                                                                                                                                                                                                        <meta name="viewport" content="width=device-width, initial-scale=1.0">
                                                                                                                                                                                                                                                                                                            <script src="https://cdn.tailwindcss.com"></script>
                                                                                                                                                                                                                                                                                                                <script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
                                                                                                                                                                                                                                                                                                                    <link href="https://fonts.googleapis.com/css2?family=El+Messiri:wght@400;700&display=swap" rel="stylesheet">
                                                                                                                                                                                                                                                                                                                        <title>Key Validation</title>
                                                                                                                                                                                                                                                                                                                            <style>
                                                                                                                                                                                                                                                                                                                                    body {
                                                                                                                                                                                                                                                                                                                                                background: url('bg.png') no-repeat center center fixed;
                                                                                                                                                                                                                                                                                                                                                            background-size: cover;
                                                                                                                                                                                                                                                                                                                                                                        font-family: 'El Messiri', sans-serif;
                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                    </style>
                                                                                                                                                                                                                                                                                                                                                                                    </head>
                                                                                                                                                                                                                                                                                                                                                                                    <body class="h-screen flex items-center justify-center">
                                                                                                                                                                                                                                                                                                                                                                                        <div class="bg-white/10 backdrop-blur-md shadow-lg rounded-lg p-8 w-full max-w-sm border border-blue-300/50">
                                                                                                                                                                                                                                                                                                                                                                                                <div class="text-center mb-6">
                                                                                                                                                                                                                                                                                                                                                                                                            <img src="royal.jpg" alt="Logo" class="mx-auto w-24 h-24 rounded-full border-4 border-green-500" />
                                                                                                                                                                                                                                                                                                                                                                                                                        <h1 class="text-2xl font-bold mt-2 text-white">TANMAY SERVER</h1> 
                                                                                                                                                                                                                                                                                                                                                                                                                                </div>

                                                                                                                                                                                                                                                                                                                                                                                                                                        <?php if ($message): ?>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <div class="mb-4 p-3 rounded <?= $message['type'] === 'success' ? 'bg-green-500' : 'bg-red-500'; ?> text-white text-center">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <?= $message['text']; ?>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                </div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <?php endif; ?>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <form method="POST" action="">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <div class="mb-4 relative">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <i class="fas fa-key absolute left-3 top-1/2 transform -translate-y-1/2 text-green-500"></i>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <input type="text" name="key_value" placeholder="Enter Key" required 
                                                                                                                      