<?php
/**
 * Guest Posts
 *
 * @package Consolety
 */

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

$consolety_ctx = \Consolety\PageContext::get();
$consolety_is_connected = $consolety_ctx->isConnected;
$consolety_credits = $consolety_ctx->credits;
$consolety_credits_reserved = $consolety_ctx->creditsReserved;
$consolety_credit_cost = \Consolety\PointsEconomy::getSubmitCost();
$consolety_host_reward = \Consolety\PointsEconomy::getHostReward();
?>

<div class="vp-dashboard">
    <?php
    $consolety_page_icon = 'dashicons-admin-post';
    $consolety_page_title = __('Guest Posts', 'consolety');
    $consolety_page_sub = __('Submit guest posts to partners or review submissions from your 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): ?>
        <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 Guest Posts', 'consolety'); ?></h2>
            <p><?php esc_html_e('This is where your backlinks happen: write a guest post, submit it to a verified partner site, and manage the posts others submit to you. Connect with Google first - it takes one click.', '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('Guest Posts = 1-to-1 content exchange with connected partners.', 'consolety'); ?>
            <?php
            printf(
                /* translators: %1$s: opening link tag, %2$s: closing link tag */
                esc_html__('Looking for 1-to-many publishing? Try %1$sCampaigns%2$s.', 'consolety'),
                '<a href="' . esc_url(admin_url('admin.php?page=consolety-campaigns')) . '">',
                '</a>'
            ); ?>
        </span>
    </div>

    <!-- Points Info Banner -->
    <div class="vp-info-banner">
        <div class="vp-info-icon"><span class="dashicons dashicons-chart-line" style="font-size: 20px; width: 20px; height: 20px;"></span></div>
        <div class="vp-info-text">
            <strong><?php
            /* translators: %d: number of points to submit */
            printf(esc_html__('Submit a guest post: %d points', 'consolety'), intval($consolety_credit_cost)); ?></strong>
            &nbsp;&bull;&nbsp;
            <span><?php
            /* translators: %d: number of points earned for hosting */
            printf(esc_html__('Host a guest post: +%d points', 'consolety'), intval($consolety_host_reward)); ?></span>
        </div>
    </div>

    <!-- Primary Tabs -->
    <div class="vp-primary-tabs">
        <button type="button" class="vp-primary-tab active" data-tab="my-submissions">
            <span class="vp-tab-icon"><span class="dashicons dashicons-clipboard"></span></span>
            <?php esc_html_e('My Orders', 'consolety'); ?>
            <span class="vp-tab-counter" id="count-submissions">0</span>
        </button>
        <button type="button" class="vp-primary-tab" data-tab="pending-review">
            <span class="vp-tab-icon"><span class="dashicons dashicons-email"></span></span>
            <?php esc_html_e('Requests', 'consolety'); ?>
            <span class="vp-tab-counter vp-tab-pending" id="count-pending">0</span>
        </button>
        <button type="button" class="vp-primary-tab" data-tab="published">
            <span class="vp-tab-icon"><span class="dashicons dashicons-yes-alt"></span></span>
            <?php esc_html_e('Published', 'consolety'); ?>
            <span class="vp-tab-counter" id="count-published">0</span>
        </button>
    </div>

    <!-- Filter Bar -->
    <div class="vp-filter-bar">
        <div class="vp-filter-group">
            <label><?php esc_html_e('Status', 'consolety'); ?></label>
            <select id="filter-status" class="vp-filter-select">
                <option value=""><?php esc_html_e('All Statuses', 'consolety'); ?></option>
                <option value="draft"><?php esc_html_e('Draft', 'consolety'); ?></option>
                <option value="pending_review"><?php esc_html_e('Pending Review', 'consolety'); ?></option>
                <option value="revision_requested"><?php esc_html_e('Revision Requested', 'consolety'); ?></option>
                <option value="approved"><?php esc_html_e('Published', 'consolety'); ?></option>
                <option value="rejected"><?php esc_html_e('Rejected', 'consolety'); ?></option>
            </select>
        </div>
        <div class="vp-filter-group vp-filter-search">
            <label><?php esc_html_e('Search', 'consolety'); ?></label>
            <input type="text" id="filter-search" class="vp-filter-input" placeholder="<?php esc_attr_e('Search by title or website...', 'consolety'); ?>">
        </div>
        <button type="button" id="btn-clear-filters" class="vp-btn vp-btn-ghost"><?php esc_html_e('Clear', 'consolety'); ?></button>
    </div>

    <div class="vp-main-layout">
        <div class="vp-content-area">
            <!-- My Submissions Tab -->
            <div id="tab-my-submissions" class="vp-tab-panel active">
                <div class="vp-tab-actions">
                    <button type="button" id="btn-new-guest-post" class="vp-btn vp-btn-primary">
                        <span class="dashicons dashicons-edit"></span>
                        <?php esc_html_e('New Guest Post', 'consolety'); ?>
                    </button>
                    <a href="<?php echo esc_url(admin_url('admin.php?page=consolety-network&tab=connected')); ?>" class="vp-btn vp-btn-ghost">
                        <span class="dashicons dashicons-networking"></span>
                        <?php esc_html_e('Find Partners in Network', 'consolety'); ?>
                    </a>
                </div>
                <div class="vp-table-wrapper">
                    <table class="vp-dark-table">
                        <thead>
                            <tr>
                                <th width="80"><?php esc_html_e('Order #', 'consolety'); ?></th>
                                <th><?php esc_html_e('Title', 'consolety'); ?></th>
                                <th width="140"><?php esc_html_e('Website', 'consolety'); ?></th>
                                <th width="120"><?php esc_html_e('Status', 'consolety'); ?></th>
                                <th width="100"><?php esc_html_e('Date', 'consolety'); ?></th>
                                <th width="100"><?php esc_html_e('Actions', 'consolety'); ?></th>
                            </tr>
                        </thead>
                        <tbody id="submissions-tbody"></tbody>
                    </table>
                    <div id="submissions-empty" class="vp-empty-view" style="display:none;">
                        <div class="vp-empty-art"><span class="dashicons dashicons-clipboard" style="font-size:48px;width:48px;height:48px;"></span></div>
                        <h3><?php esc_html_e('No submissions yet', 'consolety'); ?></h3>
                        <p><?php esc_html_e('Go to Network to find partners and submit guest posts.', 'consolety'); ?></p>
                        <a href="<?php echo esc_url(admin_url('admin.php?page=consolety-network&tab=connected')); ?>" class="vp-btn vp-btn-primary" style="margin-top:12px;">
                            <span class="dashicons dashicons-networking"></span>
                            <?php esc_html_e('Go to Network', 'consolety'); ?>
                        </a>
                    </div>
                    <div id="submissions-loading" class="vp-loading-view">
                        <span class="spinner is-active"></span>
                        <?php esc_html_e('Loading...', 'consolety'); ?>
                    </div>
                </div>
            </div>

            <!-- Pending Review Tab -->
            <div id="tab-pending-review" class="vp-tab-panel">
                <div class="vp-table-wrapper">
                    <table class="vp-dark-table">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Title', 'consolety'); ?></th>
                                <th width="150"><?php esc_html_e('From', 'consolety'); ?></th>
                                <th width="100"><?php esc_html_e('Words', 'consolety'); ?></th>
                                <th width="140"><?php esc_html_e('Received', 'consolety'); ?></th>
                                <th width="120"><?php esc_html_e('Action', 'consolety'); ?></th>
                            </tr>
                        </thead>
                        <tbody id="pending-tbody"></tbody>
                    </table>
                    <div id="pending-empty" class="vp-empty-view" style="display:none;">
                        <div class="vp-empty-art"><span class="dashicons dashicons-star-filled" style="font-size:48px;width:48px;height:48px;color:#f59e0b;"></span></div>
                        <h3><?php esc_html_e('No pending reviews', 'consolety'); ?></h3>
                        <p><?php esc_html_e('Guest posts from your partners will appear here.', 'consolety'); ?></p>
                    </div>
                    <div id="pending-loading" class="vp-loading-view">
                        <span class="spinner is-active"></span>
                        <?php esc_html_e('Loading...', 'consolety'); ?>
                    </div>
                </div>
            </div>

            <!-- Published Tab -->
            <div id="tab-published" class="vp-tab-panel">
                <div class="vp-table-wrapper">
                    <table class="vp-dark-table">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Title', 'consolety'); ?></th>
                                <th width="150"><?php esc_html_e('From', 'consolety'); ?></th>
                                <th width="140"><?php esc_html_e('Published', 'consolety'); ?></th>
                                <th width="120"><?php esc_html_e('View', 'consolety'); ?></th>
                            </tr>
                        </thead>
                        <tbody id="published-tbody"></tbody>
                    </table>
                    <div id="published-empty" class="vp-empty-view" style="display:none;">
                        <div class="vp-empty-art"><span class="dashicons dashicons-book" style="font-size:48px;width:48px;height:48px;color:#10b981;"></span></div>
                        <h3><?php esc_html_e('No published posts yet', 'consolety'); ?></h3>
                        <p><?php esc_html_e('Approved guest posts will appear here.', 'consolety'); ?></p>
                    </div>
                    <div id="published-loading" class="vp-loading-view">
                        <span class="spinner is-active"></span>
                        <?php esc_html_e('Loading...', 'consolety'); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- New Guest Post Modal -->
    <div id="modal-new-post" class="vp-gp-modal" style="display:none;">
        <div class="vp-gp-modal-overlay"></div>
        <div class="vp-gp-modal-content vp-gp-modal-wide">
            <div class="vp-gp-modal-header">
                <h2><span class="dashicons dashicons-edit" style="margin-right:8px;"></span><?php esc_html_e('Submit Guest Post', 'consolety'); ?></h2>
                <button type="button" class="vp-gp-modal-close">&times;</button>
            </div>
            <div class="vp-gp-modal-body">
                <div id="modal-notice-area" class="vp-modal-notice-area" style="display:none;"></div>
                <form id="form-new-post">
                    <div class="vp-form-group vp-partner-selection-area">
                        <div id="partner-label-wrap">
                            <label><?php esc_html_e('Select Partner Site', 'consolety'); ?> <span class="required">*</span></label>
                        </div>
                        <input type="hidden" id="gp-partner" name="host_site_id" required>
                        <?php include __DIR__ . '/components/partner-selection-cards.php'; ?>
                    </div>

                    <div class="vp-form-group">
                        <label><?php esc_html_e('Title', 'consolety'); ?> <span class="required">*</span></label>
                        <input type="text" id="gp-title" name="title" maxlength="255" required placeholder="<?php esc_attr_e('Enter a compelling 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('', 'gp-content', [
                            'textarea_name' => 'content',
                            'textarea_rows' => 20,
                            'media_buttons' => false,
                            'teeny' => false,
                            'quicktags' => true,
                        ]);
                        ?>
                        <div class="vp-editor-meta">
                            <span id="word-count">0 words</span>
                        </div>
                    </div>

                    <div class="vp-form-group vp-detected-links">
                        <label><span class="dashicons dashicons-admin-links" style="margin-right:6px;"></span><?php esc_html_e('Detected Links', 'consolety'); ?></label>
                        <div id="links-list" class="vp-links-list">
                            <p class="vp-no-links"><?php esc_html_e('No links detected yet. Add links in the editor above.', 'consolety'); ?></p>
                        </div>
                    </div>

                    <div class="vp-form-footer">
                        <div class="vp-credit-info">
                            <span class="dashicons dashicons-info"></span>
                            <?php
                            /* translators: %d: number of points reserved on submission */
                            printf(esc_html__('Submitting reserves %d points (deducted when approved)', 'consolety'), intval($consolety_credit_cost)); ?>
                        </div>
                        <div class="vp-form-buttons">
                            <button type="button" class="vp-btn vp-btn-outline" id="btn-save-draft">
                                <?php esc_html_e('Save Draft', 'consolety'); ?>
                            </button>
                            <button type="submit" class="vp-btn vp-btn-primary" id="btn-submit">
                                <?php esc_html_e('Submit for Review', 'consolety'); ?>
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Review Modal -->
    <div id="modal-review" class="vp-gp-modal" style="display:none;">
        <div class="vp-gp-modal-overlay"></div>
        <div class="vp-gp-modal-content vp-gp-modal-wide">
            <div class="vp-gp-modal-header">
                <h2><span class="dashicons dashicons-clipboard" style="margin-right:8px;"></span><?php esc_html_e('Review Guest Post', 'consolety'); ?></h2>
                <button type="button" class="vp-gp-modal-close">&times;</button>
            </div>
            <div class="vp-gp-modal-body">
                <div id="modal-review-notice-area" class="vp-modal-notice-area" style="display:none;"></div>
                <div class="vp-review-grid">
                    <div class="vp-review-content">
                        <h3 id="review-title"></h3>
                        <div class="vp-review-meta">
                            <span><strong><?php esc_html_e('From:', 'consolety'); ?></strong> <span id="review-from"></span></span>
                            <span><strong><?php esc_html_e('Words:', 'consolety'); ?></strong> <span id="review-words"></span></span>
                        </div>
                        <div id="review-body" class="vp-review-body"></div>
                        <div id="review-links" class="vp-review-links"></div>
                        <div id="review-bio" class="vp-review-bio"></div>
                    </div>
                    <div class="vp-review-actions">
                        <input type="hidden" id="review-post-id">

                        <div class="vp-action-card vp-action-approve">
                            <h4><span class="dashicons dashicons-yes-alt" style="color:#10b981;margin-right:6px;"></span><?php esc_html_e('Approve & Publish', 'consolety'); ?></h4>
                            <p><?php esc_html_e('Creates a published post on your site.', 'consolety'); ?></p>

                            <div class="vp-form-group" style="margin-bottom:12px;">
                                <label for="approve-category"><?php esc_html_e('Category', 'consolety'); ?></label>
                                <select id="approve-category">
                                    <?php
                                    $consolety_categories = get_categories(['hide_empty' => false, 'orderby' => 'name']);
                                    foreach ($consolety_categories as $cat) {
                                        $consolety_is_selected = ($cat->slug === 'guest-posts' || $cat->name === 'Guest Posts');
                                        echo '<option value="' . esc_attr($cat->term_id) . '"' . selected($consolety_is_selected, true, false) . '>' . esc_html($cat->name) . '</option>';
                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="vp-form-group vp-featured-image-group" style="margin-bottom:12px;">
                                <label for="approve-featured-image"><?php esc_html_e('Featured Image', 'consolety'); ?> <span class="optional">(<?php esc_html_e('optional', 'consolety'); ?>)</span></label>
                                <input type="hidden" id="approve-featured-image-id" value="">
                                <div id="featured-image-preview" class="vp-featured-image-preview" style="display:none;">
                                    <img src="" alt="Featured Image Preview" id="featured-image-img">
                                    <button type="button" class="vp-btn-remove-image" id="btn-remove-featured-image" title="<?php esc_attr_e('Remove image', 'consolety'); ?>">
                                        <span class="dashicons dashicons-no-alt"></span>
                                    </button>
                                </div>
                                <button type="button" class="vp-btn vp-btn-outline vp-btn-sm" id="btn-select-featured-image">
                                    <span class="dashicons dashicons-format-image"></span>
                                    <?php esc_html_e('Select Image', 'consolety'); ?>
                                </button>
                            </div>

                            <button type="button" class="vp-btn vp-btn-success" id="btn-approve"><?php esc_html_e('Approve & Publish', 'consolety'); ?></button>
                            <span class="vp-credit-earn">+<?php echo intval($consolety_host_reward); ?> points</span>
                        </div>

                        <div class="vp-action-card vp-action-revision">
                            <h4><span class="dashicons dashicons-edit" style="margin-right:6px;"></span><?php esc_html_e('Request Revision', 'consolety'); ?></h4>
                            <textarea id="revision-feedback" placeholder="What changes are needed?"></textarea>
                            <button type="button" class="vp-btn vp-btn-outline" id="btn-revision"><?php esc_html_e('Request', 'consolety'); ?></button>
                        </div>

                        <div class="vp-action-card vp-action-reject">
                            <h4><span class="dashicons dashicons-no" style="color:#ef4444;margin-right:6px;"></span><?php esc_html_e('Reject', 'consolety'); ?></h4>
                            <textarea id="reject-reason" placeholder="Reason for rejection (minimum 50 characters)..." maxlength="2000"></textarea>
                            <div class="vp-char-counter"><span id="reject-char-count">0</span>/50 minimum</div>
                            <button type="button" class="vp-btn vp-btn-danger" id="btn-reject"><?php esc_html_e('Reject', 'consolety'); ?></button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php endif; ?>
</div>

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