<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/templates/social/pinterest-connect.php
// Description: Pinterest connection page - OAuth flow entry point - NUCLEAR+++
// Last Edited: 2026-05-27
// ==================================================

namespace AutoMarketerAI;

defined('ABSPATH') || exit;

$connect_url = PinterestAuth::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;">Pinterest is not configured.</strong> '
        . 'The <code>AMAI_PIN_CLIENT_ID</code> and <code>AMAI_PIN_CLIENT_SECRET</code> constants are missing from <code>wp-config.php</code>. '
        . 'Add them to enable Pinterest.</div></div></div>';
    return;
}

$auth_error = get_transient('amai_pinterest_auth_error');
if ($auth_error !== false) {
    delete_transient('amai_pinterest_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:#E60023;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.477 2 2 6.477 2 12c0 4.236 2.636 7.855 6.356 9.312-.088-.791-.167-2.005.035-2.868.181-.78 1.172-4.97 1.172-4.97s-.299-.598-.299-1.482c0-1.388.806-2.428 1.808-2.428.853 0 1.267.641 1.267 1.408 0 .858-.546 2.141-.828 3.33-.236.995.499 1.806 1.476 1.806 1.771 0 3.132-1.867 3.132-4.563 0-2.386-1.715-4.054-4.164-4.054-2.837 0-4.5 2.128-4.5 4.327 0 .857.33 1.775.741 2.276a.3.3 0 0 1 .069.284c-.076.313-.244.995-.277 1.134-.044.183-.146.221-.337.133-1.249-.581-2.03-2.407-2.03-3.874 0-3.154 2.292-6.052 6.608-6.052 3.469 0 6.165 2.473 6.165 5.776 0 3.447-2.173 6.22-5.19 6.22-1.013 0-1.966-.527-2.292-1.148l-.623 2.378c-.226.869-.835 1.958-1.244 2.621.937.29 1.931.446 2.962.446 5.523 0 10-4.477 10-10S17.523 2 12 2z" fill="white"/>
                </svg>
            </div>
            <h2 class="amai-fb-connect-heading">Connect your Pinterest account</h2>
            <p class="amai-fb-connect-subheading">
                Link your Pinterest account and AutoMarketer AI will create and publish Pins for your business 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 = [
                'pinterest_auth_denied'          => 'You cancelled the Pinterest authorization. No worries - ready when you are.',
                'pinterest_missing_params'       => 'Something went wrong during authorization. Please try again.',
                'pinterest_invalid_state'        => 'Security check failed. Please try connecting again.',
                'pinterest_missing_verifier'     => 'Security check failed. Please try connecting again.',
                'pinterest_token_exchange_failed' => 'Could not complete the connection. Please try again.',
                'pinterest_empty_token'          => 'Authorization failed. Please try again.',
                'pinterest_user_fetch_failed'    => 'Connected but could not fetch your profile. Please try again.',
                'pinterest_empty_user_id'        => '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 Pinterest Business account yet?</div>
                <p class="amai-fb-help-body">
                    A Pinterest Business account is free and gives you access to analytics and publishing tools.
                    It takes about 2 minutes to set up.
                </p>
                <a href="https://business.pinterest.com/" target="_blank" rel="noopener noreferrer" class="amai-fb-help-link">
                    Create a Pinterest Business account &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 Pinterest</div>
                    <div class="amai-fb-step-desc">Click the button below and sign in with your Pinterest account. Grant AutoMarketer AI permission to create Pins on your behalf.</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">We connect your account</div>
                    <div class="amai-fb-step-desc">Once authorized, your Pinterest account links automatically. No extra setup needed.</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">Start generating Pins</div>
                    <div class="amai-fb-step-desc">AutoMarketer AI creates image Pins with compelling descriptions tailored to your business and schedules them throughout the week.</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:#E60023;">
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M12 2C6.477 2 2 6.477 2 12c0 4.236 2.636 7.855 6.356 9.312-.088-.791-.167-2.005.035-2.868.181-.78 1.172-4.97 1.172-4.97s-.299-.598-.299-1.482c0-1.388.806-2.428 1.808-2.428.853 0 1.267.641 1.267 1.408 0 .858-.546 2.141-.828 3.33-.236.995.499 1.806 1.476 1.806 1.771 0 3.132-1.867 3.132-4.563 0-2.386-1.715-4.054-4.164-4.054-2.837 0-4.5 2.128-4.5 4.327 0 .857.33 1.775.741 2.276a.3.3 0 0 1 .069.284c-.076.313-.244.995-.277 1.134-.044.183-.146.221-.337.133-1.249-.581-2.03-2.407-2.03-3.874 0-3.154 2.292-6.052 6.608-6.052 3.469 0 6.165 2.473 6.165 5.776 0 3.447-2.173 6.22-5.19 6.22-1.013 0-1.966-.527-2.292-1.148l-.623 2.378c-.226.869-.835 1.958-1.244 2.621.937.29 1.931.446 2.962.446 5.523 0 10-4.477 10-10S17.523 2 12 2z" fill="white"/>
                </svg>
                Connect with Pinterest
            </a>
            <p class="amai-fb-connect-fine">We never post without your knowledge. You review every Pin before it goes live.</p>
        </div>

        <div class="amai-fb-connect-help" style="margin-top:32px;padding:20px 24px;background:#FDF6F3;border:1.5px solid #EDD5C8;border-radius:12px;">
            <div class="amai-fb-help-heading" style="margin-bottom:8px;">Note on Pinterest publishing</div>
            <p class="amai-fb-help-body">
                While our Pinterest app is under review, Pins will be created as drafts in your account.
                Once approved by Pinterest, posts will publish publicly on your scheduled dates.
            </p>
        </div>

    </div>
</div>