<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /templates/social/store-dashboard.php
// Description: Store integration dashboard -- WooCommerce connection status, catalog health, recent product posts - NUCLEAR+++
// Last Edited: 2026-05-15
// ==================================================

namespace AutoMarketerAI;

defined('ABSPATH') || exit;

// Variables passed from StoreChannel::render():
// $is_connected    bool
// $catalog_count   int
// $catalog_health  string: 'ok' | 'empty' | 'never_synced'
// $last_synced     string formatted date or ''
// $recent_posts    array
// $provider_name   string
// $provider_icon   string (SVG markup)

$active_modules = (array) get_option('amai_active_modules', []);
$store_active   = in_array('store-integration', $active_modules, true);
$approval_mode  = (string) get_option('amai_approval_mode', 'review');
?>

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

    <?php if (!$store_active) : ?>
    <div class="amai-fbdash-notice amai-fbdash-notice--warn">
        <div class="amai-fbdash-notice-body">
            The Store Integration module is not active. Enable it on the
            <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-modules')); ?>">Modules page</a>
            to start generating product promotion posts.
        </div>
    </div>
    <?php endif; ?>

    <?php if ($store_active && !$is_connected) : ?>
    <div class="amai-fbdash-notice amai-fbdash-notice--info">
        <div class="amai-fbdash-notice-body">
            No store connected yet. Go to the
            <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-modules')); ?>">Modules page</a>
            and connect your WooCommerce store to start generating product posts.
        </div>
    </div>
    <?php endif; ?>

    <?php if ($store_active && $is_connected && $catalog_health === 'empty') : ?>
    <div class="amai-fbdash-notice amai-fbdash-notice--warn">
        <div class="amai-fbdash-notice-body">
            Your store catalog synced but returned zero products. Check that your WooCommerce store has published products.
        </div>
    </div>
    <?php endif; ?>

    <?php if ($store_active && $is_connected) : ?>

    <div class="amai-store-provider-card">
        <div class="amai-store-provider-icon">
            <?php echo $provider_icon; ?>
        </div>
        <div class="amai-store-provider-info">
            <div class="amai-store-provider-name"><?php echo esc_html($provider_name); ?></div>
            <div class="amai-store-provider-status">
                <?php if ($catalog_health === 'ok') : ?>
                <span class="amai-store-status-dot amai-store-status-dot--ok"></span>
                <?php echo esc_html($catalog_count); ?> product<?php echo $catalog_count !== 1 ? 's' : ''; ?> in catalog
                <?php if ($last_synced !== '') : ?>
                &nbsp;&middot;&nbsp; Last synced <?php echo esc_html($last_synced); ?>
                <?php endif; ?>
                <?php elseif ($catalog_health === 'never_synced') : ?>
                <span class="amai-store-status-dot amai-store-status-dot--pending"></span>
                Waiting for first nightly sync
                <?php else : ?>
                <span class="amai-store-status-dot amai-store-status-dot--warn"></span>
                Catalog sync returned no products
                <?php endif; ?>
            </div>
        </div>
        <form method="post" class="amai-store-sync-form">
            <?php wp_nonce_field('amai_store_sync_now', '_wpnonce'); ?>
            <input type="hidden" name="amai_store_action" value="sync_now">
            <button type="submit" class="amai-store-sync-btn">Sync now</button>
        </form>
    </div>

    <div class="amai-store-how-card">
        <div class="amai-store-how-eyebrow">HOW IT WORKS</div>
        <div class="amai-store-how-grid">
            <div class="amai-store-how-item">
                <div class="amai-store-how-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="#D97757" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/><rect x="7" y="14" width="3" height="3" rx="0.5" fill="#D97757" stroke="none"/><rect x="14" y="14" width="3" height="3" rx="0.5" fill="#D97757" stroke="none"/></svg></div>
                <div class="amai-store-how-label">Mon, Wed, Fri</div>
                <div class="amai-store-how-desc">Product posts go out three times a week on weekdays, separate from your daily content.</div>
            </div>
            <div class="amai-store-how-item">
                <div class="amai-store-how-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="32" height="32" fill="none" stroke="#e53e3e" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="19" y1="5" x2="5" y2="19"/><circle cx="6.5" cy="6.5" r="2.5"/><circle cx="17.5" cy="17.5" r="2.5"/></svg></div>
                <div class="amai-store-how-label">Sales first</div>
                <div class="amai-store-how-desc">Sale items get featured first in a sale - regular - sale pattern to maximize promotion impact.</div>
            </div>
            <div class="amai-store-how-item">
                <div class="amai-store-how-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="#D97757" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" fill="#D97757" stroke="#D97757"/></svg></div>
                <div class="amai-store-how-label">New products jump the queue</div>
                <div class="amai-store-how-desc">Any newly added product gets featured right after current sale items before returning to regular rotation.</div>
            </div>
            <div class="amai-store-how-item">
                <div class="amai-store-how-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="#D97757" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="17 1 21 5 17 9"/><path d="M3 11V9a4 4 0 0 1 4-4h14"/><polyline points="7 23 3 19 7 15"/><path d="M21 13v2a4 4 0 0 1-4 4H3"/></svg></div>
                <div class="amai-store-how-label">Full rotation</div>
                <div class="amai-store-how-desc">Every product gets its turn, newest to oldest. When the list ends it starts over automatically.</div>
            </div>
        </div>
        <div class="amai-store-how-approval">
            Posts are added as
            <strong><?php echo $approval_mode === 'autopilot' ? 'approved (Autopilot on)' : 'drafts for your review'; ?></strong>.
            Change this in your
            <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-social&channel=facebook')); ?>">approval settings</a>.
        </div>
    </div>

    <?php if (!empty($recent_posts)) : ?>
    <div class="amai-store-recent-section">
        <div class="amai-store-section-title">Recent product posts</div>
        <div class="amai-store-recent-list">
            <?php foreach ($recent_posts as $post) :
                $channel      = (string) ($post['channel']      ?? '');
                $content      = (string) ($post['post_content'] ?? '');
                $status       = (string) ($post['status']       ?? '');
                $date         = (string) ($post['scheduled_date'] ?? '');
                $status_class = $status === 'posted' ? 'posted' : ($status === 'approved' ? 'approved' : 'draft');
            ?>
            <div class="amai-store-recent-row">
                <div class="amai-store-recent-channel amai-store-channel-<?php echo esc_attr($channel); ?>">
                    <?php echo esc_html(ucfirst($channel)); ?>
                </div>
                <div class="amai-store-recent-content">
                    <?php echo esc_html(mb_substr($content, 0, 140) . (mb_strlen($content) > 140 ? '...' : '')); ?>
                </div>
                <div class="amai-store-recent-meta">
                    <span class="amai-store-recent-date"><?php echo esc_html($date); ?></span>
                    <span class="amai-store-recent-status amai-store-status--<?php echo esc_attr($status_class); ?>">
                        <?php echo esc_html(ucfirst($status)); ?>
                    </span>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <?php if ($catalog_health !== 'never_synced' && $catalog_count === 0) : ?>
    <div class="amai-store-empty-state">
        <div class="amai-store-empty-icon">&#128722;</div>
        <div class="amai-store-empty-title">No products in catalog yet</div>
        <div class="amai-store-empty-desc">Your store connected successfully. The catalog will sync tonight at 2:30am UTC. You can also trigger a manual sync above.</div>
    </div>
    <?php endif; ?>

    <?php endif; ?>

</div>