<?php
include 'db.php';
if (session_status() === PHP_SESSION_NONE) session_start();

// Fetch license and branch information
$result = $conn->query("
    SELECT 
        license_key, 
        branch_name, 
        last_verified_at, 
        license_cache
    FROM branch_info 
    LIMIT 1
");

$licenseData = $result->fetch_assoc();
$license_key = $licenseData['license_key'] ?? '';
$branch_name = $licenseData['branch_name'] ?? 'Not Set';
$last_verified_at = $licenseData['last_verified_at'] ?? 'Never';
$license_cache = $licenseData['license_cache'] ?? '';

// Parse license cache for additional info
$cached_data = json_decode($license_cache, true);
$license_status = $cached_data['success'] ?? false;
$license_message = $cached_data['message'] ?? 'No verification data available';

// Determine license mode based on available data
$license_mode = 'Unknown';
if ($cached_data) {
    // Check for explicit license type
    if (isset($cached_data['license_type'])) {
        $license_mode = $cached_data['license_type'];
    } 
    // Check for support end date to determine if it's a trial/demo
    elseif (isset($cached_data['support_end_date'])) {
        $support_end = strtotime($cached_data['support_end_date']);
        $now = time();
        $days_diff = ($support_end - $now) / (60 * 60 * 24);
        
        if ($days_diff <= 30) {
            $license_mode = 'trial';
        } else {
            $license_mode = 'permanent';
        }
    }
    // Check branch name for demo indicators
    elseif (isset($cached_data['branch_name'])) {
        $branch_name_lower = strtolower($cached_data['branch_name']);
        if (strpos($branch_name_lower, 'demo') !== false || strpos($branch_name_lower, 'test') !== false) {
            $license_mode = 'demo';
        } else {
            $license_mode = 'permanent';
        }
    }
}

// Determine verification status
$verification_status = 'Unknown';
$verification_class = 'text-muted';

if ($last_verified_at && $last_verified_at !== 'Never') {
    $days_since_verification = (new DateTime())->diff(new DateTime($last_verified_at))->days;
    if ($days_since_verification <= 7) {
        $verification_status = 'Verified (Online)';
        $verification_class = 'text-success';
    } elseif ($days_since_verification <= 30) {
        $verification_status = 'Verified (Cached)';
        $verification_class = 'text-warning';
    } else {
        $verification_status = 'Outdated';
        $verification_class = 'text-danger';
    }
} else {
    $verification_status = 'Never Verified';
    $verification_class = 'text-danger';
}
?>

<!-- License Info Modal -->
<div class="modal fade" id="licenseInfoModal" tabindex="-1" aria-labelledby="licenseInfoModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="licenseInfoModalLabel">
                    <i class="fas fa-key me-2"></i>License Information
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body p-0">
                <!-- License Key -->
                <div class="info-row border-bottom">
                    <div class="p-3">
                        <div class="info-label">License Key</div>
                        <div class="info-value">
                            <?php if ($license_key): ?>
                                <code class="text-primary"><?php echo htmlspecialchars($license_key); ?></code>
                            <?php else: ?>
                                <span class="text-danger">No License Key Found</span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- Verification Status -->
                <div class="info-row border-bottom">
                    <div class="p-3">
                        <div class="info-label">Verification Status</div>
                        <div class="info-value">
                            <span class="status-badge <?php echo str_replace('text-', 'status-', $verification_class); ?>">
                                <?php echo $verification_status; ?>
                            </span>
                            <?php if ($last_verified_at && $last_verified_at !== 'Never'): ?>
                                <small class="text-muted ms-2">Last verified: <?php echo date('M d, Y H:i', strtotime($last_verified_at)); ?></small>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- License Mode -->
                <div class="info-row border-bottom">
                    <div class="p-3">
                        <div class="info-label">License Mode</div>
                        <div class="info-value">
                            <?php 
                            $mode_class = 'status-muted';
                            $mode_text = 'Unknown';
                            
                            if (strtolower($license_mode) === 'demo' || strtolower($license_mode) === 'trial') {
                                $mode_class = 'status-warning';
                                $mode_text = 'Demo/Trial';
                            } elseif (strtolower($license_mode) === 'permanent' || strtolower($license_mode) === 'full') {
                                $mode_class = 'status-verified';
                                $mode_text = 'Permanent';
                            } else {
                                $mode_text = ucfirst($license_mode);
                            }
                            ?>
                            <span class="status-badge <?php echo $mode_class; ?>">
                                <?php echo $mode_text; ?>
                            </span>
                        </div>
                    </div>
                </div>

                <!-- Branch Information -->
                <div class="info-row">
                    <div class="p-3">
                        <div class="info-label">Branch Name</div>
                        <div class="info-value"><?php echo htmlspecialchars($branch_name); ?></div>
                    </div>
                </div>

                <!-- License Message -->
                <?php if ($license_message && $license_message !== 'No verification data available'): ?>
                <div class="info-row border-top">
                    <div class="p-3">
                        <div class="info-label">License Message</div>
                        <div class="info-value"><?php echo htmlspecialchars($license_message); ?></div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
            <div class="modal-footer bg-light">
                <div class="d-flex justify-content-between align-items-center w-100">
                    <button type="button" class="btn btn-outline-danger btn-sm" onclick="deleteLicenseData()">
                        <i class="fas fa-trash-alt me-1"></i>Clear License Data
                    </button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        <i class="fas fa-times me-2"></i>Close
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.info-label {
    font-weight: 600;
    color: #5a5c69;
    margin-bottom: 5px;
    font-size: 0.9rem;
}
.info-value {
    color: #858796;
    word-break: break-all;
    font-size: 0.95rem;
}
.status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
}
.status-verified {
    background-color: #d4edda;
    color: #155724;
}
.status-warning {
    background-color: #fff3cd;
    color: #856404;
}
.status-danger {
    background-color: #f8d7da;
    color: #721c24;
}
.status-muted {
    background-color: #e2e3e5;
    color: #6c757d;
}
</style>

