<?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-connect.php
// Description: Yelp connection page - OAuth flow entry point - NUCLEAR+++
// Last Edited: 2026-05-27
// ==================================================

namespace AutoMarketerAI;

defined('ABSPATH') || exit;

$connect_url = YelpAuth::getAuthUrl();

if ($connect_url === '') {
    echo '<div class="amai-fb-connect-area"><div class="amai-fb-connect-shell">'
        . '<div class="amai-notice amai-notice--warn" style="margin:32px auto;max-width:640px;padding:20px 24px;background:#FEF3C7;border:1.5px solid #FCD34D;border-radius:8px;font-size:14px;color:#78716C;">'
        . '<strong style="color:#57534E;">Yelp is not configured.</strong> '
        . 'The <code>AMAI_YELP_CLIENT_ID</code> and <code>AMAI_YELP_CLIENT_SECRET</code> constants are missing from <code>wp-config.php</code>. '
        . 'Add them to enable Yelp.</div></div></div>';
    return;
}

$auth_error = get_transient('amai_yelp_auth_error');
if ($auth_error !== false) {
    delete_transient('amai_yelp_auth_error');
}
?>
<div class="amai-fb-connect-area">
    <div class="amai-fb-connect-shell">

        <div class="amai-fb-connect-hero">
            <div class="amai-fb-connect-icon" style="background:#FF1A1A1a;border-radius:14px;width:52px;height:52px;display:flex;align-items:center;justify-content:center;">
                <svg width="28" height="28" 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="#FF1A1A"/>
                </svg>
            </div>
            <h2 class="amai-fb-connect-heading">Connect your Yelp Business</h2>
            <p class="amai-fb-connect-subheading">
                Link your Yelp Business account so AutoMarketer AI can monitor your reviews and help you respond automatically.
            </p>
        </div>

        <?php if ($auth_error !== false) : ?>
        <div class="amai-fb-connect-error">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <circle cx="8" cy="8" r="7" stroke="currentColor" stroke-width="1.3" fill="none"/>
                <path d="M8 4.5v4" stroke="currentColor" stroke-width="1.3" stroke-linecap="round"/>
                <circle cx="8" cy="11" r="0.6" fill="currentColor"/>
            </svg>
            <?php
            $error_messages = [
                'yelp_auth_denied'          => 'You cancelled the Yelp authorization. No worries - ready when you are.',
                'yelp_missing_params'       => 'Something went wrong during authorization. Please try again.',
                'yelp_invalid_state'        => 'Security check failed. Please try connecting again.',
                'yelp_token_exchange_failed' => 'Could not complete the connection. Please try again.',
                'yelp_empty_token'          => 'Authorization failed. Please try again.',
            ];
            echo '<span>' . esc_html($error_messages[(string) $auth_error] ?? 'Something went wrong. Please try again.') . '</span>';
            ?>
        </div>
        <?php endif; ?>

        <div class="amai-fb-connect-newbie-zone">
            <div class="amai-fb-help-block">
                <div class="amai-fb-help-heading">Don't have a Yelp Business account yet?</div>
                <p class="amai-fb-help-body">
                    Claiming your Yelp Business page is free. It lets you respond to reviews, update your info, and reach customers searching for businesses like yours.
                </p>
                <a href="https://biz.yelp.com/claim" target="_blank" rel="noopener noreferrer" class="amai-fb-help-link">
                    Claim your Yelp Business page &rarr;
                </a>
            </div>
        </div>

        <div class="amai-fb-connect-steps">
            <div class="amai-fb-connect-step">
                <div class="amai-fb-step-num">1</div>
                <div class="amai-fb-step-body">
                    <div class="amai-fb-step-title">Authorize with Yelp</div>
                    <div class="amai-fb-step-desc">Click the button below and sign in with your Yelp Business account. Grant AutoMarketer AI permission to access your business data.</div>
                </div>
            </div>
            <div class="amai-fb-connect-step">
                <div class="amai-fb-step-num">2</div>
                <div class="amai-fb-step-body">
                    <div class="amai-fb-step-title">Select your business</div>
                    <div class="amai-fb-step-desc">If you manage multiple locations, choose which one to connect. You can add more later.</div>
                </div>
            </div>
            <div class="amai-fb-connect-step">
                <div class="amai-fb-step-num">3</div>
                <div class="amai-fb-step-body">
                    <div class="amai-fb-step-title">Monitor and respond</div>
                    <div class="amai-fb-step-desc">AutoMarketer AI monitors your reviews and helps you craft professional responses that protect and build your reputation.</div>
                </div>
            </div>
        </div>

        <div class="amai-fb-connect-action">
            <a href="<?php echo esc_url($connect_url); ?>" class="amai-fb-connect-btn" style="background:#FF1A1A;">
                <svg width="18" height="18" 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>
                Connect with Yelp
            </a>
            <p class="amai-fb-connect-fine">We never post without your approval. You review every response before it goes live.</p>
        </div>

    </div>
</div>