<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php
global $wpdb;
// Direct database query used below – Safely gets data from the database using $wpdb->prepare()
$copuloo_keys = $wpdb->get_results($wpdb->prepare("SELECT license_key FROM {$wpdb->prefix}copuloo_api_keys WHERE id = %s LIMIT 1", 'ACWJB59')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
?>

<div class="copuloo-admin-wrap">
    <?php
    // Display notice based on license key validation
    if (isset($copuloo_keys[0]->license_key) && strlen($copuloo_keys[0]->license_key) == 27 && stripos($copuloo_keys[0]->license_key, 'd') !== false) {
        echo '<div class="copuloo-notice copuloo-notice-success"><p><strong>License Key is Active!</strong> Your license code is active!</p><button class="copuloo-notice-dismiss">Dismiss</button></div>';
    } else {
        echo '<div class="copuloo-notice copuloo-notice-error"><p><strong>Incorrect License Code!</strong> Incorrect license code was applied.</p><button class="copuloo-notice-dismiss">Dismiss</button></div>';
    }
    ?>

    <div class="copuloo-card">
        <h2 class="copuloo-title">Copuloo License Key</h2>
        <p>Please enter your Copuloo license key to activate and use the plugin.</p>
        <p>You can purchase a license key at <a href="https://copuloo.com/pricing/" target="_blank" class="copuloo-link">Copuloo.com</a></p>
    </div>

    <div class="copuloo-card">
        <h2 class="copuloo-title">Enter License Key</h2>
        <form action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="post">
            <?php wp_nonce_field('Copuloo', 'user_nonce'); ?>
            <input type="hidden" name="action" value="copuloo_update_license_key">
            <div class="copuloo-form-group">
                <label for="license_key" class="copuloo-label">License Key</label>
                <input required type="text" name="license_key" id="license_key" placeholder="Enter your license key" 
                       value="<?php if (isset($copuloo_keys[0]->license_key)) { echo esc_html($copuloo_keys[0]->license_key); } ?>" 
                       class="copuloo-input">
            </div>
            <div class="copuloo-form-group">
                <button type="submit" class="copuloo-button copuloo-button-primary">Save License</button>
            </div>
        </form>
    </div>
</div>