<?php
/**
 * Flavor – Restaurant Menu
 *
 * Upsell notices for Pro features.
 * Registers meta boxes and admin pages that point to Flavor Pro.
 * No Pro functionality is included here — this file only shows
 * informational notices with links to the separate Pro plugin.
 *
 * @package    FlavorMenu
 * @copyright  Copyright (c) 2024 Flavor Studio
 * @license    GPL-2.0-or-later
 * @link       https://flavor-menu.com
 */

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

class Flavor_Upsell {

    const PRO_URL = 'https://flavor-menu.com/#pricing';

    public static function init() {
        add_action( 'add_meta_boxes', array( __CLASS__, 'register_meta_boxes' ) );
        add_action( 'admin_menu',     array( __CLASS__, 'register_pages' ) );
    }

    // -------------------------------------------------------------------------
    // Meta boxes on dish edit screen
    // -------------------------------------------------------------------------

    public static function register_meta_boxes() {
        add_meta_box(
            'flavor_allergens_upsell',
            __( 'Allergens & Nutrition', 'flavor-restaurant-menu' ),
            array( __CLASS__, 'render_allergens_metabox' ),
            'flavor_menu_item',
            'normal',
            'default'
        );
    }

    public static function render_allergens_metabox() {
        self::metabox_notice(
            __( 'Allergens & Nutrition', 'flavor-restaurant-menu' ),
            __( 'Display EU-mandatory allergen information and nutritional values directly on your menu cards. Required under EU Food Information Regulation 1169/2011.', 'flavor-restaurant-menu' ),
            array(
                __( '14 EU mandatory allergens (gluten, milk, nuts …)', 'flavor-restaurant-menu' ),
                __( 'Nutritional values per 100 g (calories, fat, carbs, protein)', 'flavor-restaurant-menu' ),
                __( 'Layout-specific display: card flip, popup or chips', 'flavor-restaurant-menu' ),
            )
        );
    }

    // -------------------------------------------------------------------------
    // Pro admin pages (informational upsell only — no Pro code)
    // -------------------------------------------------------------------------

    public static function register_pages() {
        $pages = array(
            array(
                'title'  => __( 'Badges', 'flavor-restaurant-menu' ),
                'slug'   => 'flavor-badges',
                'cb'     => array( __CLASS__, 'page_badges' ),
            ),
            array(
                'title'  => __( 'Custom Themes', 'flavor-restaurant-menu' ),
                'slug'   => 'flavor-theme-settings',
                'cb'     => array( __CLASS__, 'page_themes' ),
            ),
            array(
                'title'  => __( 'Typography', 'flavor-restaurant-menu' ),
                'slug'   => 'flavor-typography',
                'cb'     => array( __CLASS__, 'page_typography' ),
            ),
            array(
                'title'  => __( 'Guest Ratings', 'flavor-restaurant-menu' ),
                'slug'   => 'flavor-ratings',
                'cb'     => array( __CLASS__, 'page_ratings' ),
            ),
            array(
                'title'  => __( 'Table Stand / QR Code', 'flavor-restaurant-menu' ),
                'slug'   => 'flavor-qr-code',
                'cb'     => array( __CLASS__, 'page_qr' ),
            ),
            array(
                'title'  => __( 'Import / Export', 'flavor-restaurant-menu' ),
                'slug'   => 'flavor-csv',
                'cb'     => array( __CLASS__, 'page_csv' ),
            ),
        );

        foreach ( $pages as $p ) {
            add_submenu_page(
                'edit.php?post_type=flavor_menu_item',
                $p['title'],
                $p['title'],
                'manage_options',
                $p['slug'],
                $p['cb']
            );
        }
    }

    public static function page_badges() {
        self::pro_page(
            __( 'Badges', 'flavor-restaurant-menu' ),
            __( 'Highlight your best dishes with eye-catching labels directly on the menu cards.', 'flavor-restaurant-menu' ),
            array(
                __( 'Create custom badges: New, Popular, Chef\'s Choice, Seasonal, Spicy and more', 'flavor-restaurant-menu' ),
                __( 'Assign multiple badges per dish', 'flavor-restaurant-menu' ),
                __( 'Shown in all layouts', 'flavor-restaurant-menu' ),
            )
        );
    }

    public static function page_themes() {
        self::pro_page(
            __( 'Custom Themes', 'flavor-restaurant-menu' ),
            __( 'Create fully custom color themes that match your restaurant\'s brand — no CSS knowledge needed.', 'flavor-restaurant-menu' ),
            array(
                __( '3 custom theme slots with 7 color pickers each', 'flavor-restaurant-menu' ),
                __( 'Additional built-in themes: Clean Light, Warm Earth', 'flavor-restaurant-menu' ),
                __( 'Live preview in the admin', 'flavor-restaurant-menu' ),
            )
        );
    }

