<?php
/**
 * Network Page - Partner Discovery with Soft Connect
 *
 * @package Consolety
 */
if (!defined('ABSPATH')) exit;

$consolety_ctx = \Consolety\PageContext::get();
$consolety_is_connected = $consolety_ctx->isConnected;
$consolety_credits = $consolety_ctx->credits;
?>

<div class="vp-dashboard">
    <?php
    $consolety_page_icon = 'dashicons-admin-site';
    $consolety_page_title = __('Network', 'consolety');
    $consolety_page_sub = __('Discover and connect with partners in your niche. Build your content network.', 'consolety');
    $consolety_show_credits = true;
    $consolety_show_tier = true;
    include CONSOLETY_PLUGIN_DIR . 'admin/views/components/page-header.php';
    ?>

    <?php if (!$consolety_is_connected): ?>
        <?php
        $consolety_preview = $consolety_ctx->getNetworkPreview();
        $consolety_pubs    = $consolety_preview['publishers'] ?? [];
        ?>
        <div class="vp-notice vp-notice-info">
            <span class="dashicons dashicons-visibility"></span>
            <span>
                <?php esc_html_e('You are previewing the live network. Connect your site to filter by niche, contact publishers, and exchange guest posts.', 'consolety'); ?>
                <a href="<?php echo esc_url($consolety_ctx->oauthUrl); ?>" style="font-weight: 600; margin-left: 4px;"><?php esc_html_e('Connect with Google', 'consolety'); ?></a>
            </span>
        </div>

        <?php if (!empty($consolety_pubs)): ?>
        <div class="vp-card">
            <div class="vp-onboard-preview-head">
                <h2><?php esc_html_e('Verified publishers', 'consolety'); ?></h2>
                <span class="vp-onboard-preview-note"><?php esc_html_e('Read-only preview', 'consolety'); ?></span>
            </div>
            <div class="vp-preview-grid">
                <?php foreach ($consolety_pubs as $consolety_pub) :
                    include CONSOLETY_PLUGIN_DIR . 'admin/views/components/preview-publisher-card.php';
                endforeach; ?>
            </div>
            <div class="vp-onboard-cta-row">
                <a href="<?php echo esc_url($consolety_ctx->oauthUrl); ?>" class="vp-btn vp-btn-primary vp-btn-lg"><?php esc_html_e('Connect with Google to join', 'consolety'); ?></a>
            </div>
        </div>
        <?php else: ?>
        <div class="vp-card vp-connect-card">
            <div class="vp-connect-icon"><span class="dashicons dashicons-admin-site vp-icon-lg"></span></div>
            <h2><?php esc_html_e('Discover verified publishers', 'consolety'); ?></h2>
            <p><?php esc_html_e('Connect your site to browse the network and start exchanging guest posts.', 'consolety'); ?></p>
            <a href="<?php echo esc_url($consolety_ctx->oauthUrl); ?>" class="vp-btn vp-btn-primary"><?php esc_html_e('Connect with Google', 'consolety'); ?></a>
        </div>
        <?php endif; ?>
    <?php else: ?>

    <?php
    // Profile completeness check
    $consolety_profile_category = $consolety_ctx->siteInfo['category_id'] ?? 0;
    $consolety_profile_language = $consolety_ctx->siteInfo['language'] ?? '';
    $consolety_profile_incomplete = empty($consolety_profile_category) || empty($consolety_profile_language);
    ?>

    <?php if ($consolety_profile_incomplete): ?>
    <div class="vp-notice vp-notice-warning">
        <span class="dashicons dashicons-warning" style="color: var(--vp-warning); flex-shrink: 0;"></span>
        <span>
            <?php esc_html_e('Your site profile is incomplete. Partners see your category and language when deciding to connect.', 'consolety'); ?>
            <a href="<?php echo esc_url(admin_url('admin.php?page=consolety')); ?>" style="color: #92400e; font-weight: 600; margin-left: 4px;">
                <?php esc_html_e('Complete your profile', 'consolety'); ?> &rarr;
            </a>
        </span>
    </div>
    <?php endif; ?>

    <!-- Tabs: Discover / Pending / Connected -->
    <div class="vp-tabs-row" role="tablist">
        <button type="button" class="vp-tab active" data-tab="discover" role="tab" aria-selected="true">
            <span class="vp-tab-icon"><span class="dashicons dashicons-search"></span></span>
            <?php esc_html_e('Discover', 'consolety'); ?>
        </button>
        <button type="button" class="vp-tab" data-tab="pending" role="tab" aria-selected="false">
            <span class="vp-tab-icon"><span class="dashicons dashicons-clock"></span></span>
            <?php esc_html_e('Pending', 'consolety'); ?>
            <span class="vp-tab-badge" id="pending-count">0</span>
        </button>
        <button type="button" class="vp-tab" data-tab="connected" role="tab" aria-selected="false">
            <span class="vp-tab-icon"><span class="dashicons dashicons-yes-alt"></span></span>
            <?php esc_html_e('Connected', 'consolety'); ?>
            <span class="vp-tab-badge" id="connected-count">0</span>
        </button>
        <a href="<?php echo esc_url(admin_url('admin.php?page=consolety-guest-posts')); ?>" class="vp-tab vp-tab-link">
            <span class="vp-tab-icon"><span class="dashicons dashicons-admin-post"></span></span>
            <?php esc_html_e('Guest Posts', 'consolety'); ?> &rarr;
        </a>
    </div>

    <!-- Discover Tab -->
    <div id="tab-discover" class="vp-tab-content active">
        <div class="vp-network-filter-bar">
            <div class="vp-filter-bar-row">
                <input type="search" id="network-search" class="vp-input" placeholder="<?php esc_attr_e('Search...', 'consolety'); ?>">
                <select id="filter-category" class="vp-select">
                    <option value="" selected><?php esc_html_e('All Categories', 'consolety'); ?></option>
                </select>
                <select id="filter-subcategory" class="vp-select" disabled>
                    <option value="" selected><?php esc_html_e('All Subcategories', 'consolety'); ?></option>
                </select>
                <p class="vp-filter-helper" id="subcategory-helper" style="margin: 2px 0 0; font-size: 12px; color: var(--vp-text-muted); display: none;"><?php esc_html_e('Select a main category first', 'consolety'); ?></p>
                <select id="filter-language" class="vp-select">
                    <option value="" selected><?php esc_html_e('All Languages', 'consolety'); ?></option>
                </select>
                <div class="vp-filter-divider"></div>
                <label class="vp-filter-toggle active" id="vp-sensitive-toggle">
                    <span class="dashicons dashicons-hidden"></span>
                    <input type="checkbox" id="filter-hide-sensitive" checked>
                    <span><?php esc_html_e('Hide Sensitive', 'consolety'); ?></span>
                </label>
                <span class="vp-partner-count-text" id="vp-partner-count-text"></span>
            </div>
            <div id="vp-active-filters" class="vp-active-filters" style="display:none;">
                <span class="vp-active-filters-label"><?php esc_html_e('Active filters:', 'consolety'); ?></span>
                <div id="vp-filter-chips" class="vp-filter-chips"></div>
                <button type="button" id="vp-clear-all-filters" class="vp-btn vp-btn-ghost vp-btn-sm"><?php esc_html_e('Clear all', 'consolety'); ?></button>
            </div>
        </div>
        <div id="network-grid" class="vp-partner-grid">
            <div class="vp-loading-state">
                <span class="spinner is-active"></span>
                <?php esc_html_e('Loading partners...', 'consolety'); ?>
            </div>
        </div>
    </div>

    <!-- Pending Tab -->
    <div id="tab-pending" class="vp-tab-content" style="display:none;">
        <div class="vp-card">
            <div class="vp-card-header">
                <h4><span class="dashicons dashicons-email" style="margin-right: 8px;"></span> <?php esc_html_e('Received Requests', 'consolety'); ?></h4>
            </div>
            <div class="vp-card-body" id="pending-received"></div>
        </div>

        <div class="vp-card" style="margin-top: 20px;">
            <div class="vp-card-header">
                <h4><span class="dashicons dashicons-upload" style="margin-right: 8px;"></span> <?php esc_html_e('Sent Requests', 'consolety'); ?></h4>
            </div>
            <div class="vp-card-body" id="pending-sent"></div>
        </div>
    </div>

    <!-- Connected Tab -->
    <div id="tab-connected" class="vp-tab-content" style="display:none;">
        <div class="vp-card">
            <div class="vp-card-header">
                <h4><span class="dashicons dashicons-groups" style="margin-right: 8px;"></span> <?php esc_html_e('Your Partners', 'consolety'); ?></h4>
            </div>
            <div class="vp-card-body">
                <div id="connected-list" class="vp-partner-grid"></div>
            </div>
        </div>
    </div>

    <?php endif; ?>
</div>


<!-- JavaScript is loaded via wp_enqueue_script in class-admin.php -->
