<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/templates/social/yelp-dashboard.php
// Description: Yelp channel dashboard - connected business view - NUCLEAR+++
// Last Edited: 2026-05-27
// ==================================================

namespace AutoMarketerAI;

defined('ABSPATH') || exit;

$connection = YelpAuth::getConnection();

if ($connection === null) {
    include AMAI_PLUGIN_DIR . 'templates/social/yelp-connect.php';
    return;
}

$business_name  = esc_html((string) ($connection['business_name'] ?? ''));
$business_url   = esc_url((string) ($connection['business_url'] ?? ''));
$avatar_url     = esc_url((string) ($connection['avatar_url'] ?? ''));
$review_count   = (int) ($connection['review_count'] ?? 0);
$rating         = (float) ($connection['rating'] ?? 0.0);
$connected_at   = (string) ($connection['connected_at'] ?? '');

$connected_date = '';
if ($connected_at !== '') {
    $ts = strtotime($connected_at);
    if ($ts !== false) {
        $connected_date = gmdate('M j, Y', $ts);
    }
}

$disconnect_nonce = wp_create_nonce('amai_yelp_disconnect');
$just_connected   = isset($_GET['connected']) && $_GET['connected'] === '1';
$disconnected     = isset($_GET['disconnected']) && $_GET['disconnected'] === '1';
?>

<div class="amai-channel-dashboard">

    <?php if ($just_connected) : ?>
    <div class="amai-dash-notice amai-dash-notice--success amai-temp-notice">
        Yelp connected successfully. You're all set.
    </div>
    <?php endif; ?>

    <?php if ($disconnected) : ?>
    <div class="amai-dash-notice amai-dash-notice--info amai-temp-notice">
        Yelp disconnected.
    </div>
    <?php endif; ?>

    <div class="amai-dash-tabs">
        <button type="button" class="amai-dash-tab amai-dash-tab--active" data-tab="dashboard">Dashboard</button>
    </div>

    <div class="amai-dash-tab-panel" data-panel="dashboard">

        <div class="amai-dash-account-card">
            <?php if ($avatar_url !== '') : ?>
            <img src="<?php echo $avatar_url; ?>" alt="<?php echo $business_name; ?>" class="amai-dash-account-avatar">
            <?php else : ?>
            <div class="amai-dash-account-avatar amai-dash-account-avatar--placeholder" style="background:#FF1A1A;">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 14.5v-9l7 4.5-7 4.5z" fill="white"/>
                </svg>
            </div>
            <?php endif; ?>

            <div class="amai-dash-account-meta">
                <div class="amai-dash-account-name"><?php echo $business_name; ?></div>
                <?php if ($rating > 0) : ?>
                <div class="amai-dash-account-handle" style="color:#D97757;">
                    &#9733; <?php echo number_format($rating, 1); ?> (<?php echo number_format($review_count); ?> reviews)
                </div>
                <?php endif; ?>
                <div class="amai-dash-account-stats">
                    <?php if ($business_url !== '') : ?>
                    <a href="<?php echo $business_url; ?>" target="_blank" rel="noopener noreferrer" class="amai-dash-account-stat" style="color:#D97757;">View on Yelp &rarr;</a>
                    <?php endif; ?>
                    <?php if ($connected_date !== '') : ?>
                    <span class="amai-dash-account-stat-sep">·</span>
                    <span class="amai-dash-account-stat">Connected <?php echo esc_html($connected_date); ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="amai-dash-account-actions">
                <span class="amai-dash-status-dot amai-dash-status-dot--green"></span>
                <span class="amai-dash-status-label">Connected</span>
                <form method="post" style="display:inline;" onsubmit="return confirm('Disconnect Yelp?');">
                    <?php wp_nonce_field('amai_yelp_disconnect'); ?>
                    <button type="submit" name="amai_yelp_disconnect" value="1" class="amai-dash-disconnect-btn">
                        Disconnect
                    </button>
                </form>
            </div>
        </div>

        <div class="amai-dash-coming-soon-block" style="margin-top:32px;padding:40px 32px;background:#FDF6F3;border:1.5px solid #EDD5C8;border-radius:16px;text-align:center;">
            <div style="font-size:36px;margin-bottom:16px;">&#11088;</div>
            <h3 style="font-family:'Nunito',sans-serif;font-size:18px;font-weight:800;color:#1C1917;margin:0 0 8px;">Review monitoring coming soon</h3>
            <p style="font-size:14px;color:#78716C;max-width:420px;margin:0 auto;">
                Your Yelp business is connected. Automated review monitoring, sentiment analysis, and AI-generated response suggestions will be available in the next update.
            </p>
        </div>

    </div>

</div>

<script>
(function () {
    var notices = document.querySelectorAll('.amai-temp-notice');
    notices.forEach(function (notice) {
        setTimeout(function () {
            notice.style.transition = 'opacity 0.4s';
            notice.style.opacity = '0';
            setTimeout(function () { notice.style.display = 'none'; }, 400);
        }, 4000);
    });
}());
</script>