<?php

namespace MovieEngine\Admin\Emails;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

class EmailSettings {

    public static function handle_actions() {
        if (isset($_GET['reset_template']) && check_admin_referer('movie_engine_reset_template')) {
            $options = get_option('movie_engine_email_settings', []);
            $template_id = isset($_GET['reset_template']) ? sanitize_text_field(wp_unslash($_GET['reset_template'])) : '';

            if (!isset($options['notifications']) || !is_array($options['notifications'])) {
                $options['notifications'] = [];
            }

            if (array_key_exists($template_id, $options['notifications'])) {
                unset($options['notifications'][$template_id]);
                remove_all_filters('sanitize_option_movie_engine_email_settings');
                update_option('movie_engine_email_settings', $options);
            }

            wp_safe_redirect(remove_query_arg(['reset_template', '_wpnonce']));
            exit;
        }
    }

    public static function render($active_tab_slug = 'emails_general') {

        settings_fields('movie_engine_emails');
        $options = get_option('movie_engine_email_settings', []);
        $email_primary_color = !empty($options['primary_color']) ? $options['primary_color'] : '#E50914';
        $border_color = !empty($options['border_color']) ? $options['border_color'] : '#333333';
        $admin_ui_color = '#E50914';

        $default_welcome_content = "<h1>Welcome aboard, {user_name}!</h1>\n<p>We are thrilled to have you as part of our cinematic community. Your account has been successfully created and your journey starts here.</p>\n<p><strong>Active Plan:</strong> <strong>{plan_name}</strong></p>\n<p>You now have access to a world of endless entertainment. Discover the latest blockbusters and trending series.</p>\n<div style='text-align:center; margin-top:30px;'><a href='{browse_url}' class='btn'>Browse Catalog</a></div>";

        $default_forgot_content = "<h1>Reset Your Password</h1>\n<p>Hello {user_name}, we received a request to reset your password for your <strong>{site_name}</strong> account.</p>\n<p>If you didn't make this request, you can safely ignore this email. Otherwise, you can reset your password using the button below:</p>\n<div style='text-align:center; margin-top:30px;'><a href='{reset_url}' class='btn'>Reset Password</a></div>\n<p style='margin-top:20px; font-size:12px; opacity:0.6;'>This link will expire in 2 hours.</p>";

        $default_receipt_content = "<h1>Hi {user_name},</h1>\n<p>Thank you for your purchase! Your payment has been successfully processed and your content is now available.</p>\n<table class='meta-table' style='table-layout:fixed; width:100%;'>\n<tr><td style='width:35%;'>Item Purchased</td><td class='val'><strong>{item_name}</strong></td></tr>\n<tr><td>Amount Paid</td><td class='val'><strong>{amount}</strong></td></tr>\n<tr><td>Transaction ID</td><td class='val' style='word-break:break-all; word-wrap:break-word; overflow-wrap:anywhere;'><strong>{transaction_id}</strong></td></tr>\n</table>\n<div style='text-align:center; margin-top:30px;'><a href='{watch_url}' class='btn'>Start Watching Now</a></div>";

        $default_admin_sale_content = "<h1>New Sale Goal! 🚀</h1>\n<p>Great news! A new transaction has been completed on your site.</p>\n<table class='meta-table'>\n<tr><td>Customer</td><td class='val'><strong>{user_name}</strong></td></tr>\n<tr><td>Item Sold</td><td class='val'><strong>{item_name}</strong></td></tr>\n<tr><td>Revenue</td><td class='val'><strong>{amount}</strong></td></tr>\n</table>\n<p>Check your payment provider dashboard for more details.</p>";

        $default_referral_sale_content = "<h1>You earned a new commission!</h1>\n<p>Hi {user_name},</p>\n<p>Great news! One of your referrals has just completed a purchase.</p>\n<div style='background: #f0fdf4; border-left: 4px solid #16a34a; padding: 15px; margin: 20px 0;'>\n<p style='margin: 0; font-size: 16px; color: #166534;'><strong>You earned: \${amount}</strong></p>\n</div>\n<p>This amount has been added to your account balance.</p>\n<div style='text-align:center; margin-top:30px;'><a href='{dashboard_link}' class='btn'>View Dashboard</a></div>";

        $default_payout_paid_content = "<h1>Payout Processed</h1>\n<p>Hi {user_name},</p>\n<p>We have successfully processed your payout request.</p>\n<div style='background: #eff6ff; border-left: 4px solid #2563eb; padding: 15px; margin: 20px 0;'>\n<p style='margin: 0; font-size: 16px; color: #1e40af;'><strong>Amount Paid: \${amount}</strong></p>\n</div>\n<p>The funds should appear in your account shortly.</p>\n<div style='text-align:center; margin-top:30px;'><a href='{dashboard_link}' class='btn'>View Payout History</a></div>";

        $default_payout_rejected_content = "<h1>Payout Request Rejected</h1>\n<p>Hi {user_name},</p>\n<p>Your recent payout request has been rejected.</p>\n<div style='background: #fff1f2; border-left: 4px solid #e11d48; padding: 15px; margin: 20px 0;'>\n<p style='margin: 0; font-size: 16px; color: #9f1239;'><strong>Amount: \${amount}</strong></p>\n<p style='margin: 10px 0 0 0; color: #881337;'><strong>Reason:</strong> {reason}</p>\n</div>\n<p>The funds have been returned to your balance.</p>\n<div style='text-align:center; margin-top:30px;'><a href='{dashboard_link}' class='btn'>Check Dashboard</a></div>";

        $default_admin_payout_content = "<h1>New Payout Request</h1>\n<p>Hello Admin,</p>\n<p>User <strong>{user_name}</strong> has requested a payout.</p>\n<div style='background: #fef9c3; border-left: 4px solid #eab308; padding: 15px; margin: 20px 0;'>\n<p style='margin: 0; font-size: 16px; color: #854d0e;'><strong>Request Amount: \${amount}</strong></p>\n</div>\n<p>Please review this in the dashboard.</p>\n<div style='text-align:center; margin-top:30px;'><a href='{admin_dashboard_link}' class='btn'>Manage Payouts</a></div>";

        $default_notifications = apply_filters('movie_engine_email_notifications_list', [
            'welcome' => [
                'name'    => esc_html__('Welcome Email', 'cine-movie-engine'),
                'enabled' => 1,
                'subject' => esc_html__('Welcome to {site_name}!', 'cine-movie-engine'),
                'content' => $default_welcome_content,
                'tags'    => ['{user_name}', '{plan_name}', '{browse_url}', '{dashboard_url}', '{site_name}']
            ],
            'forgot_password' => [
                'name'    => esc_html__('Forgot Password', 'cine-movie-engine'),
                'enabled' => 1,
                'subject' => esc_html__('Password Reset Request - {site_name}', 'cine-movie-engine'),
                'content' => $default_forgot_content,
                'tags'    => ['{user_name}', '{reset_url}', '{site_name}']
            ]
        ]);

        $pro_templates = [
            'receipt' => esc_html__('Payment Receipt', 'cine-movie-engine'),
            'admin_sale' => esc_html__('Admin: New Sale', 'cine-movie-engine'),
            'admin-payout-request' => esc_html__('Admin: New Payout Request', 'cine-movie-engine'),
            'referral-sale' => esc_html__('Referral: Commission Earned', 'cine-movie-engine'),
            'payout-paid' => esc_html__('Referral: Payout Processed', 'cine-movie-engine'),
            'payout-rejected' => esc_html__('Referral: Payout Rejected', 'cine-movie-engine')
        ];

        $notifications = [];
        foreach ($default_notifications as $key => $default) {
            $saved = isset($options['notifications'][$key]) ? $options['notifications'][$key] : [];
            $notifications[$key] = array_merge($default, $saved);
        }

        $current_sub_tab = str_replace('emails', '', $active_tab_slug);
        if (empty($current_sub_tab) || $current_sub_tab === '_general') {
            $current_sub_tab = 'general';
        } else {
            $current_sub_tab = ltrim($current_sub_tab, '_');
        }
?>
        <input type="hidden" name="movie_engine_email_settings[active_sub_tab]" id="movie-engine-active-sub-tab" value="<?php echo esc_attr($current_sub_tab); ?>">
        <div class="movie-engine-settings-section">
            <div class="movie-engine-section-header">
                <h2><?php esc_html_e('Notification Center', 'cine-movie-engine'); ?></h2>
                <p class="description"><?php esc_html_e('Manage all automated customer and admin email notifications.', 'cine-movie-engine'); ?></p>
            </div>

            <div class="movie-engine-sub-tabs">
                <a href="#general" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'general' ? 'active' : ''); ?>"><?php esc_html_e('Global Settings', 'cine-movie-engine'); ?></a>
                <a href="#templates" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'templates' ? 'active' : ''); ?>"><?php esc_html_e('Notifications', 'cine-movie-engine'); ?></a>
            </div>

            <div class="movie-engine-sub-tab-content">

                <div id="general" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'general' ? 'active' : ''); ?>">
                    <div class="movie-engine-card">
                        <div class="movie-engine-card-header">
                            <h3><span class="dashicons dashicons-admin-users"></span> <?php esc_html_e('Sender Details', 'cine-movie-engine'); ?></h3>
                        </div>
                        <div class="movie-engine-card-body">
                            <div class="movie-engine-form-row">
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('From Name', 'cine-movie-engine'); ?></label>
                                    <input type="text" name="movie_engine_email_settings[from_name]" value="<?php echo esc_attr($options['from_name'] ?? get_bloginfo('name')); ?>" class="movie-engine-input" placeholder="e.g. MovieEngine Support">
                                </div>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('From Email', 'cine-movie-engine'); ?></label>
                                    <input type="email" name="movie_engine_email_settings[from_email]" value="<?php echo esc_attr($options['from_email'] ?? get_option('admin_email')); ?>" class="movie-engine-input" placeholder="e.g. no-reply@yoursite.com">
                                </div>
                            </div>
                            <div class="movie-engine-form-group">
                                <label><?php esc_html_e('Reply To (Optional)', 'cine-movie-engine'); ?></label>
                                <input type="email" name="movie_engine_email_settings[reply_to]" value="<?php echo esc_attr($options['reply_to'] ?? ''); ?>" class="movie-engine-input" placeholder="e.g. support@yoursite.com">
                            </div>

                            <div class="movie-engine-form-group me-mt-15 me-border-top me-pt-15">
                                <label><?php esc_html_e('Admin Notification Emails', 'cine-movie-engine'); ?></label>
                                <input type="text" name="movie_engine_email_settings[admin_emails]" value="<?php echo esc_attr($options['admin_emails'] ?? get_option('admin_email')); ?>" class="movie-engine-input" placeholder="e.g. admin@site.com, boss@site.com">
                                <p class="description"><?php esc_html_e('Comma separated list of emails to receive admin alerts.', 'cine-movie-engine'); ?></p>
                            </div>
                        </div>
                    </div>

                    <div class="movie-engine-card">
                        <div class="movie-engine-card-header">
                            <h3><span class="dashicons dashicons-welcome-widgets-menus"></span> <?php esc_html_e('Email Header Styling', 'cine-movie-engine'); ?></h3>
                        </div>
                        <div class="movie-engine-card-body">
                            <div class="movie-engine-form-group">
                                <label><?php esc_html_e('Header Logo', 'cine-movie-engine'); ?></label>
                                <div class="movie-engine-media-picker">
                                    <input type="text" id="movie_engine_email_logo" name="movie_engine_email_settings[logo_url]" value="<?php echo esc_attr($options['logo_url'] ?? ''); ?>" class="movie-engine-input">
                                    <button type="button" class="movie-engine-btn me-btn-secondary me-upload-btn" data-target="#movie_engine_email_logo"><?php esc_html_e('Upload Logo', 'cine-movie-engine'); ?></button>
                                </div>
                            </div>
                            <div class="movie-engine-form-row">
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Logo Width (px)', 'cine-movie-engine'); ?></label>
                                    <input type="number" name="movie_engine_email_settings[logo_width]" value="<?php echo esc_attr($options['logo_width'] ?? '150'); ?>" class="movie-engine-input" placeholder="150">
                                </div>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Logo Height (px)', 'cine-movie-engine'); ?></label>
                                    <input type="number" name="movie_engine_email_settings[logo_height]" value="<?php echo esc_attr($options['logo_height'] ?? ''); ?>" class="movie-engine-input" placeholder="Auto">
                                </div>
                            </div>
                            <div class="movie-engine-form-group">
                                <label><?php esc_html_e('Header Background Color', 'cine-movie-engine'); ?></label>
                                <div class="movie-engine-color-control">
                                    <span class="color-preview" style="background-color: <?php echo esc_attr($options['header_bg_color'] ?? '#141414'); ?>"></span>
                                    <input type="text" name="movie_engine_email_settings[header_bg_color]" value="<?php echo esc_attr($options['header_bg_color'] ?? '#141414'); ?>" class="movie-engine-color-hex">
                                    <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['header_bg_color'] ?? '#141414'); ?>">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="movie-engine-card me-mt-25">
                        <div class="movie-engine-card-header">
                            <h3><span class="dashicons dashicons-editor-textcolor"></span> <?php esc_html_e('Email Content & Body Styling', 'cine-movie-engine'); ?></h3>
                        </div>
                        <div class="movie-engine-card-body">
                            <div class="movie-engine-form-row">
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Email Backdrop (Outer)', 'cine-movie-engine'); ?></label>
                                    <div class="movie-engine-color-control">
                                        <span class="color-preview" style="background-color: <?php echo esc_attr($options['email_bg_color'] ?? '#0f0f0f'); ?>"></span>
                                        <input type="text" name="movie_engine_email_settings[email_bg_color]" value="<?php echo esc_attr($options['email_bg_color'] ?? '#0f0f0f'); ?>" class="movie-engine-color-hex">
                                        <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['email_bg_color'] ?? '#0f0f0f'); ?>">
                                    </div>
                                </div>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Main Content Background', 'cine-movie-engine'); ?></label>
                                    <div class="movie-engine-color-control">
                                        <span class="color-preview" style="background-color: <?php echo esc_attr($options['content_bg_color'] ?? '#1a1a1a'); ?>"></span>
                                        <input type="text" name="movie_engine_email_settings[content_bg_color]" value="<?php echo esc_attr($options['content_bg_color'] ?? '#1a1a1a'); ?>" class="movie-engine-color-hex">
                                        <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['content_bg_color'] ?? '#1a1a1a'); ?>">
                                    </div>
                                </div>
                            </div>

                            <div class="movie-engine-form-row">
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Heading/Title Color', 'cine-movie-engine'); ?></label>
                                    <div class="movie-engine-color-control">
                                        <span class="color-preview" style="background-color: <?php echo esc_attr($options['content_title_color'] ?? '#ffffff'); ?>"></span>
                                        <input type="text" name="movie_engine_email_settings[content_title_color]" value="<?php echo esc_attr($options['content_title_color'] ?? '#ffffff'); ?>" class="movie-engine-color-hex">
                                        <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['content_title_color'] ?? '#ffffff'); ?>">
                                    </div>
                                </div>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Main Text Color', 'cine-movie-engine'); ?></label>
                                    <div class="movie-engine-color-control">
                                        <span class="color-preview" style="background-color: <?php echo esc_attr($options['content_text_color'] ?? '#cccccc'); ?>"></span>
                                        <input type="text" name="movie_engine_email_settings[content_text_color]" value="<?php echo esc_attr($options['content_text_color'] ?? '#cccccc'); ?>" class="movie-engine-color-hex">
                                        <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['content_text_color'] ?? '#cccccc'); ?>">
                                    </div>
                                </div>
                            </div>

                            <div class="movie-engine-form-row">
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Link Color', 'cine-movie-engine'); ?></label>
                                    <div class="movie-engine-color-control">
                                        <span class="color-preview" style="background-color: <?php echo esc_attr($options['link_color'] ?? '#E50914'); ?>"></span>
                                        <input type="text" name="movie_engine_email_settings[link_color]" value="<?php echo esc_attr($options['link_color'] ?? '#E50914'); ?>" class="movie-engine-color-hex">
                                        <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['link_color'] ?? '#E50914'); ?>">
                                    </div>
                                </div>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Table/Content Border', 'cine-movie-engine'); ?></label>
                                    <div class="movie-engine-color-control">
                                        <span class="color-preview" style="background-color: <?php echo esc_attr($options['border_color'] ?? '#333333'); ?>"></span>
                                        <input type="text" name="movie_engine_email_settings[border_color]" value="<?php echo esc_attr($options['border_color'] ?? '#333333'); ?>" class="movie-engine-color-hex">
                                        <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['border_color'] ?? '#333333'); ?>">
                                    </div>
                                </div>
                            </div>

                            <div class="movie-engine-form-row">
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Primary Button Color', 'cine-movie-engine'); ?></label>
                                    <div class="movie-engine-color-control">
                                        <span class="color-preview" style="background-color: <?php echo esc_attr($options['primary_color'] ?? '#E50914'); ?>"></span>
                                        <input type="text" name="movie_engine_email_settings[primary_color]" value="<?php echo esc_attr($options['primary_color'] ?? '#E50914'); ?>" class="movie-engine-color-hex">
                                        <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['primary_color'] ?? '#E50914'); ?>">
                                    </div>
                                </div>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Button Text Color', 'cine-movie-engine'); ?></label>
                                    <div class="movie-engine-color-control">
                                        <span class="color-preview" style="background-color: <?php echo esc_attr($options['btn_text_color'] ?? '#FFFFFF'); ?>"></span>
                                        <input type="text" name="movie_engine_email_settings[btn_text_color]" value="<?php echo esc_attr($options['btn_text_color'] ?? '#FFFFFF'); ?>" class="movie-engine-color-hex">
                                        <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['btn_text_color'] ?? '#FFFFFF'); ?>">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="movie-engine-card me-mt-25">
                        <div class="movie-engine-card-header">
                            <h3><span class="dashicons dashicons-editor-insertmore"></span> <?php esc_html_e('Email Footer Styling', 'cine-movie-engine'); ?></h3>
                        </div>
                        <div class="movie-engine-card-body">
                            <div class="movie-engine-form-group">
                                <label><?php esc_html_e('Footer Background Color', 'cine-movie-engine'); ?></label>
                                <div class="movie-engine-color-control">
                                    <span class="color-preview" style="background-color: <?php echo esc_attr($options['footer_bg_color'] ?? '#1a1a1a'); ?>"></span>
                                    <input type="text" name="movie_engine_email_settings[footer_bg_color]" value="<?php echo esc_attr($options['footer_bg_color'] ?? '#1a1a1a'); ?>" class="movie-engine-color-hex">
                                    <input type="color" class="movie-engine-color-picker-input" value="<?php echo esc_attr($options['footer_bg_color'] ?? '#1a1a1a'); ?>">
                                </div>
                            </div>

                            <div class="movie-engine-form-row">
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Privacy Link', 'cine-movie-engine'); ?></label>
                                    <input type="text" name="movie_engine_email_settings[privacy_text]" value="<?php echo esc_attr($options['privacy_text'] ?? esc_html__('Privacy Policy', 'cine-movie-engine')); ?>" class="movie-engine-input me-mb-5" placeholder="<?php esc_html_e('Text', 'cine-movie-engine'); ?>">
                                    <input type="text" name="movie_engine_email_settings[privacy_url]" value="<?php echo esc_attr($options['privacy_url'] ?? home_url('/privacy-policy')); ?>" class="movie-engine-input" placeholder="<?php esc_html_e('URL', 'cine-movie-engine'); ?>">
                                </div>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Terms Link', 'cine-movie-engine'); ?></label>
                                    <input type="text" name="movie_engine_email_settings[terms_text]" value="<?php echo esc_attr($options['terms_text'] ?? esc_html__('Terms of Service', 'cine-movie-engine')); ?>" class="movie-engine-input me-mb-5" placeholder="<?php esc_html_e('Text', 'cine-movie-engine'); ?>">
                                    <input type="text" name="movie_engine_email_settings[terms_url]" value="<?php echo esc_attr($options['terms_url'] ?? home_url('/terms')); ?>" class="movie-engine-input" placeholder="<?php esc_html_e('URL', 'cine-movie-engine'); ?>">
                                </div>
                            </div>

                            <div class="movie-engine-form-group">
                                <label><?php esc_html_e('Footer Copyright Text', 'cine-movie-engine'); ?></label>
                                <textarea name="movie_engine_email_settings[footer_text]" class="movie-engine-textarea" rows="2" placeholder="<?php esc_html_e('e.g. © 2025 MovieEngine. All rights reserved.', 'cine-movie-engine'); ?>"><?php echo esc_textarea($options['footer_text'] ?? ''); ?></textarea>
                            </div>
                        </div>
                    </div>

                    <div class="movie-engine-card">
                        <div class="movie-engine-card-header">
                            <h3><span class="dashicons dashicons-email-alt"></span> <?php esc_html_e('Test Email', 'cine-movie-engine'); ?></h3>
                        </div>
                        <div class="movie-engine-card-body">
                            <div class="movie-engine-test-email-wrap me-flex-gap">
                                <input type="email" id="movie-engine-test-email-target" class="movie-engine-input me-flex-1" placeholder="<?php esc_html_e('Enter your email address...', 'cine-movie-engine'); ?>">
                                <button type="button" id="movie-engine-send-test-email" class="movie-engine-btn me-btn-primary">
                                    <span class="dashicons dashicons-paper-plane"></span> <?php esc_html_e('Send Test', 'cine-movie-engine'); ?>
                                </button>
                            </div>
                            <div id="movie-engine-test-email-status" class="me-mt-10"></div>
                        </div>
                    </div>
                </div>

                <div id="templates" class="movie-engine-sub-panel <?php echo $current_sub_tab === 'templates' ? 'active' : ''; ?>">
                    <div class="movie-engine-notification-editor-layout">
                        <div class="movie-engine-notification-sidebar">
                            <?php foreach ($notifications as $key => $notif) : ?>
                                <div class="movie-engine-notif-item <?php echo $key === 'welcome' ? 'active' : ''; ?>" data-target="<?php echo esc_attr($key); ?>">
                                    <div class="movie-engine-notif-info">
                                        <span class="movie-engine-notif-label"><?php echo esc_html($notif['name']); ?></span>
                                        <small class="movie-engine-notif-type"><?php echo (strpos($key, 'admin') !== false) ? 'Admin' : 'Customer'; ?></small>
                                    </div>
                                    <span class="movie-engine-notif-status <?php echo !empty($notif['enabled']) ? 'enabled' : ''; ?>"></span>
                                </div>
                            <?php endforeach; ?>

                            <?php if (! apply_filters('movie_engine_is_pro', false)) : ?>
                                <?php foreach ($pro_templates as $key => $name) : ?>
                                    <div class="movie-engine-notif-item me-pro-item" data-target="<?php echo esc_attr($key); ?>" style="opacity: 0.6; cursor: pointer;">
                                        <div class="movie-engine-notif-info">
                                            <span class="movie-engine-notif-label"><?php echo esc_html($name); ?></span>
                                            <small class="movie-engine-notif-type">PRO Feature</small>
                                        </div>
                                        <span class="me-pro-badge-small" style="font-size: 10px; padding: 2px 6px; background: #E50914; color: #fff; border-radius: 4px;">PRO</span>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>

                        <div class="movie-engine-notification-main">
                            <?php
                            $first_panel_key = array_key_first($notifications);
                            foreach ($notifications as $key => $notif) :
                            ?>
                                <div id="panel-<?php echo esc_attr($key); ?>" class="movie-engine-notif-panel <?php echo $key === $first_panel_key ? 'active' : ''; ?>">
                                    <div class="movie-engine-panel-header">
                                        <div class="me-flex-1">
                                            <h3><?php echo esc_html($notif['name']); ?></h3>
                                        </div>
                                        <div class="movie-engine-toggle-group">
                                            <label class="movie-engine-switch">
                                                <input type="hidden" name="movie_engine_email_settings[notifications][<?php echo esc_attr($key); ?>][enabled]" value="0">
                                                <input type="checkbox" name="movie_engine_email_settings[notifications][<?php echo esc_attr($key); ?>][enabled]" value="1" <?php checked($notif['enabled'] ?? 1, 1); ?>>
                                                <span class="movie-engine-slider"></span>
                                            </label>
                                            <span class="movie-engine-toggle-label"><?php esc_html_e('Enable Notification', 'cine-movie-engine'); ?></span>
                                        </div>
                                    </div>

                                    <div class="movie-engine-form-group">
                                        <label><?php esc_html_e('Email Subject', 'cine-movie-engine'); ?></label>
                                        <input type="text" name="movie_engine_email_settings[notifications][<?php echo esc_attr($key); ?>][subject]" value="<?php echo esc_attr($notif['subject']); ?>" class="movie-engine-input me-personalize-target">
                                    </div>

                                    <div class="movie-engine-tags-container">
                                        <label><?php esc_html_e('Personalization Tags:', 'cine-movie-engine'); ?></label>
                                        <div class="movie-engine-tag-cloud">
                                            <?php foreach ($notif['tags'] as $tag) : ?>
                                                <span class="movie-engine-tag-badge" onclick="copyTagToClipboard('<?php echo esc_js($tag); ?>')"><?php echo esc_html($tag); ?></span>
                                            <?php endforeach; ?>
                                        </div>
                                    </div>

                                    <div class="movie-engine-editor-wrap">
                                        <label><?php esc_html_e('Email Content', 'cine-movie-engine'); ?></label>
                                        <?php
                                        $content = !empty($notif['content']) ? $notif['content'] : '';
                                        if (empty($content)) {
                                            $def_key = "default_{$key}_content";
                                            $content = isset($$def_key) ? $$def_key : '';
                                        }

                                        $content = preg_replace('/<span[^>]*data-mce-type=["\']bookmark["\'][^>]*>.*?<\/span>/i', '', $content);

                                        $editor_bg    = !empty($options['content_bg_color']) ? $options['content_bg_color'] : '#111111';
                                        $editor_text  = !empty($options['content_text_color']) ? $options['content_text_color'] : '#ffffff';
                                        $editor_border = !empty($options['border_color']) ? $options['border_color'] : '#333333';
                                        $editor_style = "body{background-color:$editor_bg;color:$editor_text;font-family:sans-serif;font-size:14px;padding:20px;}a{color:$email_primary_color;}table{width:100%;border-collapse:collapse;margin:20px 0;border:1px solid $editor_border!important;}td,th{padding:12px 15px;border:1px solid $editor_border!important;color:inherit;}.btn{background:$email_primary_color;color:#fff;padding:10px 20px;text-decoration:none;border-radius:4px;display:inline-block;}";

                                        $editor_id = 'me' . str_replace('_', '', $key);
                                        wp_editor(
                                            $content,
                                            $editor_id,
                                            [
                                                'textarea_name' => "movie_engine_email_settings[notifications][$key][content]",
                                                'media_buttons' => true,
                                                'textarea_rows' => 12,
                                                'quicktags'     => true,
                                                'tinymce'       => [
                                                    'body_class' => 'movie-engine-editor-body',
                                                    'content_style' => $editor_style,
                                                ]
                                            ]
                                        );
                                        ?>

                                        <div class="movie-engine-template-reset-wrap me-mt-15 me-pt-15 me-border-top-light me-flex-end">
                                            <a href="<?php echo esc_url(wp_nonce_url(add_query_arg(['reset_template' => $key, 'tab' => 'emails_templates']), 'movie_engine_reset_template')); ?>"
                                                class="movie-engine-btn-reset-single"
                                                onclick="return confirm('<?php echo esc_js(__('Are you sure? This will reset ONLY this template to its official default.', 'cine-movie-engine')); ?>');">
                                                <span class="dashicons dashicons-undo"></span>
                                                <?php esc_html_e('Reset to Default Template', 'cine-movie-engine'); ?>
                                            </a>
                                        </div>
                                    </div>

                                    <input type="hidden" name="movie_engine_email_settings[notifications][<?php echo esc_attr($key); ?>][name]" value="<?php echo esc_attr($notif['name']); ?>">
                                    <input type="hidden" name="movie_engine_email_settings[notifications][<?php echo esc_attr($key); ?>][tags_json]" value="<?php echo esc_attr(json_encode($notif['tags'])); ?>">
                                </div>
                            <?php endforeach; ?>

                            <?php if (! apply_filters('movie_engine_is_pro', false)) : ?>
                                <?php foreach ($pro_templates as $pro_key => $pro_name) : ?>
                                    <div id="panel-<?php echo esc_attr($pro_key); ?>" class="movie-engine-notif-panel">
                                        <div class="me-pro-notice-box" style="margin: 50px auto; max-width: 800px; padding: 0; background: #fff; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,0.08); overflow: hidden; border: 1px solid #e5e5e5; display: flex; align-items: stretch;">
                                            <div style="background: linear-gradient(135deg, #E50914 0%, #b20710 100%); padding: 40px; text-align: center; color: #fff; flex: 0 0 35%; display: flex; flex-direction: column; justify-content: center; align-items: center;">
                                                <div style="background: rgba(255,255,255,0.2); width: 70px; height: 70px; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin-bottom: 20px;">
                                                    <span class="dashicons dashicons-lock" style="font-size: 32px; width: 32px; height: 32px; color: #fff;"></span>
                                                </div>
                                                <h3 style="margin: 0; color: #fff; font-size: 24px; font-weight: 700; line-height: 1.3;"><?php esc_html_e('Unlock Premium Email Notifications', 'cine-movie-engine'); ?></h3>
                                            </div>
                                            <div style="padding: 40px; flex: 1; display: flex; flex-direction: column; justify-content: center;">
                                                <p style="margin-top: 0; margin-bottom: 25px; color: #555; font-size: 16px; line-height: 1.6;">
                                                    <?php esc_html_e('Get access to essential automated emails for your business, including Payment Receipts, Admin Alerts, Referral Notifications, and Payout Updates.', 'cine-movie-engine'); ?>
                                                </p>

                                                <div class="me-pro-features-grid" style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; text-align: left; margin-bottom: 30px;">
                                                    <?php foreach ($pro_templates as $name) : ?>
                                                        <div style="font-size: 14px; color: #333; display: flex; align-items: center;">
                                                            <span class="dashicons dashicons-yes" style="color: #46b450; margin-right: 8px; font-size: 18px;"></span>
                                                            <span style="font-weight: 500;"><?php echo esc_html($name); ?></span>
                                                        </div>
                                                    <?php endforeach; ?>
                                                </div>

                                                <div style="display: flex; align-items: center; gap: 15px; flex-wrap: wrap;">
                                                    <a href="<?php echo esc_url(\MovieEngine\Admin\Upgrade::get_pro_url('pricing')); ?>" target="_blank" style="display: inline-flex; align-items: center; justify-content: center; gap: 10px; background: linear-gradient(135deg, #E50914 0%, #b20710 100%); color: #fff; font-size: 15px; font-weight: 600; padding: 14px 32px; border-radius: 8px; text-decoration: none; transition: all 0.3s ease; box-shadow: 0 4px 15px rgba(229, 9, 20, 0.3);" onmouseover="this.style.transform='translateY(-2px)'; this.style.boxShadow='0 6px 20px rgba(229, 9, 20, 0.4)';" onmouseout="this.style.transform='translateY(0)'; this.style.boxShadow='0 4px 15px rgba(229, 9, 20, 0.3)';">
                                                        <?php esc_html_e('Upgrade to Pro', 'cine-movie-engine'); ?> <span class="dashicons dashicons-arrow-right-alt2" style="font-size: 18px; margin-top: 2px;"></span>
                                                    </a>
                                                    <span style="font-size: 13px; color: #888;">
                                                        <span class="dashicons dashicons-shield" style="font-size: 16px; width: 16px; height: 16px; vertical-align: text-bottom; margin-right: 2px;"></span>
                                                        <?php esc_html_e('30-day money-back guarantee', 'cine-movie-engine'); ?>
                                                    </span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

            </div>

            <div class="movie-engine-form-actions">
                <?php submit_button(__('Save Changes', 'cine-movie-engine'), 'primary me-btn-primary', 'submit', false, ['style' => "background: $admin_ui_color; border-color: $admin_ui_color;"]); ?>
                <?php if ($current_sub_tab === 'general') : ?>
                    <button type="button" id="movie-engine-email-reset-btn" class="movie-engine-reset-section-btn" data-tab="emails_general">
                        <span class="dashicons dashicons-undo"></span>
                        <?php esc_html_e('Reset Section', 'cine-movie-engine'); ?>
                    </button>
                <?php endif; ?>
            </div>
        </div>

        <style>
            :root {
                --me-primary: <?php echo esc_attr($admin_ui_color); ?>;
                --me-border-color: <?php echo esc_attr($border_color); ?>;
            }
        </style>
<?php
    }
}
