<?php
if (!defined('ABSPATH'))
    exit;

class PRECIOUSUITE_Settings
{

    public function __construct()
    {
        add_action('admin_menu', array($this, 'add_admin_menu'), 42);
        add_action('admin_init', array($this, 'register_settings'));
    }

    public function add_admin_menu()
    {
        add_submenu_page(
            'preciousuite',
            'Gold Calculator Settings',
            'Buyback Calculator',
            'manage_options',
            'preciousuite-settings',
            array($this, 'settings_page_html')
        );
    }

    public function register_settings()
    {
        register_setting('preciousuite_settings_group', 'preciousuite_manual_gold_rate', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('preciousuite_settings_group', 'preciousuite_buyback_margin', array('sanitize_callback' => 'absint'));
        register_setting('preciousuite_settings_group', 'preciousuite_enable_lead_capture', array('sanitize_callback' => 'absint'));
        register_setting('preciousuite_settings_group', 'preciousuite_currency', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('preciousuite_settings_group', 'preciousuite_smtp_enable', array('sanitize_callback' => 'absint'));
        register_setting('preciousuite_settings_group', 'preciousuite_smtp_host', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('preciousuite_settings_group', 'preciousuite_smtp_port', array('sanitize_callback' => 'absint'));
        register_setting('preciousuite_settings_group', 'preciousuite_smtp_username', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('preciousuite_settings_group', 'preciousuite_smtp_password', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('preciousuite_settings_group', 'preciousuite_whatsapp_enable', array('sanitize_callback' => 'absint'));
        register_setting('preciousuite_settings_group', 'preciousuite_whatsapp_number', array('sanitize_callback' => 'sanitize_text_field'));
    }

    public function settings_page_html()
    {
        ?>
        <div class="wrap preciousuite-wrap precioussuite-wrap preciousuite-dashboard preciousuite-gold-price">
            <div class="preciousuite-dashboard-shell">
                <?php $this->render_page_sidebar('settings'); ?>

                <main class="preciousuite-dashboard-main">
                    <section class="preciousuite-dashboard-card preciousuite-dashboard-hero-card">
                        <div class="preciousuite-dashboard-hero-content" style="max-width: 100% !important;">
                            <h1><?php esc_html_e('Daily Gold Rate Settings', 'preciousuite-gold-buyback-quote-calculator'); ?>
                            </h1>
                            <p><?php esc_html_e('Configure the buyback calculator rate, payout percentage, lead capture, WhatsApp sharing, and email delivery.', 'preciousuite-gold-buyback-quote-calculator'); ?>
                            </p>
                        </div>
                    </section>

                    <div class="preciousuite-dashboard-grids">
                        <section class="preciousuite-dashboard-card">
                            <div class="preciousuite-main-panel">
                                <form method="post" action="options.php" class="preciousuite-form preciousuite-buyback-form">
                                    <?php
                                    settings_fields('preciousuite_settings_group');
                                    do_settings_sections('preciousuite_settings_group');
                                    ?>
                                    <div class="preciousuite-panel-heading">
                                        <div>
                                            <h2><?php esc_html_e('Calculator Settings', 'preciousuite-gold-buyback-quote-calculator'); ?>
                                            </h2>
                                            <p><?php esc_html_e('Set the market rate and payout behavior used by the frontend gold buyback calculator.', 'preciousuite-gold-buyback-quote-calculator'); ?>
                                            </p>
                                        </div>
                                    </div>

                                    <div class="preciousuite-short-code-container" bis_skin_checked="1">
                                        <?php esc_html_e('Use shortcode anywhere on your website:', 'preciousuite-gold-buyback-quote-calculator'); ?>
                                        <code>[preciousuite_calculator]</code>
                                    </div>

                                    <table class="form-table">
                                        <tr valign="top">
                                            <th scope="row">Today's Gold Rate (per gram)</th>
                                            <td>
                                                <input type="number" step="0.01" name="preciousuite_manual_gold_rate"
                                                    value="<?php echo esc_attr(get_option('preciousuite_manual_gold_rate', '75.00')); ?>"
                                                    class="regular-text" />
                                            </td>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">Your Payout Percentage (%)</th>
                                            <td>
                                                <input type="number" name="preciousuite_buyback_margin"
                                                    value="<?php echo esc_attr(get_option('preciousuite_buyback_margin', '95')); ?>"
                                                    class="regular-text" />
                                                <p class="description">Example: 95 means you pay the customer 95% of market
                                                    value.
                                                    Your
                                                    profit
                                                    is
                                                    5%.</p>
                                            </td>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">Enable Lead Capture</th>
                                            <td>
                                                <input type="checkbox" name="preciousuite_enable_lead_capture" value="1" <?php checked(get_option('preciousuite_enable_lead_capture', '1'), 1); ?> />
                                                <p class="description">If checked, shows the "Lock this Price" email form to
                                                    visitors.
                                                </p>
                                            </td>
                                        </tr>

                                        <tr>
                                            <th colspan="2" style="padding-top:20px;">
                                                <hr>
                                                <h2>WhatsApp Integration</h2>
                                            </th>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">Enable WhatsApp Share</th>
                                            <td>
                                                <input type="checkbox" name="preciousuite_whatsapp_enable" value="1" <?php checked(get_option('preciousuite_whatsapp_enable'), 1); ?> />
                                                <p class="description">If enabled, customers get a button to instantly forward
                                                    their
                                                    quote
                                                    to
                                                    your
                                                    WhatsApp.</p>
                                            </td>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">Admin WhatsApp Number</th>
                                            <td>
                                                <input type="text" name="preciousuite_whatsapp_number"
                                                    value="<?php echo esc_attr(get_option('preciousuite_whatsapp_number')); ?>"
                                                    class="regular-text" placeholder="e.g. 15551234567" />
                                                <p class="description">Include country code. Do not use + or - or spaces.
                                                    Example
                                                    for
                                                    India:
                                                    919876543210</p>
                                            </td>
                                        </tr>

                                        <tr>
                                            <th colspan="2" style="padding-top:20px;">
                                                <hr>
                                                <h2>Localization</h2>
                                            </th>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">Currency</th>
                                            <td>
                                                <?php $currency = get_option('preciousuite_currency', '$'); ?>
                                                <select name="preciousuite_currency">
                                                    <option value="$" <?php selected($currency, '$'); ?>>USD ($)</option>
                                                    <option value="€" <?php selected($currency, '€'); ?>>EUR (€)</option>
                                                    <option value="£" <?php selected($currency, '£'); ?>>GBP (£)</option>
                                                    <option value="₹" <?php selected($currency, '₹'); ?>>INR (₹)</option>
                                                    <option value="A$" <?php selected($currency, 'A$'); ?>>AUD (A$)</option>
                                                    <option value="C$" <?php selected($currency, 'C$'); ?>>CAD (C$)</option>
                                                </select>
                                                <p class="description">Select the currency symbol to display on the calculator.
                                                </p>
                                            </td>
                                        </tr>

                                        <tr>
                                            <th colspan="2" style="padding-top:30px;">
                                                <hr>
                                                <h2>Email Deliverability (SMTP)</h2>
                                                <p>Enable and configure these settings if your local server (XAMPP) cannot send
                                                    lead
                                                    emails.
                                                </p>
                                            </th>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">Enable Custom SMTP</th>
                                            <td>
                                                <input type="checkbox" name="preciousuite_smtp_enable" value="1" <?php checked(get_option('preciousuite_smtp_enable'), 1); ?> />
                                                <span class="description">Check to route emails through an external SMTP
                                                    server.</span>
                                            </td>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">SMTP Host</th>
                                            <td>
                                                <input type="text" name="preciousuite_smtp_host"
                                                    value="<?php echo esc_attr(get_option('preciousuite_smtp_host', 'smtp.gmail.com')); ?>"
                                                    class="regular-text" />
                                            </td>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">SMTP Port</th>
                                            <td>
                                                <input type="number" name="preciousuite_smtp_port"
                                                    value="<?php echo esc_attr(get_option('preciousuite_smtp_port', '587')); ?>"
                                                    class="regular-text" />
                                                <p class="description">Commonly 587 (TLS) or 465 (SSL)</p>
                                            </td>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">SMTP Username / Email</th>
                                            <td>
                                                <input type="text" name="preciousuite_smtp_username"
                                                    value="<?php echo esc_attr(get_option('preciousuite_smtp_username')); ?>"
                                                    class="regular-text" />
                                            </td>
                                        </tr>
                                        <tr valign="top">
                                            <th scope="row">SMTP Password</th>
                                            <td>
                                                <input type="password" name="preciousuite_smtp_password"
                                                    value="<?php echo esc_attr(get_option('preciousuite_smtp_password')); ?>"
                                                    class="regular-text" />
                                                <p class="description">If using Gmail, generate an <a
                                                        href="https://myaccount.google.com/apppasswords" target="_blank">App
                                                        Password</a> to
                                                    use
                                                    here.</p>
                                            </td>
                                        </tr>
                                    </table>
                                    <div class="preciousuite-form-footer">
                                        <input type="submit" name="submit" id="submit"
                                            class="button button-primary update-settings-btn"
                                            value="<?php esc_attr_e('Update Settings', 'preciousuite-gold-buyback-quote-calculator'); ?>">
                                        <span><?php esc_html_e('These settings control the frontend quote calculator and customer lead flow.', 'preciousuite-gold-buyback-quote-calculator'); ?></span>
                                    </div>
                                    <div class="preciousuite-info-grid">
                                        <div class="preciousuite-info-card">
                                            <div class="preciousuite-info-card-icon">
                                                <svg width="24" height="24" viewBox="0 0 24 24" fill="none"
                                                    stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                                    stroke-linejoin="round">
                                                    <rect x="4" y="4" width="16" height="16" rx="2" ry="2"></rect>
                                                    <line x1="9" y1="9" x2="15" y2="9"></line>
                                                    <line x1="9" y1="13" x2="15" y2="13"></line>
                                                    <line x1="9" y1="17" x2="15" y2="17"></line>
                                                </svg>
                                            </div>
                                            <div class="preciousuite-info-card-content">
                                                <strong><?php esc_html_e('Quote workflow', 'preciousuite-gold-buyback-quote-calculator'); ?></strong>
                                                <p><?php esc_html_e('Customers calculate a payout estimate and can lock the quote when lead capture is enabled.', 'preciousuite-gold-buyback-quote-calculator'); ?>
                                                </p>
                                            </div>
                                        </div>
                                        <div class="preciousuite-info-card">
                                            <div class="preciousuite-info-card-icon">
                                                <svg width="24" height="24" viewBox="0 0 24 24" fill="none"
                                                    stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                                    stroke-linejoin="round">
                                                    <line x1="22" y1="2" x2="11" y2="13"></line>
                                                    <polygon points="22 2 15 22 11 13 2 9 22 2"></polygon>
                                                </svg>
                                            </div>
                                            <div class="preciousuite-info-card-content">
                                                <strong><?php esc_html_e('Lead delivery', 'preciousuite-gold-buyback-quote-calculator'); ?></strong>
                                                <p><?php esc_html_e('Use SMTP and WhatsApp options to route quote leads to your preferred sales workflow.', 'preciousuite-gold-buyback-quote-calculator'); ?>
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </section>
                    </div>
                </main>

                <?php $this->render_right_sidebar(); ?>
            </div>
        </div>
        <?php
    }

    private function render_page_sidebar($active_tab)
    {
        ?>
        <aside class="preciousuite-dashboard-nav">
            <div class="preciousuite-dashboard-brand">
                <div class="preciousuite-page-brand">
                    <div class="preciousuite-box preciousuite-logo">
                        <a href="<?php echo esc_url("https://www.preciousuite.com"); ?>" target="_blank"
                            rel="noopener noreferrer" class="preciousuite-page-title">
                            <img src="<?php echo esc_url(PRECIOUSUITE_URL . 'assets/uploads/Preciousuite_Logo.png'); ?>"
                                alt="<?php esc_attr_e('Preciousuite Logo', 'preciousuite-gold-buyback-quote-calculator'); ?>">
                        </a>
                    </div>
                </div>
            </div>
            <nav class="preciousuite-dashboard-nav-inner">
                <div class="preciousuite-nav-links">
                    <a class="<?php echo 'settings' === $active_tab ? 'is-active' : ''; ?>"
                        href="<?php echo esc_url(admin_url('admin.php?page=preciousuite-settings')); ?>">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                            stroke-linecap="round" stroke-linejoin="round" style="margin-right: .75rem;">
                            <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
                            <line x1="3" y1="9" x2="21" y2="9"></line>
                            <line x1="9" y1="21" x2="9" y2="9"></line>
                        </svg>
                        <?php esc_html_e('Calculator Settings', 'preciousuite-gold-buyback-quote-calculator'); ?>
                    </a>
                    <a class="<?php echo 'quotes' === $active_tab ? 'is-active' : ''; ?>"
                        href="<?php echo esc_url(admin_url('edit.php?post_type=preciousuite_quote')); ?>">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                            stroke-linecap="round" stroke-linejoin="round" style="margin-right: .75rem;">
                            <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
                            <polyline points="14 2 14 8 20 8"></polyline>
                            <line x1="16" y1="13" x2="8" y2="13"></line>
                            <line x1="16" y1="17" x2="8" y2="17"></line>
                            <polyline points="10 9 9 9 8 9"></polyline>
                        </svg>
                        <?php esc_html_e('Quotes', 'preciousuite-gold-buyback-quote-calculator'); ?>
                    </a>
                </div>

                <div class="preciousuite-sidebar-help-card">
                    <strong>
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
                            stroke-linecap="round" stroke-linejoin="round">
                            <circle cx="12" cy="12" r="10"></circle>
                            <circle cx="12" cy="12" r="4"></circle>
                            <line x1="4.93" y1="4.93" x2="9.17" y2="9.17"></line>
                            <line x1="14.83" y1="14.83" x2="19.07" y2="19.07"></line>
                            <line x1="14.83" y1="9.17" x2="19.07" y2="4.93"></line>
                            <line x1="14.83" y1="9.17" x2="18.36" y2="5.64"></line>
                            <line x1="4.93" y1="19.07" x2="9.17" y2="14.83"></line>
                        </svg>
                        Need help?
                    </strong>
                    <p>Check our documentation or contact support.</p>
                    <a href="https://wordpress.org/support/plugin/preciousuite-gold-buyback-quote-calculator/" target="_blank"
                        rel="noopener noreferrer">Visit Support &rarr;</a>
                </div>
            </nav>
        </aside>
        <?php
    }

    private function render_right_sidebar()
    {
        ?>
        <aside class="preciousuite-dashboard-sidebar">
            <section class="preciousuite-sidebar-card preciousuite-pro-card">
                <h2>
                    <?php esc_html_e('Preciousuite Pro Bundle', 'preciousuite-gold-buyback-quote-calculator'); ?>
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
                        stroke-linecap="round" stroke-linejoin="round">
                        <path d="M6 3h12l4 6-10 12L2 9l4-6z"></path>
                        <path d="M2 9h20"></path>
                        <path d="M12 21L8 9"></path>
                        <path d="M12 21l4-12"></path>
                        <path d="M6 3l2 6"></path>
                        <path d="M18 3l-2 6"></path>
                    </svg>
                </h2>

                <p style="margin: 12px 0 16px 0; color: #475569; font-size: 13px;">
                    <?php esc_html_e('Grow your jewelry buyback workflow with advanced quote controls, premium templates, and priority support.', 'preciousuite-gold-buyback-quote-calculator'); ?>
                </p>

                <div class="preciousuite-pro-features">
                    <div class="preciousuite-pro-feature">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff" stroke-width="2">
                            <circle cx="12" cy="12" r="11" stroke="none" />
                            <path d="M8 12.5l3 3 5-6" />
                        </svg>
                        <span><?php esc_html_e('Advanced buyback quote workflows', 'preciousuite-gold-buyback-quote-calculator'); ?></span>
                    </div>
                    <div class="preciousuite-pro-feature">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff" stroke-width="2">
                            <circle cx="12" cy="12" r="11" stroke="none" />
                            <path d="M8 12.5l3 3 5-6" />
                        </svg>
                        <span><?php esc_html_e('Professional invoice and certificate templates', 'preciousuite-gold-buyback-quote-calculator'); ?></span>
                    </div>
                    <div class="preciousuite-pro-feature">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff" stroke-width="2">
                            <circle cx="12" cy="12" r="11" stroke="none" />
                            <path d="M8 12.5l3 3 5-6" />
                        </svg>
                        <span><?php esc_html_e('More chart layouts and display options', 'preciousuite-gold-buyback-quote-calculator'); ?></span>
                    </div>
                    <div class="preciousuite-pro-feature">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff" stroke-width="2">
                            <circle cx="12" cy="12" r="11" stroke="none" />
                            <path d="M8 12.5l3 3 5-6" />
                        </svg>
                        <span><?php esc_html_e('Priority support for setup questions', 'preciousuite-gold-buyback-quote-calculator'); ?></span>
                    </div>
                </div>

                <div class="preciousuite-pro-actions">
                    <a class="button button-primary"
                        href="<?php echo esc_url('https://preciousuite.com/products'); ?>"><?php esc_html_e('View Upgrade', 'preciousuite-gold-buyback-quote-calculator'); ?></a>
                    <small><?php esc_html_e('Optional bundle features. No forced upsells.', 'preciousuite-gold-buyback-quote-calculator'); ?></small>
                </div>
            </section>

            <section class="preciousuite-sidebar-card preciousuite-quick-actions">
                <h2>
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
                        stroke-linecap="round" stroke-linejoin="round">
                        <path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"></path>
                        <path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"></path>
                    </svg>
                    <?php esc_html_e('Quick Actions', 'preciousuite-gold-buyback-quote-calculator'); ?>
                </h2>
                <div class="preciousuite-action-list">
                    <a href="<?php echo esc_url(admin_url('admin.php?page=preciousuite-settings')); ?>">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                            stroke-linecap="round" stroke-linejoin="round">
                            <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
                            <line x1="3" y1="9" x2="21" y2="9"></line>
                            <line x1="9" y1="21" x2="9" y2="9"></line>
                        </svg>
                        <?php esc_html_e('Calculator Settings', 'preciousuite-gold-buyback-quote-calculator'); ?>
                    </a>
                    <a href="<?php echo esc_url(admin_url('edit.php?post_type=preciousuite_quote')); ?>">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                            stroke-linecap="round" stroke-linejoin="round">
                            <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
                            <polyline points="14 2 14 8 20 8"></polyline>
                            <line x1="16" y1="13" x2="8" y2="13"></line>
                            <line x1="16" y1="17" x2="8" y2="17"></line>
                            <polyline points="10 9 9 9 8 9"></polyline>
                        </svg>
                        <?php esc_html_e('View Quotes', 'preciousuite-gold-buyback-quote-calculator'); ?>
                    </a>
                    <a href="https://wordpress.org/support/plugin/preciousuite-gold-buyback-quote-calculator/" target="_blank"
                        rel="noopener noreferrer">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                            stroke-linecap="round" stroke-linejoin="round">
                            <path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"></path>
                            <path d="M13.73 21a2 2 0 0 1-3.46 0"></path>
                        </svg>
                        <?php esc_html_e('Support', 'preciousuite-gold-buyback-quote-calculator'); ?>
                    </a>
                </div>
            </section>

            <section class="preciousuite-sidebar-card preciousuite-learn-card">
                <h2>
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
                        stroke-linecap="round" stroke-linejoin="round">
                        <path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"></path>
                        <path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"></path>
                    </svg>
                    <?php esc_html_e('Learn Buyback', 'preciousuite-gold-buyback-quote-calculator'); ?>
                </h2>
                <p>
                    <?php esc_html_e('Use the shortcode, payout percentage, lead capture, WhatsApp, and SMTP settings to create a complete buyback quote workflow.', 'preciousuite-gold-buyback-quote-calculator'); ?>
                </p>
                <a href="https://www.preciousuite.com/docs/#preciousuite-gold-buyback-quote-calculator" target="_blank"
                    rel="noopener noreferrer"
                    class="preciousuite-link"><?php esc_html_e('View documentation &rarr;', 'preciousuite-gold-buyback-quote-calculator'); ?></a>
            </section>
        </aside>
        <?php
    }

    private function render_tabs($active_tab)
    {
        $tabs = array(
            'settings' => array(
                'label' => __('Calculator Settings', 'preciousuite-gold-buyback-quote-calculator'),
                'url' => admin_url('admin.php?page=preciousuite-settings'),
            ),
            'quotes' => array(
                'label' => __('Quotes', 'preciousuite-gold-buyback-quote-calculator'),
                'url' => admin_url('edit.php?post_type=preciousuite_quote'),
            ),
        );
        ?>
        <h2 class="nav-tab-wrapper preciousuite-tabs">
            <?php foreach ($tabs as $tab_key => $tab): ?>
                <a href="<?php echo esc_url($tab['url']); ?>"
                    class="nav-tab <?php echo $active_tab === $tab_key ? 'nav-tab-active' : ''; ?>">
                    <?php echo esc_html($tab['label']); ?>
                </a>
            <?php endforeach; ?>
        </h2>
        <?php
    }
}
