<?php

namespace auctionforge\inc\controllers;

if (!defined('ABSPATH')) {
    die('You shouldnt be here');
}

class ShortcodesPage
{
    public function init()
    {
        add_action('admin_menu', array($this, 'register_admin_page'));
    }

    public function register_admin_page()
    {
        add_submenu_page(
            'edit.php?post_type=lot',
            __('Shortcodes', 'auctionforge'),
            __('Shortcodes', 'auctionforge'),
            'edit_posts',
            'auctionforge-shortcodes',
            array($this, 'render_page')
        );
    }

    public function render_page()
    {
        $shortcodes = array(
            array(
                'shortcode'   => '[bp_cooldown_sale]',
                'name'        => 'Upcoming Auctions (Card Layout)',
                'description' => 'Displays upcoming auctions in a compact card grid layout with countdown timers. Includes both Bidspirit and manually added auctions, sorted by date. Supports category filtering by auction number prefix.',
                'options'     => array(
                    array('attr' => 'title', 'default' => '(empty)', 'desc' => 'Optional heading displayed above the auction grid.'),
                    array('attr' => 'category', 'default' => '(empty)', 'desc' => 'Single letter to filter by auction category (e.g. "R" for Real Estate). Only auctions whose number starts with this letter will be shown.'),
                    array('attr' => 'exclude_category', 'default' => '(empty)', 'desc' => 'Comma-separated letters of categories to exclude (e.g. "R" or "R,S"). Auctions whose number starts with any listed letter will be hidden.'),
                ),
                'examples'    => array(
                    '[bp_cooldown_sale]',
                    '[bp_cooldown_sale title="Upcoming Auctions"]',
                    '[bp_cooldown_sale category="R" title="Real Estate Auctions"]',
                    '[bp_cooldown_sale exclude_category="R" title="All Except Real Estate"]',
                    '[bp_cooldown_sale exclude_category="R,S" title="Filtered Auctions"]',
                ),
            ),
            array(
                'shortcode'   => '[bp_upcoming_wide]',
                'name'        => 'Upcoming Auctions (Wide Layout)',
                'description' => 'Displays upcoming auctions in a wide, horizontal layout with image on the left and details on the right. Includes countdown timers, full descriptions, and action buttons. Includes both Bidspirit and manually added auctions, sorted by date. Fully self-contained CSS.',
                'options'     => array(
                    array('attr' => 'title', 'default' => '(empty)', 'desc' => 'Optional heading displayed above the auction list with a colored underline.'),
                    array('attr' => 'register_url', 'default' => '(empty)', 'desc' => 'Optional URL for a "Register" button shown alongside "View Catalog" / "View Auction". If empty, only the catalog/auction button is shown (full width).'),
                ),
                'examples'    => array(
                    '[bp_upcoming_wide title="Upcoming Auctions"]',
                    '[bp_upcoming_wide title="Upcoming Auctions" register_url="https://example.com/register"]',
                ),
            ),
            array(
                'shortcode'   => '[bp_past_sales]',
                'name'        => 'Past Sales / Archived Auctions',
                'description' => 'Displays a list of past/archived auctions with their results.',
                'options'     => array(
                    array('attr' => 'title', 'default' => '(empty)', 'desc' => 'Optional heading displayed above the list.'),
                    array('attr' => 'limit', 'default' => '10', 'desc' => 'Maximum number of past auctions to display.'),
                ),
                'examples'    => array(
                    '[bp_past_sales]',
                    '[bp_past_sales title="Past Sales" limit="5"]',
                ),
            ),
            array(
                'shortcode'   => '[bp_login_form]',
                'name'        => 'Bidspirit Login Form',
                'description' => 'Renders a login form for Bidspirit user authentication.',
                'options'     => array(),
                'examples'    => array('[bp_login_form]'),
            ),
            array(
                'shortcode'   => '[bp_login_buttons]',
                'name'        => 'Bidspirit Login Buttons',
                'description' => 'Displays login/logout action buttons for Bidspirit users.',
                'options'     => array(),
                'examples'    => array('[bp_login_buttons]'),
            ),
            array(
                'shortcode'   => '[bp_register_form]',
                'name'        => 'Bidspirit Registration Form',
                'description' => 'Renders a registration form for new Bidspirit users.',
                'options'     => array(),
                'examples'    => array('[bp_register_form]'),
            ),
            array(
                'shortcode'   => '[bp_profile_buttons]',
                'name'        => 'Profile Buttons',
                'description' => 'Displays profile-related action buttons for logged-in Bidspirit users.',
                'options'     => array(),
                'examples'    => array('[bp_profile_buttons]'),
            ),
            array(
                'shortcode'   => '[bp_submission_form]',
                'name'        => 'Item Submission Form',
                'description' => 'Renders a form that allows users to submit items for auction consideration. Includes fields for item details, images, and contact information.',
                'options'     => array(),
                'examples'    => array('[bp_submission_form]'),
            ),
            array(
                'shortcode'   => '[bp_lot_list]',
                'name'        => 'Lot List',
                'description' => 'Displays a list of lots (items) from auctions. Supports multiple display modes and templates including a slider view for featured items.',
                'options'     => array(
                    array('attr' => 'title', 'default' => 'Lot List', 'desc' => 'Heading displayed above the lot list.'),
                    array('attr' => 'limit', 'default' => '5', 'desc' => 'Number of lots to display. Use -1 to show all available lots.'),
                    array('attr' => 'mode', 'default' => 'default', 'desc' => 'Which lots to display: "default" (featured/most recent), "archive" (from past auctions), "upcoming" (from upcoming auctions), "random_upcoming" (random lots from active auctions), "random_past" (random sold lots from past auctions, soldBid > 0), "high_value_past" (past lots ordered by estimate, highest first), or "high_value_sold" (past sold lots ordered by sold price, highest first).'),
                    array('attr' => 'tmp', 'default' => 'list', 'desc' => 'Display template: "list" for a standard grid/list view, or "slider" for a horizontal carousel.'),
                    array('attr' => 'loadmore', 'default' => '1', 'desc' => 'Show a "Load More" button: 1 to show, 0 to hide.'),
                    array('attr' => 'mobile_cols', 'default' => '2', 'desc' => 'Number of lot columns on mobile: 1 for single-item carousel with swipe arrows, 2 for default two-column grid.'),
                ),
                'examples'    => array(
                    '[bp_lot_list]',
                    '[bp_lot_list title="Featured Lots" tmp="slider" limit="-1"]',
                    '[bp_lot_list title="The most popular in archive" tmp="slider" limit="-1"]',
                    '[bp_lot_list title="Upcoming Lots" mode="upcoming" limit="10"]',
                    '[bp_lot_list title="Past Lots" mode="archive" limit="8" loadmore="0"]',
                    '[bp_lot_list title="Highlighted Items" mobile_cols="1" limit="-1"]',
                    '[bp_lot_list title="Featured Picks" mode="random_upcoming" tmp="slider" limit="8"]',
                    '[bp_lot_list title="Recent Sales" mode="random_past" tmp="slider" limit="12"]',
                    '[bp_lot_list title="High Value Lots" mode="high_value_past" tmp="slider" limit="8"]',
                    '[bp_lot_list title="Top Sales" mode="high_value_sold" tmp="slider" limit="8"]',
                ),
            ),
            array(
                'shortcode'   => '[bp_categories_filter]',
                'name'        => 'Auction Categories Filter',
                'description' => 'Displays a filter/navigation bar of auction categories, allowing visitors to filter lots by category.',
                'options'     => array(),
                'examples'    => array('[bp_categories_filter]'),
            ),
            array(
                'shortcode'   => '[bp_auction_list]',
                'name'        => 'Auction List (Filterable)',
                'description' => 'Displays upcoming or past auctions with optional category filtering. Supports both card and wide layouts. Categories are defined in Settings > Auction Categories. For Bidspirit auctions, the first letter of the Auction Number is matched against the category letter. For manual auctions, the category is selected in the auction editor.',
                'options'     => array(
                    array('attr' => 'type', 'default' => 'upcoming', 'desc' => 'Which auctions to display: "upcoming" or "past".'),
                    array('attr' => 'layout', 'default' => 'card', 'desc' => 'Display layout: "card" (compact grid) or "wide" (horizontal layout with image left, details right).'),
                    array('attr' => 'category', 'default' => '(empty)', 'desc' => 'Single letter to filter by auction category (e.g. "R" for Real Estate). Must match a category defined in Settings.'),
                    array('attr' => 'title', 'default' => '(empty)', 'desc' => 'Optional heading displayed above the auction list.'),
                    array('attr' => 'limit', 'default' => '0 (all)', 'desc' => 'Maximum number of auctions to display. 0 = unlimited.'),
                    array('attr' => 'register_url', 'default' => '(empty)', 'desc' => 'Optional registration URL. Only used with wide layout.'),
                ),
                'examples'    => array(
                    '[bp_auction_list]',
                    '[bp_auction_list type="upcoming" layout="wide" title="Upcoming Auctions"]',
                    '[bp_auction_list type="past" layout="card" limit="6"]',
                    '[bp_auction_list type="upcoming" category="R" title="Real Estate Auctions"]',
                    '[bp_auction_list type="past" category="R" layout="wide" title="Past Real Estate Sales"]',
                ),
            ),
            array(
                'shortcode'   => '[bp_lots_by_category category=""]',
                'name'        => 'Lots by Category',
                'description' => 'Displays lots from all auctions that belong to a specific Bidspirit category. The category is matched by name (case-insensitive) against the auction tags imported from Bidspirit. Use this to create dedicated pages or sections for a single category across all auctions.',
                'options'     => array(
                    array('attr' => 'category', 'default' => '(required)', 'desc' => 'The category name to filter by (e.g. "Paintings"). Matched case-insensitively against Bidspirit auction tags.'),
                    array('attr' => 'title', 'default' => '(empty)', 'desc' => 'Optional heading displayed above the lot list.'),
                    array('attr' => 'tmp', 'default' => 'list', 'desc' => 'Display template: "list" for a standard grid view (default), or "slider" for a horizontal carousel.'),
                    array('attr' => 'limit', 'default' => '20', 'desc' => 'Maximum number of lots to display. Use -1 to show all matching lots.'),
                    array('attr' => 'loadmore', 'default' => '1', 'desc' => 'Show a "Load More" button: 1 to show, 0 to hide.'),
                    array('attr' => 'mobile_cols', 'default' => '2', 'desc' => 'Number of lot columns on mobile: 1 for single-item carousel with swipe arrows, 2 for default two-column grid.'),
                ),
                'examples'    => array(
                    '[bp_lots_by_category category="Paintings"]',
                    '[bp_lots_by_category category="Sculptures" title="All Sculptures" limit="-1"]',
                    '[bp_lots_by_category category="Furniture" title="Furniture Collection" limit="12" loadmore="0"]',
                    '[bp_lots_by_category category="Jewelry" mobile_cols="1"]',
                    '[bp_lots_by_category category="Paintings" tmp="slider" title="Paintings Carousel" limit="-1"]',
                ),
            ),
            array(
                'shortcode'   => '[bp_popular_lots]',
                'name'        => 'Most Popular Lots',
                'description' => 'Displays the most viewed lots as a slider or grid. Uses the same lot card templates as other shortcodes with live bid data and prices. Requires "Enable Lot Analytics" to be turned on in AuctionForge Settings.',
                'options'     => array(
                    array('attr' => 'title', 'default' => 'Most Popular Lots', 'desc' => 'Heading above the lots.'),
                    array('attr' => 'limit', 'default' => '8', 'desc' => 'Max lots to display.'),
                    array('attr' => 'auction_id', 'default' => '(empty)', 'desc' => 'Filter by auction term_id. Empty = all auctions.'),
                    array('attr' => 'tmp', 'default' => 'slider', 'desc' => 'Display template: "slider" for Swiper carousel, "grid" for grid layout.'),
                    array('attr' => 'mobile_cols', 'default' => '2', 'desc' => 'Number of columns on mobile (slider mode). Set to 1 for single-slide with arrows.'),
                    array('attr' => 'period', 'default' => '(empty)', 'desc' => 'Time period filter, e.g. "7 days", "30 days", "1 year". Empty = all time.'),
                    array('attr' => 'loadmore', 'default' => '0', 'desc' => 'Show "Load more" button in grid mode. 1 = yes, 0 = no.'),
                ),
                'examples'    => array(
                    '[bp_popular_lots]',
                    '[bp_popular_lots tmp="slider" limit="12"]',
                    '[bp_popular_lots tmp="grid" limit="8" loadmore="1"]',
                    '[bp_popular_lots tmp="slider" mobile_cols="1"]',
                    '[bp_popular_lots auction_id="92" title="Top Lots This Auction"]',
                    '[bp_popular_lots period="30 days" title="Popular This Month"]',
                ),
            ),
            array(
                'shortcode'   => '[bp_most_favorited]',
                'name'        => 'Most Favorited Lots',
                'description' => 'Displays lots with the most favorites (hearts) as a slider or grid. Uses the same lot card templates as other shortcodes. Requires "Enable Lot Analytics" to be turned on in AuctionForge Settings.',
                'options'     => array(
                    array('attr' => 'title', 'default' => 'Most Favorited Lots', 'desc' => 'Heading above the lots.'),
                    array('attr' => 'limit', 'default' => '8', 'desc' => 'Max lots to display.'),
                    array('attr' => 'auction_id', 'default' => '(empty)', 'desc' => 'Filter by auction term_id. Empty = all auctions.'),
                    array('attr' => 'tmp', 'default' => 'slider', 'desc' => 'Display template: "slider" for Swiper carousel, "grid" for grid layout.'),
                    array('attr' => 'mobile_cols', 'default' => '2', 'desc' => 'Number of columns on mobile (slider mode). Set to 1 for single-slide with arrows.'),
                    array('attr' => 'loadmore', 'default' => '0', 'desc' => 'Show "Load more" button in grid mode. 1 = yes, 0 = no.'),
                ),
                'examples'    => array(
                    '[bp_most_favorited]',
                    '[bp_most_favorited tmp="slider" limit="6"]',
                    '[bp_most_favorited tmp="grid" limit="12" loadmore="1"]',
                    '[bp_most_favorited auction_id="92" title="Fan Favorites"]',
                ),
            ),
            array(
                'shortcode'   => '[bp_trending_lots]',
                'name'        => 'Trending Lots',
                'description' => 'Displays lots with the most views in the last N days as a slider or grid. Uses the same lot card templates as other shortcodes. Requires "Enable Lot Analytics" to be turned on in AuctionForge Settings.',
                'options'     => array(
                    array('attr' => 'title', 'default' => 'Trending Lots', 'desc' => 'Heading above the lots.'),
                    array('attr' => 'limit', 'default' => '8', 'desc' => 'Max lots to display.'),
                    array('attr' => 'days', 'default' => '7', 'desc' => 'Number of days to look back.'),
                    array('attr' => 'tmp', 'default' => 'slider', 'desc' => 'Display template: "slider" for Swiper carousel, "grid" for grid layout.'),
                    array('attr' => 'mobile_cols', 'default' => '2', 'desc' => 'Number of columns on mobile (slider mode). Set to 1 for single-slide with arrows.'),
                    array('attr' => 'loadmore', 'default' => '0', 'desc' => 'Show "Load more" button in grid mode. 1 = yes, 0 = no.'),
                ),
                'examples'    => array(
                    '[bp_trending_lots]',
                    '[bp_trending_lots tmp="slider" days="14" limit="12"]',
                    '[bp_trending_lots tmp="grid" days="3" title="Hot Right Now"]',
                    '[bp_trending_lots tmp="slider" mobile_cols="1"]',
                ),
            ),
        );
        ?>
        <style>
            .auctionforge-sc-wrap { max-width: 900px; margin: 20px auto 40px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
            .auctionforge-sc-wrap h1 { font-size: 24px; font-weight: 600; margin-bottom: 5px; }
            .auctionforge-sc-wrap .auctionforge-sc-subtitle { color: #666; margin-bottom: 30px; font-size: 14px; }
            .auctionforge-sc-card { background: #fff; border: 1px solid #ddd; border-radius: 6px; padding: 24px 28px; margin-bottom: 20px; }
            .auctionforge-sc-card-header { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
            .auctionforge-sc-card-header h2 { font-size: 16px; font-weight: 600; margin: 0; }
            .auctionforge-sc-code-wrap { display: inline-flex; align-items: center; gap: 8px; background: #f0f0f1; border: 1px solid #ddd; border-radius: 4px; padding: 6px 12px; font-family: Consolas, Monaco, monospace; font-size: 13px; cursor: pointer; transition: background 0.15s; }
            .auctionforge-sc-code-wrap:hover { background: #e2e2e3; }
            .auctionforge-sc-code-wrap .dashicons { font-size: 16px; width: 16px; height: 16px; color: #666; }
            .auctionforge-sc-code-wrap.copied { background: #d4edda; border-color: #a3d9a5; }
            .auctionforge-sc-code-wrap.copied .dashicons { color: #28a745; }
            .auctionforge-sc-desc { color: #444; font-size: 13px; line-height: 1.6; margin-bottom: 16px; }
            .auctionforge-sc-options { margin-bottom: 16px; }
            .auctionforge-sc-options h3 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: #555; margin: 0 0 8px; }
            .auctionforge-sc-opt-table { width: 100%; border-collapse: collapse; font-size: 13px; }
            .auctionforge-sc-opt-table th { text-align: left; padding: 6px 10px; background: #f7f7f7; border: 1px solid #e5e5e5; font-weight: 600; color: #333; }
            .auctionforge-sc-opt-table td { padding: 6px 10px; border: 1px solid #e5e5e5; color: #444; }
            .auctionforge-sc-opt-table code { background: #f0f0f1; padding: 1px 5px; border-radius: 3px; font-size: 12px; }
            .auctionforge-sc-examples h3 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: #555; margin: 0 0 8px; }
            .auctionforge-sc-example { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
            .auctionforge-sc-example-code { display: inline-flex; align-items: center; gap: 6px; background: #f7f7f7; border: 1px solid #e0e0e0; border-radius: 3px; padding: 5px 10px; font-family: Consolas, Monaco, monospace; font-size: 12px; cursor: pointer; transition: background 0.15s; flex: 1; }
            .auctionforge-sc-example-code:hover { background: #eee; }
            .auctionforge-sc-example-code .dashicons { font-size: 14px; width: 14px; height: 14px; color: #888; flex-shrink: 0; }
            .auctionforge-sc-example-code.copied { background: #d4edda; border-color: #a3d9a5; }
            .auctionforge-sc-no-options { font-size: 13px; color: #888; font-style: italic; }
        </style>

        <div class="wrap auctionforge-sc-wrap">
            <h1><?php _e('Available Shortcodes', 'auctionforge'); ?></h1>
            <p class="auctionforge-sc-subtitle"><?php _e('Click any shortcode or example to copy it to your clipboard.', 'auctionforge'); ?></p>

            <?php foreach ($shortcodes as $sc) : ?>
                <div class="auctionforge-sc-card">
                    <div class="auctionforge-sc-card-header">
                        <h2><?php echo esc_html($sc['name']); ?></h2>
                    </div>

                    <div style="margin-bottom: 12px;">
                        <span class="auctionforge-sc-code-wrap" onclick="auctionforgeScCopy(this)" data-code="<?php echo esc_attr($sc['shortcode']); ?>">
                            <span class="dashicons dashicons-clipboard"></span>
                            <code><?php echo esc_html($sc['shortcode']); ?></code>
                        </span>
                    </div>

                    <p class="auctionforge-sc-desc"><?php echo esc_html($sc['description']); ?></p>

                    <?php if (!empty($sc['options'])) : ?>
                        <div class="auctionforge-sc-options">
                            <h3><?php _e('Options', 'auctionforge'); ?></h3>
                            <table class="auctionforge-sc-opt-table">
                                <thead>
                                    <tr>
                                        <th style="width:120px;"><?php _e('Attribute', 'auctionforge'); ?></th>
                                        <th style="width:100px;"><?php _e('Default', 'auctionforge'); ?></th>
                                        <th><?php _e('Description', 'auctionforge'); ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($sc['options'] as $opt) : ?>
                                        <tr>
                                            <td><code><?php echo esc_html($opt['attr']); ?></code></td>
                                            <td><?php echo esc_html($opt['default']); ?></td>
                                            <td><?php echo esc_html($opt['desc']); ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php else : ?>
                        <p class="auctionforge-sc-no-options"><?php _e('No configurable options.', 'auctionforge'); ?></p>
                    <?php endif; ?>

                    <?php if (!empty($sc['examples']) && count($sc['examples']) > 1) : ?>
                        <div class="auctionforge-sc-examples">
                            <h3><?php _e('Examples', 'auctionforge'); ?></h3>
                            <?php foreach ($sc['examples'] as $ex) : ?>
                                <div class="auctionforge-sc-example">
                                    <span class="auctionforge-sc-example-code" onclick="auctionforgeScCopy(this)" data-code="<?php echo esc_attr($ex); ?>">
                                        <span class="dashicons dashicons-clipboard"></span>
                                        <code><?php echo esc_html($ex); ?></code>
                                    </span>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endforeach; ?>
        </div>

        <script>
        function auctionforgeScCopy(el) {
            var code = el.getAttribute('data-code');
            if (navigator.clipboard && navigator.clipboard.writeText) {
                navigator.clipboard.writeText(code).then(function() {
                    el.classList.add('copied');
                    setTimeout(function() { el.classList.remove('copied'); }, 1500);
                });
            } else {
                var ta = document.createElement('textarea');
                ta.value = code;
                ta.style.position = 'fixed';
                ta.style.left = '-9999px';
                document.body.appendChild(ta);
                ta.select();
                document.execCommand('copy');
                document.body.removeChild(ta);
                el.classList.add('copied');
                setTimeout(function() { el.classList.remove('copied'); }, 1500);
            }
        }
        </script>
        <?php
    }
}
