<?php
namespace Bookaro\Controller\Admin;

defined( 'ABSPATH' ) || exit;

use function add_submenu_page;
use function current_user_can;
use function esc_html__;
use function esc_html_e;
use function esc_url;
use function wp_die;

/**
 * Renders the Lite vs Pro comparison page in wp-admin.
 */
class UpgradeController
{
    /**
     * Register hooks for the upgrade page.
     */
    public function register_hooks(): void
    {
        if ( function_exists( 'bookaro_is_pro_active' ) && bookaro_is_pro_active() ) {
            return;
        }
        add_action( 'admin_menu', [ $this, 'add_submenu_page' ], 25 );
    }

    /**
     * Add the submenu page under MyBookaro.
     */
    public function add_submenu_page(): void
    {
        $capability = function_exists( 'bookaro_get_admin_menu_capability' )
            ? bookaro_get_admin_menu_capability()
            : 'manage_options';
        add_submenu_page(
            'bookaro-bookings',
            __( 'Lite Vs Pro', 'mybookaro' ),
            __( 'Lite Vs Pro', 'mybookaro' ),
            $capability,
            'bookaro-upgrade',
            [ $this, 'render_page' ]
        );
    }

    /**
     * Render the comparison table and upgrade link.
     */
    public function render_page(): void
    {
        $can_manage_admin = function_exists( 'bookaro_current_user_can_manage_admin' )
            ? bookaro_current_user_can_manage_admin()
            : current_user_can( 'manage_options' );
        if ( ! $can_manage_admin ) {
            wp_die( esc_html__( 'You do not have permission to access this page.', 'mybookaro' ) );
        }

        $upgrade_url = function_exists( 'bookaro_get_upgrade_url' )
            ? (string) bookaro_get_upgrade_url()
            : '';

        $rows = [
            [
                'label' => __( 'Booking requests & approvals', 'mybookaro' ),
                'lite'  => __( 'Included: customers request bookings; providers approve, reject, reschedule, or cancel, with a guarded status flow.', 'mybookaro' ),
                'pro'   => __( 'Everything in Lite, plus:', 'mybookaro' ),
                'pro_points' => [
                    __( 'Payment-aware status flow, including paid and refunded states', 'mybookaro' ),
                    __( 'Per-provider booking chat for customer questions', 'mybookaro' ),
                ],
            ],
            [
                'label' => __( 'Availability & scheduling', 'mybookaro' ),
                'lite'  => __( 'Included: weekly hours, booking interval, capacity per slot, and date exceptions/closures.', 'mybookaro' ),
                'pro'   => __( 'Everything in Lite, plus:', 'mybookaro' ),
                'pro_points' => [
                    __( 'Two-way Google Calendar and iCal sync to reduce double-booking', 'mybookaro' ),
                ],
            ],
            [
                'label' => __( 'Dashboards & reminders', 'mybookaro' ),
                'lite'  => __( 'Included: provider and customer dashboards, onboarding checklists, and email reminders (24h/1h).', 'mybookaro' ),
                'pro'   => __( 'Everything in Lite, plus:', 'mybookaro' ),
                'pro_points' => [
                    __( 'SMS reminders and notifications alongside email', 'mybookaro' ),
                ],
            ],
            [
                'label' => __( 'Payments & payouts', 'mybookaro' ),
                'lite'  => __( 'Not included.', 'mybookaro' ),
                'pro'   => __( 'WooCommerce checkout and payout operations:', 'mybookaro' ),
                'pro_points' => [
                    __( 'Create WooCommerce orders for bookings — Pay now, Pay later, or let the customer choose', 'mybookaro' ),
                    __( 'Provider payout tracking with a percentage or fixed revenue split', 'mybookaro' ),
                    __( 'Cancellation fees with a configurable window and automatic refunds', 'mybookaro' ),
                    __( 'No-show fees with a configurable window and automatic refunds', 'mybookaro' ),
                    __( 'Order-to-booking status mapping (both directions) with a lockable booking product', 'mybookaro' ),
                ],
            ],
            [
                'label' => __( 'Calendar sync', 'mybookaro' ),
                'lite'  => __( 'Not included.', 'mybookaro' ),
                'pro'   => __( 'Calendar providers:', 'mybookaro' ),
                'pro_points' => [
                    __( 'Google Calendar via OAuth (connect plus two-way sync)', 'mybookaro' ),
                    __( 'iCal feed for read-only calendar apps', 'mybookaro' ),
                    __( 'Sync health diagnostics to surface failed syncs', 'mybookaro' ),
                ],
            ],
            [
                'label' => __( 'Messaging & chat', 'mybookaro' ),
                'lite'  => __( 'Not included.', 'mybookaro' ),
                'pro'   => __( 'SMS and booking chat:', 'mybookaro' ),
                'pro_points' => [
                    __( 'SMS via Twilio for new bookings, approvals, 24h/1h reminders, and review invites', 'mybookaro' ),
                    __( 'Per-provider routing; providers choose which templates they receive', 'mybookaro' ),
                    __( 'Customizable templates with tokens, delivery diagnostics, and content preview', 'mybookaro' ),
                    __( 'Booking chat: native MyListing messaging on Directory; MyBookaro chat on Generic Directory and Single Business', 'mybookaro' ),
                ],
            ],
            [
                'label' => __( 'Automation & webhooks', 'mybookaro' ),
                'lite'  => __( 'Not included.', 'mybookaro' ),
                'pro'   => __( 'Outbound webhooks:', 'mybookaro' ),
                'pro_points' => [
                    __( 'Events: booking.created, booking.updated, booking.cancelled, booking.paid', 'mybookaro' ),
                    __( 'Signed payloads with a shared secret', 'mybookaro' ),
                    __( 'Idempotency (dedupe identical events) and retries with backoff', 'mybookaro' ),
                    __( 'Per-provider endpoints and event selection, with delivery diagnostics', 'mybookaro' ),
                ],
            ],
            [
                'label' => __( 'Custom forms & profiles', 'mybookaro' ),
                'lite'  => __( 'Not included.', 'mybookaro' ),
                'pro'   => __( 'Custom data capture:', 'mybookaro' ),
                'pro_points' => [
                    __( 'Custom booking fields and profile-select fields', 'mybookaro' ),
                    __( 'Customer profiles, optionally scoped per listing', 'mybookaro' ),
                    __( 'Media and gallery fields with upload roles, file limits, and allowed file types', 'mybookaro' ),
                    __( 'Taxonomy-backed select options', 'mybookaro' ),
                    __( 'Shareable profile links with visibility controls', 'mybookaro' ),
                    __( 'Profile reminders on a recurring schedule', 'mybookaro' ),
                ],
            ],
            [
                'label' => __( 'Staff', 'mybookaro' ),
                'lite'  => __( 'Not included.', 'mybookaro' ),
                'pro'   => __( 'Provider staff:', 'mybookaro' ),
                'pro_points' => [
                    __( 'Provider-managed roster (add, edit, enable, disable, remove)', 'mybookaro' ),
                    __( 'Manual assignment from Bookings and Calendar', 'mybookaro' ),
                    __( 'Allowlist or denylist which providers can use staff', 'mybookaro' ),
                    __( 'No customer-selected staff and no automatic routing/dispatch', 'mybookaro' ),
                ],
            ],
            [
                'label' => __( 'Reporting & exports', 'mybookaro' ),
                'lite'  => __( 'Not included.', 'mybookaro' ),
                'pro'   => __( 'Analytics and CSV exports:', 'mybookaro' ),
                'pro_points' => [
                    __( 'Revenue, booking volume, and top-service charts by date range and granularity', 'mybookaro' ),
                    __( 'CSV exports for bookings, summaries, and service usage', 'mybookaro' ),
                    __( 'Admin and provider-scoped exports with audit logging', 'mybookaro' ),
                ],
            ],
        ];
        ?>
        <div class="wrap bookaro-admin bookaro-upgrade">
            <div class="bookaro-card bookaro-hero bookaro-upgrade-hero">
                <div class="bookaro-hero__content">
                    <div class="bookaro-hero__brand">
                        <img src="<?php echo esc_url( BOOKARO_PLUGIN_URL . 'assets/img/mb-logo.svg' ); ?>" alt="<?php esc_attr_e( 'MyBookaro', 'mybookaro' ); ?>" width="120" height="36" />
                    </div>
                    <h1><?php esc_html_e( 'Lite vs Pro', 'mybookaro' ); ?></h1>
                    <p class="description"><?php esc_html_e( 'Lite already covers booking requests, availability, dashboards, reminders, and diagnostics for MyListing Directory and Single Business, with guided Generic Directory support. Pro adds payments, Google Calendar + iCal sync, lane-scoped chat, Staff workflows, automation, and reporting.', 'mybookaro' ); ?></p>
                </div>
                <div class="bookaro-hero__actions">
                    <?php if ( $upgrade_url ) : ?>
                        <a class="button button-primary" href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener noreferrer">
                            <?php esc_html_e( 'Upgrade to MyBookaro Pro', 'mybookaro' ); ?>
                        </a>
                    <?php endif; ?>
                </div>
            </div>

            <div class="bookaro-card bookaro-compare">
                <div class="bookaro-card__header">
                    <h2><?php esc_html_e( 'Feature comparison', 'mybookaro' ); ?></h2>
                    <p class="description"><?php esc_html_e( 'Lite delivers a complete booking workflow for MyListing Directory, Single Business, and guided Generic Directory deployments. Pro adds payments, calendar sync, messaging, automation, Staff, and reporting on top of everything in Lite.', 'mybookaro' ); ?></p>
                </div>
                <table class="widefat striped bookaro-compare-table">
                    <thead>
                        <tr>
                            <th><?php esc_html_e( 'Outcome', 'mybookaro' ); ?></th>
                            <th><?php esc_html_e( 'Lite', 'mybookaro' ); ?></th>
                            <th><?php esc_html_e( 'Pro', 'mybookaro' ); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $rows as $row ) : ?>
                            <tr>
                                <td><strong><?php echo esc_html( $row['label'] ); ?></strong></td>
                                <td><?php echo esc_html( $row['lite'] ); ?></td>
                                <td>
                                    <?php echo esc_html( $row['pro'] ); ?>
                                    <?php if ( ! empty( $row['pro_points'] ) ) : ?>
                                        <ul class="bookaro-compare-points" style="margin:0.4em 0 0;padding-left:1.2em;list-style:disc;">
                                            <?php foreach ( $row['pro_points'] as $point ) : ?>
                                                <li><?php echo esc_html( $point ); ?></li>
                                            <?php endforeach; ?>
                                        </ul>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <div class="bookaro-card bookaro-upgrade-cta">
                <div class="bookaro-upgrade-cta__content">
                    <h2><?php esc_html_e( 'Everything in Lite, plus paid booking operations', 'mybookaro' ); ?></h2>
                    <p class="description"><?php esc_html_e( 'Pro layers payments and payout split, Google Calendar + iCal sync, SMS and booking chat, webhook automation, custom forms and profiles, Staff workflows, analytics, and exports on top of the Lite booking workflow.', 'mybookaro' ); ?></p>
                    <?php if ( ! $upgrade_url ) : ?>
                        <p class="description"><?php esc_html_e( 'Upgrade link is currently unavailable.', 'mybookaro' ); ?></p>
                    <?php endif; ?>
                </div>
            </div>

            <p class="description bookaro-footer-note">
                <?php esc_html_e( 'MyListing is a third-party product and trademark of its respective owner. MyBookaro is an independent plugin and is not affiliated with, endorsed by, or sponsored by 27collective.', 'mybookaro' ); ?>
            </p>
        </div>
        <?php
    }
}
