<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /templates/social/facebook-picker.php
// Description: Facebook Business Page picker - NUCLEAR+++
// Last Edited: 2026-03-27
// ==================================================

namespace AutoMarketerAI;

// No direct access
defined('ABSPATH') || exit;

// $pending_pages passed from SocialPage::renderChannel()
$pages = $pending_pages['pages'] ?? [];

// Sign the full pending payload so we can pass it through the form
// without relying on DB persistence between requests.
$pending_payload = '';
$pending_sig     = '';
if (!empty($pending_pages)) {
    $pending_payload = base64_encode((string) json_encode($pending_pages, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
    $pending_sig     = FacebookAuth::signPending($pending_payload);
}
?>
<div class="amai-fb-picker-area">

    <div class="amai-fb-picker-shell">

        <div class="amai-fb-picker-header">
            <h2 class="amai-fb-picker-heading">Which page should we post to?</h2>
            <p class="amai-fb-picker-sub">
                These are the Business Pages you manage on Facebook.
                Pick the one you want AutoMarketer AI to handle.
            </p>
        </div>

        <?php if (empty($pages)) : ?>
        <div class="amai-fb-picker-body">
            <div class="amai-fb-notice amai-fb-notice-warn">
                <div class="amai-fb-notice-body">
                    We couldn't find any Business Pages on your account. Make sure you're an admin on a Facebook Business Page, then try connecting again.
                    <a href="https://www.facebook.com/pages/creation/" target="_blank" rel="noopener noreferrer" class="amai-fb-notice-link">Create a Business Page →</a>
                </div>
            </div>
        </div>
        <div class="amai-fb-picker-footer">
            <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-social&channel=facebook')); ?>" class="amai-fb-back-link">← Start over</a>
        </div>
        <?php else : ?>

        <form method="post"
              action="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-social&channel=facebook')); ?>"
              id="amai-fb-picker-form">
            <?php wp_nonce_field('amai_fb_select_page'); ?>
            <input type="hidden" name="amai_fb_action"   value="select_page"/>
            <input type="hidden" name="page_id"          id="amai-selected-page-id" value=""/>
            <input type="hidden" name="pending_payload"  value="<?php echo esc_attr($pending_payload); ?>"/>
            <input type="hidden" name="pending_sig"      value="<?php echo esc_attr($pending_sig); ?>"/>

            <div class="amai-fb-pages-list">
                <?php foreach ($pages as $page) :
                    $page_id      = esc_attr((string) ($page['id'] ?? ''));
                    $page_name    = esc_html((string) ($page['name'] ?? ''));
                    $page_cat     = esc_html((string) ($page['category'] ?? ''));
                    $page_pic     = esc_url((string) ($page['picture_url'] ?? ''));
                    $page_follows  = (int) ($page['followers'] ?? 0);
                    $follows_fmt   = number_format($page_follows);
                    $follows_label = $page_follows === 1 ? 'follower' : 'followers';
                    $initials     = strtoupper(substr((string) ($page['name'] ?? 'P'), 0, 2));
                ?>
                <div class="amai-fb-page-card"
                     data-page-id="<?php echo $page_id; ?>"
                     role="button"
                     tabindex="0">
                    <div class="amai-fb-page-avatar">
                        <?php if ($page_pic !== '') : ?>
                            <img src="<?php echo $page_pic; ?>"
                                 alt="<?php echo $page_name; ?>"
                                 width="48"
                                 height="48"/>
                        <?php else : ?>
                            <div class="amai-fb-page-initials"><?php echo esc_html($initials); ?></div>
                        <?php endif; ?>
                    </div>
                    <div class="amai-fb-page-info">
                        <div class="amai-fb-page-name"><?php echo $page_name; ?></div>
                        <div class="amai-fb-page-meta">
                            <?php if ($page_cat !== '') : ?>
                                <span><?php echo $page_cat; ?></span>
                                <span class="amai-fb-page-meta-dot">·</span>
                            <?php endif; ?>
                            <span><?php echo esc_html($follows_fmt . ' ' . $follows_label); ?></span>
                        </div>
                    </div>
                    <div class="amai-fb-page-check">
                        <div class="amai-fb-page-radio"></div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>

            <div class="amai-fb-picker-footer">
                <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-social&channel=facebook')); ?>"
                   class="amai-fb-back-link">← Use a different account</a>
                <button type="submit"
                        class="amai-fb-select-btn"
                        id="amai-fb-select-btn"
                        disabled>
                    Connect this page →
                </button>
            </div>
        </form>

        <?php endif; ?>

    </div>
</div>