<?php
namespace Bookaro\Controller\Admin\Settings;

defined( 'ABSPATH' ) || exit;

final class NotificationsTab extends AbstractSettingsTab
{
    public function get_key(): string
    {
        return 'notifications';
    }

    public function get_section(): array
    {
        return [
            'id'    => 'bookaro_notifications',
            'title' => __( 'Email', 'mybookaro' ),
            'page'  => 'bookaro-booking-settings-notifications',
            'desc'  => __( 'Email notifications, reminders, review invites, and delivery diagnostics.', 'mybookaro' ),
        ];
    }

    public function get_fields(): array
    {
        $templates = $this->context( 'email_templates', [] );
        $subjects = $this->context( 'email_default_subjects', [] );
        $bodies = $this->context( 'email_default_bodies', [] );
        self::register_pro_only_strings();
        $tokens = [
            '{service}',
            '{date}',
            '{date_to}',
            '{date_range}',
            '{provider}',
            '{provider_email}',
            '{provider_phone}',
            '{owner}',
            '{owner_name}',
            '{email}',
            '{customer_phone}',
            '{recipient}',
            '{actor}',
            '{reason}',
            '{old_date}',
            '{old_date_to}',
            '{old_date_range}',
            '{review_link}',
            '{listing_title}',
            '{listing_url}',
            '{booking_id}',
            '{manage_booking_url}',
            '{timezone}',
        ];

        $template_dependencies = [
            'new_booking' => 'notify_new_booking',
            'approved' => 'notify_new_booking',
            'rejected' => 'notify_new_booking',
            'cancelled' => 'notify_new_booking',
            'rescheduled' => 'notify_new_booking',
            'reminder_24h' => 'notify_reminder',
            'reminder_1h' => 'notify_reminder',
            'review_invite' => 'notify_review_invite',
        ];

        $fields = [
            [
                'id' => 'heading_email_delivery',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_notifications',
                'page' => 'bookaro-booking-settings-notifications',
                'args' => [
                    'title' => __( 'Delivery and diagnostics', 'mybookaro' ),
                    'description' => __( 'Configure email delivery and verify your outgoing mail.', 'mybookaro' ),
                    'field_type' => 'heading',
                ],
            ],
            [ 'id'=>'log_email', 'label'=>__( 'Error Log Email', 'mybookaro' ), 'callback'=>'render_email_field', 'section'=>'bookaro_notifications','page'=>'bookaro-booking-settings-notifications','args'=>[ 'default'=>'','description'=>__( 'Optional. Where to send booking error notifications.', 'mybookaro' ),'tooltip'=>__( 'Used for error notifications and system alerts.', 'mybookaro' ) ] ],
            [ 'id'=>'email_test', 'label'=>__( 'Test Email', 'mybookaro' ), 'callback'=>'render_test_email_field','section'=>'bookaro_notifications','page'=>'bookaro-booking-settings-notifications','args'=>[ 'description'=>__( 'Send a test email to confirm outgoing mail is working.', 'mybookaro' ),'tooltip'=>__( 'Sends a single test message to the address you choose.', 'mybookaro' ) ] ],
            [ 'id'=>'log_php_errors', 'label'=>__( 'Capture PHP Errors', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_notifications','page'=>'bookaro-booking-settings-notifications','args'=>[ 'label'=>__( 'Log plugin PHP errors when WP_DEBUG is off', 'mybookaro' ), 'default'=>0,'tooltip'=>__( 'Helpful for debugging in production.', 'mybookaro' ) ] ],
            [ 'id'=>'log_file_enabled', 'label'=>__( 'Admin Log Viewer', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_notifications','page'=>'bookaro-booking-settings-notifications','args'=>[ 'label'=>__( 'Store MyBookaro logs so they can be viewed in WP-Admin', 'mybookaro' ), 'default'=>0,'description'=>__( 'Required to show entries in MyBookaro > Logs.', 'mybookaro' ),'tooltip'=>__( 'Writes log entries to wp-content/uploads/bookaro/bookaro.log for admin viewing and download.', 'mybookaro' ) ] ],
            [
                'id' => 'heading_email_toggles',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_notifications',
                'page' => 'bookaro-booking-settings-notifications',
                'args' => [
                    'title' => __( 'Notification toggles', 'mybookaro' ),
                    'description' => __( 'Choose which email notifications to send, including reminders and review invites.', 'mybookaro' ),
                    'field_type' => 'heading',
                ],
            ],
            [ 'id'=>'notify_new_booking', 'label'=>__( 'New Booking Email', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_notifications','page'=>'bookaro-booking-settings-notifications','args'=>[ 'label'=>__( 'Enable new booking and status emails', 'mybookaro' ),'tooltip'=>__( 'Sends new booking, approved, rejected, cancelled, and rescheduled emails.', 'mybookaro' ) ] ],
            [ 'id'=>'notify_reminder', 'label'=>__( 'Reminder Emails', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_notifications','page'=>'bookaro-booking-settings-notifications','args'=>[ 'label'=>__( 'Enable 24h and 1h reminder emails', 'mybookaro' ),'description'=>__( 'Schedules two customer reminders: 24 hours and 1 hour before booking start.', 'mybookaro' ),'tooltip'=>__( 'Queued when a booking is approved; only future reminder times are scheduled.', 'mybookaro' ),'tooltip_example'=>__( 'If a booking starts tomorrow at 15:00, reminders are queued for 15:00 today and 14:00 tomorrow.', 'mybookaro' ) ] ],
            [ 'id'=>'notify_review_invite','label'=>__( 'Review Invite Email', 'mybookaro' ),'callback'=>'render_checkbox_field','section'=>'bookaro_notifications','page'=>'bookaro-booking-settings-notifications','args'=>[ 'label'=>__( 'Enable review invite emails', 'mybookaro' ),'description'=>__( 'Queues a follow-up email after a booking is completed.', 'mybookaro' ),'tooltip'=>__( 'Sent to customers after completion through a scheduled task.', 'mybookaro' ),'tooltip_example'=>__( 'After a booking is marked completed, MyBookaro queues a review invite email that links to the listing review section.', 'mybookaro' ) ] ],
            [
                'id' => 'note_notification_automation',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_notifications',
                'page' => 'bookaro-booking-settings-notifications',
                'args' => [
                    'title' => __( 'Reminder and review invite scheduling', 'mybookaro' ),
                    'description' => __( 'Reminder emails are queued when a booking is approved and send at 24h and 1h before start (future times only). Review invite emails are queued when a booking is completed. Scheduled delivery runs through WordPress Cron.', 'mybookaro' ),
                    'variant' => 'info',
                    'field_type' => 'note',
                ],
            ],
            [
                'id' => 'heading_email_templates',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_notifications',
                'page' => 'bookaro-booking-settings-notifications',
                'args' => [
                    'title' => __( 'Templates', 'mybookaro' ),
                    'description' => __( 'Customize email subjects and bodies for each notification.', 'mybookaro' ),
                    'field_type' => 'heading',
                ],
            ],
            [
                'id' => 'note_email_tokens',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_notifications',
                'page' => 'bookaro-booking-settings-notifications',
                'args' => [
                    'title' => __( 'Template tokens', 'mybookaro' ),
                    'description' => __( 'Use tokens like {service}, {date}, {provider}, {owner}, {email}, {review_link} inside subjects and bodies.', 'mybookaro' ),
                    'variant' => 'info',
                    'field_type' => 'note',
                ],
            ],
        ];

        foreach ( $templates as $key => $label ) {
            $label_text = is_string( $label ) ? $label : '';
            $depends = [];
            if ( isset( $template_dependencies[ $key ] ) ) {
                $depends = [
                    [ 'field' => $template_dependencies[ $key ], 'operator' => 'truthy' ],
                ];
            }
            $fields[] = [
                'id'       => "template_{$key}",
                'label'    => '',
                'callback' => 'render_email_template_field',
                'section'  => 'bookaro_notifications',
                'page'     => 'bookaro-booking-settings-notifications',
                'args'     => [
                    'template_key' => $key,
                    'template_label' => $label_text,
                    'subject_id' => "{$key}_subject",
                    'body_id' => "{$key}_body",
                    'subject_default' => $subjects[ $key ] ?? '',
                    'body_default' => $bodies[ $key ] ?? '',
                    'tokens' => $tokens,
                    'depends' => $depends,
                    'enabled_id' => "notify_template_{$key}",
                    'enabled_default' => 1,
                    'enabled_label' => __( 'Enable this email template', 'mybookaro' ),
                    'field_type' => 'template',
                ],
            ];
        }

        return $fields;
    }

    /**
     * Pro-only strings kept in Lite for translations.
     */
    private static function register_pro_only_strings(): void
    {
        __( 'Chat Message Emails', 'mybookaro' );
        __( 'Email users about new chat messages', 'mybookaro' );
        __( 'Emails send only when the recipient is not actively reading the chat.', 'mybookaro' );
        __( 'Disabled for MyListing lane because MyListing uses native messaging. Generic Directory and Single Business lanes use MyBookaro booking chat emails.', 'mybookaro' );
        __( 'Chat templates apply to MyBookaro booking chat in Generic Directory and Single Business lanes. MyListing lane keeps native MyListing messaging.', 'mybookaro' );
        __( 'Chat emails are Pro-only and require an active MyBookaro Pro license.', 'mybookaro' );
    }
}
