<?php

namespace Rtc\PlantShopWC\Admin\Views\Templates;

use Rtc\PlantShopWC\Admin\Views\GeneralSettingsView;
use Rtc\PlantShopWC\Admin\Views\Partials\ProCta;
class SettingsPage extends Template {
    public static function render_settings_page() : void {
        // Determine the active tab (read-only UI state).
        // Show premium tabs in Free too (they will render CTA stubs there).
        $allowed_tabs = array(
            'settings',
            'company',
            'print',
            'labels'
        );
        // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended -- Reading a benign UI parameter ("tab") that does not perform actions, change capabilities, or write data.
        $tab_raw = 'settings';
        // phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
        if ( isset( $_GET['tab'] ) ) {
            $maybe_tab = sanitize_text_field( wp_unslash( $_GET['tab'] ) );
            if ( $maybe_tab !== '' ) {
                $tab_raw = $maybe_tab;
            }
        }
        // phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
        // Sanitize to a key and whitelist.
        $tab_sanitized = sanitize_key( $tab_raw );
        $active_tab = ( in_array( $tab_sanitized, $allowed_tabs, true ) ? $tab_sanitized : 'settings' );
        ?>

        <div id="plantshop-wc-page"
            class="flex flex-col"
            x-data="
            <?php 
        echo esc_attr( wp_json_encode( array(
            'tab' => $active_tab,
        ) ) );
        ?>
            "
            x-effect="window.plantshop_wc_effect($data)">
            <nav class="relative flex justify-between gap-[20px] px-[20px] bg-white border-b border-[#c3c4c7]">
                <div class="flex items-center gap-[20px]">
                    <div class="flex items-center">
                        <img class="h-[48px] mr-[10px]" src="
                        <?php 
        echo esc_url( PLANTSHOP_WC_LOGO_URL );
        ?>
                        " alt="Logo"/>
                        <h1 class="!text-[20px]">
                        <?php 
        esc_html_e( 'PlantShop for WooCommerce', 'plantshop-wc' );
        ?>
                        </h1>
                    </div>
                </div>
                <div class="warp flex items-center gap-[20px] *:gap-[20px] *:flex *:text-[16px]">
                    <div class="*:focus:!shadow-none">
                        <a class="py-[25px] !text-[#b3b4cb] border-transparent border-b-[2px] hover:text-[#000000ad] hover:border-[#00000054]" :class="tab=='settings' ? '!text-black !border-[#307F57]' : ''" href="#" @click.prevent="tab='settings'">
                        <?php 
        esc_html_e( 'Settings', 'plantshop-wc' );
        ?>
                        </a>
                        <a class="py-[25px] !text-[#b3b4cb] border-transparent border-b-[2px] hover:text-[#000000ad] hover:border-[#00000054]" :class="tab=='company' ? '!text-black !border-[#307F57]' : ''" href="#" @click.prevent="tab='company'">
                            <?php 
        esc_html_e( 'Company Data', 'plantshop-wc' );
        ?>
                        </a>
                        <a class="py-[25px] !text-[#b3b4cb] border-transparent border-b-[2px] hover:text-[#000000ad] hover:border-[#00000054]" :class="tab=='print' ? '!text-black !border-[#307F57]' : ''" href="#" @click.prevent="tab='print'">
                            <?php 
        esc_html_e( 'Label printing', 'plantshop-wc' );
        ?>
                        </a>
                        <a class="py-[25px] !text-[#b3b4cb] border-transparent border-b-[2px] hover:text-[#000000ad] hover:border-[#00000054]" :class="tab=='labels' ? '!text-black !border-[#307F57]' : ''" href="#" @click.prevent="tab='labels'">
                            <?php 
        esc_html_e( 'Labels', 'plantshop-wc' );
        ?>
                        </a>
                    </div>
                    <div class="nav-tab-wrapper !border-transparent !border-b-[2px] !p-0 *:border-none *:p-0">
                        <a class="nav-tab fs" href="#">
                            &nbsp;
                        </a>
                    </div>
                </div>
            </nav>
            <div class="plantshop-wc-content *:px-[20px]">
                <?php 
        self::settings();
        if ( \plantshop_wc_fs()->can_use_premium_code() ) {
            // Pro build: render full premium tabs (methods may be stripped in Free).
            self::company_data__premium_only();
            self::label_print__premium_only();
            self::label_list__premium_only();
        } else {
            // Free build: render CTA stubs for premium tabs.
            self::company_data_stub();
            self::label_print_stub();
            self::label_list_stub();
        }
        ?>
            </div>
        </div>

        <?php 
    }

