<?php
/**
 * Campaigns Page - Article Marketplace
 *
 * Pro users can create campaigns (ready-to-publish articles)
 * All users (Free + Pro) can browse and claim campaigns
 *
 * @package Consolety
 */

if (!defined('ABSPATH')) {
    exit;
}

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

<div class="vp-dashboard">
    <?php
    $consolety_page_icon = 'dashicons-megaphone';
    $consolety_page_title = __('Campaigns', 'consolety');
    $consolety_page_sub = __('Browse and publish ready-to-go articles from the marketplace.', 'consolety');
    $consolety_show_credits = true;
    $consolety_show_tier = true;
    include CONSOLETY_PLUGIN_DIR . 'admin/views/components/page-header.php';
    ?>

    <?php if (!$consolety_is_connected): ?>
        <div class="vp-card vp-connect-card">
            <div class="vp-connect-icon"><span class="dashicons dashicons-warning vp-icon-lg" style="color: var(--vp-warning);"></span></div>
            <h2><?php esc_html_e('Connect to unlock Campaigns', 'consolety'); ?></h2>
            <p><?php esc_html_e('Campaigns are ready-made articles from the network. Claim one, publish it on your site, and earn points - or (on Pro) create your own campaign and get one article published across multiple verified sites at once.', 'consolety'); ?></p>
            <a href="<?php echo esc_url(admin_url('admin.php?page=consolety')); ?>" class="vp-btn vp-btn-primary"><?php esc_html_e('Connect your site first', 'consolety'); ?></a>
        </div>
    <?php else: ?>

    <!-- Cross-module hint -->
    <div class="vp-cross-hint">
        <span class="dashicons dashicons-info"></span>
        <span>
            <?php esc_html_e('Campaigns = publish ready-to-go articles from the marketplace.', 'consolety'); ?>
            <?php
            printf(
                /* translators: %1$s: opening link tag, %2$s: closing link tag */
                esc_html__('For 1-to-1 content exchange, use %1$sGuest Posts%2$s.', 'consolety'),
                '<a href="' . esc_url(admin_url('admin.php?page=consolety-guest-posts')) . '">',
                '</a>'
            ); ?>
        </span>
    </div>

    <?php
    // $consolety_categories is prepared in render_campaigns() and passed to the view
    ?>

    <!-- Tabs: Discover / My Campaigns / My Claims -->
    <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="my-campaigns" role="tab" aria-selected="false">
            <span class="vp-tab-icon"><span class="dashicons dashicons-admin-page"></span></span>
            <?php esc_html_e('My Campaigns', 'consolety'); ?>
        </button>
        <button type="button" class="vp-tab" data-tab="my-claims" role="tab" aria-selected="false">
            <span class="vp-tab-icon"><span class="dashicons dashicons-clipboard"></span></span>
            <?php esc_html_e('My Claims', 'consolety'); ?>
        </button>
    </div>

    <!-- Discover Tab -->
    <div id="tab-discover" class="vp-tab-content active">
        <div class="vp-card">
            <div class="vp-card-header">
                <h4><?php esc_html_e('Available Campaigns', 'consolety'); ?></h4>
                <p class="vp-card-description"><?php esc_html_e('Browse available campaigns and claim articles to publish on your site.', 'consolety'); ?></p>
            </div>
            <div class="vp-card-body">
                <div id="campaigns-list" class="vp-campaigns-grid">
                    <div class="vp-loading-state">
                        <span class="spinner is-active"></span>
                        <?php esc_html_e('Loading campaigns...', 'consolety'); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- My Campaigns Tab -->
    <div id="tab-my-campaigns" class="vp-tab-content" style="display:none;">
        <div class="vp-card">
            <div class="vp-card-header">
                <div>
                    <h4><?php esc_html_e('My Campaigns', 'consolety'); ?></h4>
                    <p class="vp-card-description"><?php esc_html_e('Manage your campaigns and review claims from publishers.', 'consolety'); ?></p>
                </div>

                <?php if ($consolety_is_pro): ?>
                <button type="button" class="vp-btn vp-btn-primary" id="create-campaign-btn">
                    <span class="dashicons dashicons-plus-alt2"></span>
                    <?php esc_html_e('Create Campaign', 'consolety'); ?>
                </button>
                <?php else: ?>
                <!-- Tell Free users the deal BEFORE they write a campaign, not
                     after the Hub rejects it. Claiming stays free for everyone. -->
                <div class="vp-campaign-pro-gate">
                    <span class="vp-badge vp-badge-soft-amber"><?php esc_html_e('Pro', 'consolety'); ?></span>
                    <span class="vp-campaign-pro-gate-text">
                        <?php esc_html_e('Creating campaigns - publishing one article across multiple network sites - is a Pro feature. Claiming campaigns is free for everyone.', 'consolety'); ?>
                    </span>
                    <a href="<?php echo esc_url($consolety_ctx->upgradeUrl); ?>" class="vp-btn vp-btn-primary">
                        <span class="dashicons dashicons-arrow-up-alt"></span>
                        <?php esc_html_e('Upgrade to Pro - EUR 29.99/mo', 'consolety'); ?>
                    </a>
                </div>
                <?php endif; ?>
            </div>
            <div class="vp-card-body">
                <div id="my-campaigns-list" class="vp-campaigns-grid">
                    <div class="vp-loading-state">
                        <span class="spinner is-active"></span>
                        <?php esc_html_e('Loading your campaigns...', 'consolety'); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- My Claims Tab -->
    <div id="tab-my-claims" class="vp-tab-content" style="display:none;">
        <div class="vp-card">
            <div class="vp-card-header">
                <h4><?php esc_html_e('My Claims', 'consolety'); ?></h4>
                <p class="vp-card-description"><?php esc_html_e('Track the status of campaigns you have claimed.', 'consolety'); ?></p>
            </div>
            <div class="vp-card-body">
                <div id="my-claims-list" class="vp-claims-list">
                    <div class="vp-loading-state">
                        <span class="spinner is-active"></span>
                        <?php esc_html_e('Loading your claims...', 'consolety'); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Create Campaign Modal -->
    <div id="create-campaign-modal" class="vp-modal-overlay" style="display: none;">
        <div class="vp-modal vp-modal-large">
            <div class="vp-modal-header">
                <h2>
                    <span class="dashicons dashicons-edit" style="margin-right: 8px;"></span>
                    <?php esc_html_e('Create Campaign', 'consolety'); ?>
                </h2>
                <button type="button" class="vp-modal-close vp-modal-backdrop-close">
                    <span class="dashicons dashicons-no-alt"></span>
                </button>
            </div>
            <div class="vp-modal-body">
                <div class="vp-modal-notice-area" style="display:none;"></div>
                <form id="create-campaign-form">
                    <div class="vp-form-group">
                        <label for="campaign-title"><?php esc_html_e('Title', 'consolety'); ?> <span class="required">*</span></label>
                        <input type="text" id="campaign-title" name="title" class="vp-input" required maxlength="255" placeholder="<?php esc_attr_e('Enter article title...', 'consolety'); ?>">
                    </div>

                    <div class="vp-form-group vp-form-editor">
                        <label><?php esc_html_e('Content', 'consolety'); ?> <span class="required">*</span></label>
                        <?php
                        wp_editor('', 'campaign-content', [
                            'textarea_name' => 'content',
                            'textarea_rows' => 15,
                            'media_buttons' => false,
                            'teeny' => false,
                            'quicktags' => true,
                        ]);
                        ?>
                        <div class="vp-editor-meta">
                            <span id="campaign-word-count" class="vp-word-count">0 / 100 words</span>
                        </div>
                    </div>

                    <div class="vp-form-group vp-detected-links">
                        <label>
                            <span class="dashicons dashicons-admin-links"></span>
                            <?php esc_html_e('Detected Links', 'consolety'); ?>
                        </label>
                        <div id="campaign-links-list" class="vp-links-list">
                            <p class="vp-no-links"><?php esc_html_e('No links detected yet.', 'consolety'); ?></p>
                        </div>
                    </div>


                    <div class="vp-divider">
                        <span><?php esc_html_e('Publisher Filters', 'consolety'); ?></span>
                    </div>

                    <p class="vp-hint" style="margin-bottom: 16px;"><?php esc_html_e('Set preferences for which publishers can claim. These are soft filters.', 'consolety'); ?></p>

                    <div class="vp-form-group">
                        <label><?php esc_html_e('Preferred Categories', 'consolety'); ?> <span class="vp-hint">(<?php esc_html_e('max 2', 'consolety'); ?>)</span></label>
                        <p class="vp-hint" style="margin-bottom: 12px;"><?php esc_html_e('Select up to 2 category combinations to target specific publisher niches.', 'consolety'); ?></p>

                        <div id="campaign-category-rows" class="vp-category-rows">
                            <div class="vp-category-row" data-row="1">
                                <select class="vp-select campaign-main-cat" data-row="1">
                                    <option value=""><?php esc_html_e('Select a category...', 'consolety'); ?></option>
                                    <?php foreach ($consolety_categories as $cat): ?>
                                        <?php if (empty($cat['parent_id'])): ?>
                                        <option value="<?php echo esc_attr($cat['id']); ?>"><?php echo esc_html($cat['name']); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                                <select class="vp-select campaign-sub-cat" data-row="1" disabled>
                                    <option value=""><?php esc_html_e('Subcategory (optional)', 'consolety'); ?></option>
                                </select>
                                <button type="button" class="vp-btn vp-btn-ghost vp-category-remove" data-row="1" style="display:none;" title="<?php esc_attr_e('Remove', 'consolety'); ?>">
                                    <span class="dashicons dashicons-no-alt"></span>
                                </button>
                            </div>
                        </div>
                        <button type="button" class="vp-btn vp-btn-ghost" id="add-category-row" style="margin-top: 8px;">
                            <span class="dashicons dashicons-plus-alt2"></span>
                            <?php esc_html_e('Add category', 'consolety'); ?>
                        </button>
                    </div>

                    <div class="vp-form-group">
                        <label for="campaign-locale"><?php esc_html_e('Content Language & Target Market', 'consolety'); ?> <span class="required">*</span></label>
                        <select id="campaign-locale" name="locale" class="vp-select" required>
                            <option value=""><?php esc_html_e('Select language and region...', 'consolety'); ?></option>

                            <optgroup label="<?php esc_attr_e('English', 'consolety'); ?>">
                                <option value="en_US"><?php esc_html_e('English (United States)', 'consolety'); ?></option>
                                <option value="en_GB"><?php esc_html_e('English (United Kingdom)', 'consolety'); ?></option>
                                <option value="en_CA"><?php esc_html_e('English (Canada)', 'consolety'); ?></option>
                                <option value="en_AU"><?php esc_html_e('English (Australia)', 'consolety'); ?></option>
                                <option value="en_NZ"><?php esc_html_e('English (New Zealand)', 'consolety'); ?></option>
                                <option value="en_IE"><?php esc_html_e('English (Ireland)', 'consolety'); ?></option>
                                <option value="en_IN"><?php esc_html_e('English (India)', 'consolety'); ?></option>
                                <option value="en_ZA"><?php esc_html_e('English (South Africa)', 'consolety'); ?></option>
                            </optgroup>

                            <optgroup label="<?php esc_attr_e('Dutch', 'consolety'); ?>">
                                <option value="nl_NL"><?php esc_html_e('Nederlands (Nederland)', 'consolety'); ?></option>
                                <option value="nl_BE"><?php esc_html_e('Nederlands (Belgie)', 'consolety'); ?></option>
                            </optgroup>

                            <optgroup label="<?php esc_attr_e('German', 'consolety'); ?>">
                                <option value="de_DE"><?php esc_html_e('Deutsch (Deutschland)', 'consolety'); ?></option>
                                <option value="de_AT"><?php esc_html_e('Deutsch (Osterreich)', 'consolety'); ?></option>
                                <option value="de_CH"><?php esc_html_e('Deutsch (Schweiz)', 'consolety'); ?></option>
                            </optgroup>

                            <optgroup label="<?php esc_attr_e('French', 'consolety'); ?>">
                                <option value="fr_FR"><?php esc_html_e('Francais (France)', 'consolety'); ?></option>
                                <option value="fr_BE"><?php esc_html_e('Francais (Belgique)', 'consolety'); ?></option>
                                <option value="fr_CH"><?php esc_html_e('Francais (Suisse)', 'consolety'); ?></option>
                                <option value="fr_CA"><?php esc_html_e('Francais (Canada)', 'consolety'); ?></option>
                            </optgroup>

                            <optgroup label="<?php esc_attr_e('Spanish', 'consolety'); ?>">
                                <option value="es_ES"><?php esc_html_e('Espanol (Espana)', 'consolety'); ?></option>
                                <option value="es_MX"><?php esc_html_e('Espanol (Mexico)', 'consolety'); ?></option>
                                <option value="es_AR"><?php esc_html_e('Espanol (Argentina)', 'consolety'); ?></option>
                                <option value="es_CO"><?php esc_html_e('Espanol (Colombia)', 'consolety'); ?></option>
                            </optgroup>

                            <optgroup label="<?php esc_attr_e('Portuguese', 'consolety'); ?>">
                                <option value="pt_PT"><?php esc_html_e('Portugues (Portugal)', 'consolety'); ?></option>
                                <option value="pt_BR"><?php esc_html_e('Portugues (Brasil)', 'consolety'); ?></option>
                            </optgroup>

                            <optgroup label="<?php esc_attr_e('Italian', 'consolety'); ?>">
                                <option value="it_IT"><?php esc_html_e('Italiano (Italia)', 'consolety'); ?></option>
                                <option value="it_CH"><?php esc_html_e('Italiano (Svizzera)', 'consolety'); ?></option>
                            </optgroup>

                            <optgroup label="<?php esc_attr_e('Nordic', 'consolety'); ?>">
                                <option value="sv_SE"><?php esc_html_e('Svenska (Sverige)', 'consolety'); ?></option>
                                <option value="da_DK"><?php esc_html_e('Dansk (Danmark)', 'consolety'); ?></option>
                                <option value="no_NO"><?php esc_html_e('Norsk (Norge)', 'consolety'); ?></option>
                                <option value="fi_FI"><?php esc_html_e('Suomi (Suomi)', 'consolety'); ?></option>
                            </optgroup>

                            <optgroup label="<?php esc_attr_e('Eastern European', 'consolety'); ?>">
                                <option value="pl_PL"><?php esc_html_e('Polski (Polska)', 'consolety'); ?></option>
                                <option value="cs_CZ"><?php esc_html_e('Cestina (Cesko)', 'consolety'); ?></option>
                                <option value="ru_RU"><?php esc_html_e('Russkij (Rossiya)', 'consolety'); ?></option>
                                <option value="uk_UA"><?php esc_html_e('Ukrainska (Ukraina)', 'consolety'); ?></option>
                            </optgroup>

                            <optgroup label="<?php esc_attr_e('Asian', 'consolety'); ?>">
                                <option value="ja_JP"><?php esc_html_e('Nihongo (Japan)', 'consolety'); ?></option>
                                <option value="ko_KR"><?php esc_html_e('Hangugeo (Korea)', 'consolety'); ?></option>
                                <option value="zh_CN"><?php esc_html_e('Zhongwen (China)', 'consolety'); ?></option>
                                <option value="zh_TW"><?php esc_html_e('Zhongwen (Taiwan)', 'consolety'); ?></option>
                            </optgroup>
                        </select>
                        <p class="vp-filter-hint"><?php esc_html_e('One campaign = one language + one target market. Publishers matching this locale will see your campaign first.', 'consolety'); ?></p>
                    </div>

                    <div class="vp-info-box">
                        <span class="dashicons dashicons-info"></span>
                        <?php esc_html_e('Creating a campaign costs 30 points (reserved from your balance). Points are finalized when the article is published.', 'consolety'); ?>
                    </div>
                </form>
            </div>
            <div class="vp-modal-footer">
                <button type="button" class="vp-btn vp-btn-secondary vp-modal-backdrop-close" id="cancel-create-campaign"><?php esc_html_e('Cancel', 'consolety'); ?></button>
                <button type="button" class="vp-btn vp-btn-primary" id="submit-create-campaign">
                    <span class="dashicons dashicons-yes"></span>
                    <?php esc_html_e('Create Campaign (30 pts)', 'consolety'); ?>
                </button>
            </div>
        </div>
    </div>

    <!-- View Campaign Modal -->
    <div id="view-campaign-modal" class="vp-modal-overlay" style="display: none;">
        <div class="vp-modal vp-modal-large">
            <div class="vp-modal-header">
                <h2 id="view-campaign-title"><?php esc_html_e('Campaign Details', 'consolety'); ?></h2>
                <button type="button" class="vp-modal-close vp-modal-backdrop-close">
                    <span class="dashicons dashicons-no-alt"></span>
                </button>
            </div>
            <div class="vp-modal-body" id="view-campaign-body">
                <!-- Content loaded via JS -->
            </div>
            <div class="vp-modal-footer" id="view-campaign-footer">
                <!-- Buttons loaded via JS -->
            </div>
        </div>
    </div>

    <!-- Claim Campaign Modal -->
    <div id="claim-campaign-modal" class="vp-modal-overlay" style="display: none;">
        <div class="vp-modal">
            <div class="vp-modal-header">
                <h2><?php esc_html_e('Claim Campaign', 'consolety'); ?></h2>
                <button type="button" class="vp-modal-close vp-modal-backdrop-close">
                    <span class="dashicons dashicons-no-alt"></span>
                </button>
            </div>
            <div class="vp-modal-body">
                <div class="vp-modal-notice-area" style="display:none;"></div>
                <p style="color: var(--vp-text-muted); margin-bottom: 16px;"><?php esc_html_e('Write a short pitch to the campaign owner explaining why your site is a good fit.', 'consolety'); ?></p>
                <form id="claim-campaign-form">
                    <input type="hidden" id="claim-campaign-id" name="campaign_id" value="">
                    <div class="vp-form-group">
                        <label for="claim-pitch"><?php esc_html_e('Your Pitch (optional)', 'consolety'); ?></label>
                        <textarea id="claim-pitch" name="pitch" class="vp-textarea" rows="4" maxlength="1000" placeholder="<?php esc_attr_e('Describe your site and why it\'s a good match for this article...', 'consolety'); ?>"></textarea>
                    </div>
                    <div class="vp-info-box" style="line-height: 1.6;">
                        <span class="dashicons dashicons-info" style="margin-top: 2px;"></span>
                        <span>
                            <?php esc_html_e('Claiming reserves 5 points from your balance.', 'consolety'); ?><br>
                            <strong style="color: var(--vp-success);"><?php esc_html_e('If accepted:', 'consolety'); ?></strong>
                            <?php
                            /* translators: %d: number of points earned for publishing */
                            printf(esc_html__('You get your 5 points back + earn %d points for publishing.', 'consolety'), intval(\Consolety\PointsEconomy::getHostReward())); ?><br>
                            <strong style="color: var(--vp-text-muted);"><?php esc_html_e('If not selected:', 'consolety'); ?></strong>
                            <?php esc_html_e('Your 5 points are automatically refunded.', 'consolety'); ?>
                        </span>
                    </div>
                </form>
            </div>
            <div class="vp-modal-footer">
                <button type="button" class="vp-btn vp-btn-secondary vp-modal-backdrop-close" id="cancel-claim-campaign"><?php esc_html_e('Cancel', 'consolety'); ?></button>
                <button type="button" class="vp-btn vp-btn-primary" id="submit-claim-campaign">
                    <span class="dashicons dashicons-yes"></span>
                    <?php esc_html_e('Claim Campaign (5 pts)', 'consolety'); ?>
                </button>
            </div>
        </div>
    </div>

    <!-- Publish Campaign Modal (publisher-side) -->
    <div id="publish-campaign-modal" class="vp-modal-overlay" style="display: none;">
        <div class="vp-modal">
            <div class="vp-modal-header">
                <h2>
                    <span class="dashicons dashicons-upload" style="margin-right: 8px;"></span>
                    <?php esc_html_e('Publish Campaign Article', 'consolety'); ?>
                </h2>
                <button type="button" class="vp-modal-close vp-modal-backdrop-close">
                    <span class="dashicons dashicons-no-alt"></span>
                </button>
            </div>
            <div class="vp-modal-body">
                <div class="vp-modal-notice-area" style="display:none;"></div>
                <p id="publish-campaign-title-display" style="font-weight: 600; font-size: 15px; margin-bottom: 16px;"></p>
                <form id="publish-campaign-form">
                    <input type="hidden" id="publish-campaign-id" value="">
                    <input type="hidden" id="publish-claim-id" value="">
                    <div class="vp-form-group">
                        <label for="publish-campaign-category"><?php esc_html_e('Category', 'consolety'); ?></label>
                        <select id="publish-campaign-category" class="vp-select">
                            <?php
                            $consolety_wp_categories = get_categories(['hide_empty' => false, 'orderby' => 'name']);
                            foreach ($consolety_wp_categories as $cat) {
                                printf(
                                    '<option value="%s">%s</option>',
                                    esc_attr($cat->term_id),
                                    esc_html($cat->name)
                                );
                            }
                            ?>
                        </select>
                    </div>
                    <div class="vp-info-box">
                        <span class="dashicons dashicons-info"></span>
                        <?php esc_html_e('The article will be published immediately on your site.', 'consolety'); ?>
                    </div>
                </form>
            </div>
            <div class="vp-modal-footer">
                <button type="button" class="vp-btn vp-btn-secondary vp-modal-backdrop-close"><?php esc_html_e('Cancel', 'consolety'); ?></button>
                <button type="button" class="vp-btn vp-btn-primary" id="submit-publish-campaign">
                    <span class="dashicons dashicons-yes"></span>
                    <?php esc_html_e('Publish Article', 'consolety'); ?>
                </button>
            </div>
        </div>
    </div>

    <!-- Review Claims Modal -->
    <div id="review-claims-modal" class="vp-modal-overlay" style="display: none;">
        <div class="vp-modal vp-modal-large">
            <div class="vp-modal-header">
                <h2><?php esc_html_e('Review Claims', 'consolety'); ?></h2>
                <button type="button" class="vp-modal-close vp-modal-backdrop-close">
                    <span class="dashicons dashicons-no-alt"></span>
                </button>
            </div>
            <div class="vp-modal-body" id="review-claims-body">
                <div class="vp-modal-notice-area" style="display:none;"></div>
                <!-- Claims loaded via JS -->
            </div>
            <div class="vp-modal-footer">
                <button type="button" class="vp-btn vp-btn-secondary vp-modal-backdrop-close"><?php esc_html_e('Close', 'consolety'); ?></button>
            </div>
        </div>
    </div>

    <!-- Custom Confirm Modal -->
    <div id="vp-confirm-modal" class="vp-modal-overlay" style="display: none;">
        <div class="vp-modal vp-modal-small">
            <div class="vp-modal-header">
                <h2 id="vp-confirm-title"><?php esc_html_e('Confirm', 'consolety'); ?></h2>
                <button type="button" class="vp-modal-close vp-modal-backdrop-close">
                    <span class="dashicons dashicons-no-alt"></span>
                </button>
            </div>
            <div class="vp-modal-body">
                <p id="vp-confirm-message"></p>
            </div>
            <div class="vp-modal-footer">
                <button type="button" class="vp-btn vp-btn-secondary" id="vp-confirm-cancel"><?php esc_html_e('Cancel', 'consolety'); ?></button>
                <button type="button" class="vp-btn vp-btn-primary" id="vp-confirm-ok"><?php esc_html_e('Confirm', 'consolety'); ?></button>
            </div>
        </div>
    </div>

    <?php endif; ?>
</div>

