<?php
if (!defined('WPINC')) {
    die;
}

$wallet_snapshot = array();
$project_id = intval(get_option('aysa_project_id', 0));
$setup_completed = get_option('aysa_setup_completed', false) ? true : false;
$auth_status = array();

if (class_exists('Aysa_Auth_Manager')) {
    $auth_status = Aysa_Auth_Manager::get_instance()->is_authenticated();
}

if (class_exists('Aysa_Wallet_Manager')) {
    $wallet = Aysa_Wallet_Manager::get_instance();
    if (!$project_id && method_exists($wallet, 'get_project_id')) {
        $project_id = intval($wallet->get_project_id());
    }
    if ($project_id > 0 && method_exists($wallet, 'get_wallet_snapshot')) {
        $wallet_snapshot = $wallet->get_wallet_snapshot($project_id, array('allow_remote' => true));
    }
}

$plan_label = trim((string) ($wallet_snapshot['plan_label'] ?? ''));
if ($plan_label === '') {
    $plan_label = trim((string) ($wallet_snapshot['plan_code'] ?? 'Not assigned'));
}

$current_credits = intval($wallet_snapshot['balance_cents'] ?? 0);
$spendable_credits = intval($wallet_snapshot['spendable_cents'] ?? 0);
$reserved_credits = intval($wallet_snapshot['reserved_links_cents'] ?? 0);
$recommended_keywords = intval($wallet_snapshot['recommended_keywords'] ?? 0);
$included_links = intval($wallet_snapshot['included_links'] ?? 0);
$is_authenticated = !empty($auth_status['authenticated']);
?>

<div class="wrap aysa-dashboard-overview">
    <h1>AYSA Overview</h1>
    <p class="description">Your plugin is connected. Use this page to confirm the current project, available credits and the next setup steps.</p>

    <div class="aysa-dashboard-grid">
        <div class="aysa-dashboard-card">
            <h2>Connection Status</h2>
            <ul>
                <li><strong>Activation:</strong> <?php echo $is_authenticated ? 'Connected' : 'Not connected'; ?></li>
                <li><strong>Setup:</strong> <?php echo $setup_completed ? 'Completed' : 'In progress'; ?></li>
                <li><strong>Project ID:</strong> <?php echo $project_id > 0 ? esc_html((string) $project_id) : 'Missing'; ?></li>
                <li><strong>Plan:</strong> <?php echo esc_html($plan_label); ?></li>
            </ul>
        </div>

        <div class="aysa-dashboard-card">
            <h2>Credit Snapshot</h2>
            <ul>
                <li><strong>Current credits:</strong> <?php echo esc_html((string) $current_credits); ?></li>
                <li><strong>Spendable now:</strong> <?php echo esc_html((string) $spendable_credits); ?></li>
                <li><strong>Reserved for links:</strong> <?php echo esc_html((string) $reserved_credits); ?></li>
                <li><strong>Included links:</strong> <?php echo esc_html((string) $included_links); ?></li>
                <li><strong>Recommended keywords:</strong> <?php echo esc_html((string) $recommended_keywords); ?></li>
            </ul>
        </div>

        <div class="aysa-dashboard-card">
            <h2>Quick Actions</h2>
            <div class="aysa-dashboard-actions">
                <a class="button button-primary" href="<?php echo esc_url(admin_url('admin.php?page=aysa-setup-seo-profile')); ?>">Continue setup</a>
                <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=aysa-research&tab=keyword-discovery')); ?>">Open Keyword Discovery</a>
                <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=aysa-settings')); ?>">Open Settings</a>
                <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=aysa-subscription')); ?>">Open Subscription</a>
            </div>
        </div>
    </div>
</div>

<style>
.aysa-dashboard-overview {
    max-width: 1200px;
}

.aysa-dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
    margin-top: 20px;
}

.aysa-dashboard-card {
    background: #fff;
    border: 1px solid #dcdcde;
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.aysa-dashboard-card h2 {
    margin-top: 0;
    margin-bottom: 16px;
}

.aysa-dashboard-card ul {
    margin: 0;
    padding-left: 18px;
}

.aysa-dashboard-card li {
    margin-bottom: 10px;
}

.aysa-dashboard-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}
</style>