    private static function settings() : void {
        ?>
        <div x-cloak x-show="tab=='settings'" class="hide-company-data">
            <form action="options.php" method="post">
                <?php 
        settings_fields( 'plantshop-wc_group' );
        do_settings_sections( 'plantshop-wc' );
        submit_button();
        ?>
            </form>
        </div>
        <?php 
    }

    /**
     * Free build stub: Company Data tab CTA
     * Renders only in Free builds (Pro shows premium-only method).
     */
    private static function company_data_stub() : void {
        ?>
        <div x-cloak x-show="tab=='company'" class="only-company-data">
            <?php 
        ProCta::render( array(
            'variant'      => 'card',
            'feature_key'  => 'company_data',
            'title'        => __( 'Company data management', 'plantshop-wc' ),
            'description'  => __( 'Store your company information once and use it across labels and documents.', 'plantshop-wc' ),
            'bullets'      => array(__( 'Centralize address and contact details', 'plantshop-wc' ), __( 'Auto-fill Pro label templates and Plant Passport fields', 'plantshop-wc' ), __( 'Keep branding consistent across prints', 'plantshop-wc' )),
            'button_label' => __( 'Get Pro', 'plantshop-wc' ),
            'icon'         => '🏷️',
            'footnote'     => __( 'Cancel anytime • 14‑day money‑back guarantee', 'plantshop-wc' ),
        ) );
        ?>
        </div>
        <?php 
    }

    /**
     * Free build stub: Label Printing tab CTA
     */
    private static function label_print_stub() : void {
        ?>
        <div x-cloak x-show="tab=='print'">
            <?php 
        ProCta::render( array(
            'variant'      => 'card',
            'feature_key'  => 'label_print',
            'title'        => __( 'Print product labels', 'plantshop-wc' ),
            'description'  => __( 'Generate and batch print QR-enabled labels directly from your products.', 'plantshop-wc' ),
            'bullets'      => array(__( 'Batch print for many products at once', 'plantshop-wc' ), __( 'Print QR codes, prices, and SKU when your template includes these fields', 'plantshop-wc' ), __( 'Customizable layout & branding', 'plantshop-wc' )),
            'button_label' => __( 'Get Pro', 'plantshop-wc' ),
            'icon'         => '🖨️',
            'footnote'     => __( 'Works with WooCommerce products out of the box', 'plantshop-wc' ),
        ) );
        ?>
        </div>
        <?php 
    }

    /**
     * Free build stub: Labels tab CTA
     */
    private static function label_list_stub() : void {
        ?>
        <div class="plantshop-wc-label-list" x-cloak x-show="tab=='labels'">
            <?php 
        ProCta::render( array(
            'variant'      => 'card',
            'feature_key'  => 'label_list',
            'title'        => __( 'Manage label templates', 'plantshop-wc' ),
            'description'  => __( 'Create, customize, and reuse label templates to save time.', 'plantshop-wc' ),
            'bullets'      => array(__( 'Create multiple templates', 'plantshop-wc' ), __( 'Drag-and-drop fields: name, price, QR', 'plantshop-wc' ), __( 'Export & import templates', 'plantshop-wc' )),
            'button_label' => __( 'Get Pro', 'plantshop-wc' ),
            'icon'         => '✨',
        ) );
        ?>
        </div>
        <?php 
    }

}