    public static function page_typography() {
        self::pro_page(
            __( 'Typography', 'flavor-restaurant-menu' ),
            __( 'Choose from 40 curated Google Fonts for your menu headings and body text — with optional local self-hosting for GDPR compliance.', 'flavor-restaurant-menu' ),
            array(
                __( '40 curated fonts: Serif, Sans-Serif and Display groups', 'flavor-restaurant-menu' ),
                __( '"Download Fonts Locally" — serves fonts from your own server, no Google request', 'flavor-restaurant-menu' ),
                __( 'Option to disable Google Fonts entirely (system font fallbacks)', 'flavor-restaurant-menu' ),
            )
        );
    }

    public static function page_ratings() {
        self::pro_page(
            __( 'Guest Ratings', 'flavor-restaurant-menu' ),
            __( 'Let your guests rate dishes directly in the menu — no account required.', 'flavor-restaurant-menu' ),
            array(
                __( '1–5 star ratings displayed in the Card Grid layout', 'flavor-restaurant-menu' ),
                __( 'Duplicate protection via localStorage and server-side IP hash', 'flavor-restaurant-menu' ),
                __( 'Enable / disable per dish or globally', 'flavor-restaurant-menu' ),
            )
        );
    }

    public static function page_qr() {
        self::pro_page(
            __( 'Table Stand / QR Code', 'flavor-restaurant-menu' ),
            __( 'Generate print-ready table stand PDFs with a QR code pointing to your digital menu.', 'flavor-restaurant-menu' ),
            array(
                __( '3 styles: Minimal, Elegant, Branded', 'flavor-restaurant-menu' ),
                __( 'DIN A5 and A6 paper sizes', 'flavor-restaurant-menu' ),
                __( 'Upload logo and background image, 4 color pickers, live preview, PDF export', 'flavor-restaurant-menu' ),
            )
        );
    }

    public static function page_csv() {
        self::pro_page(
            __( 'Import / Export', 'flavor-restaurant-menu' ),
            __( 'Import your entire menu from a CSV file (Excel or Google Sheets) or export all dishes at once.', 'flavor-restaurant-menu' ),
            array(
                __( 'Import all 18 dish fields including subcategories, allergens and nutritional values', 'flavor-restaurant-menu' ),
                __( 'Missing categories are created automatically', 'flavor-restaurant-menu' ),
                __( 'Export all dishes as UTF-8 CSV — download empty template included', 'flavor-restaurant-menu' ),
            )
        );
    }

    // -------------------------------------------------------------------------
    // Shared render helpers
    // -------------------------------------------------------------------------

    /**
     * Renders the upsell notice inside a meta box.
     */
    private static function metabox_notice( $title, $description, array $bullets ) {
        $pro_url = self::PRO_URL;
        ?>
        <div style="padding: 4px 0 8px;">
            <p style="margin: 0 0 10px; color: #3c434a; font-size: 13px; line-height: 1.5;">
                <?php echo esc_html( $description ); ?>
            </p>
            <ul style="margin: 0 0 14px 18px; list-style: disc; color: #3c434a; font-size: 13px; line-height: 1.7;">
                <?php foreach ( $bullets as $bullet ) : ?>
                    <li><?php echo esc_html( $bullet ); ?></li>
                <?php endforeach; ?>
            </ul>
            <a href="<?php echo esc_url( $pro_url ); ?>"
               class="button button-primary"
               target="_blank"
               rel="noopener">
                <?php
                /* translators: %s: feature name */
                printf( esc_html__( 'Get %s with Flavor Pro →', 'flavor-restaurant-menu' ), esc_html( $title ) );
                ?>
            </a>
        </div>
        <?php
    }

    /**
     * Renders a full admin upsell page.
     */
    private static function pro_page( $title, $description, array $bullets ) {
        $pro_url = self::PRO_URL;
        ?>
        <div class="wrap">
            <h1><?php echo esc_html( $title ); ?></h1>
            <div style="max-width: 640px; margin-top: 24px; padding: 28px 32px; background: #fff; border: 1px solid #c3c4c7; border-radius: 4px;">
                <p style="margin: 0 0 16px; font-size: 14px; color: #3c434a; line-height: 1.6;">
                    <?php echo esc_html( $description ); ?>
                </p>
                <ul style="margin: 0 0 24px 20px; list-style: disc; font-size: 14px; color: #3c434a; line-height: 1.8;">
                    <?php foreach ( $bullets as $bullet ) : ?>
                        <li><?php echo esc_html( $bullet ); ?></li>
                    <?php endforeach; ?>
                </ul>
                <a href="<?php echo esc_url( $pro_url ); ?>"
                   class="button button-primary button-hero"
                   target="_blank"
                   rel="noopener">
                    <?php esc_html_e( 'Get Flavor Pro →', 'flavor-restaurant-menu' ); ?>
                </a>
                <p style="margin: 16px 0 0; font-size: 12px; color: #646970;">
                    <?php esc_html_e( 'Flavor Pro is a separate plugin available at flavor-menu.com. Installing it alongside the free version automatically unlocks all Pro features.', 'flavor-restaurant-menu' ); ?>
                </p>
            </div>
        </div>
        <?php
    }
}
