<?php
/**
 * BookiFlex Notification Extension
 *
 * @package    BookiFlex
 * @subpackage Notification
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

 namespace BookiFlex\Extensions\Notification\Admin;

use BookiFlex\Core\Contract\PluginInterface;
use Carbon_Fields\Container\Container;
use Carbon_Fields\Field;

defined( 'ABSPATH' ) || exit;

class NotificationSettings
{
    protected PluginInterface $plugin;
	// Mail common settings
    public const MAIL_GREETING  = 'bflex__mail__greeting';
    public const MAIL_SIGNATURE = 'bflex__mail__signature';
    public const MAIL_LOGO      = 'bflex__mail__logo';
    public const MAIL_FROM_EMAIL = 'bflex__mail__from_email';
    public const MAIL_FROM_NAME = 'bflex__mail__from_name';
    public const MAIL_BCC       = 'bflex__mail__bcc';

    // Notifications — Customer
    public const CUSTOMER_NEW_SUBJECT         = 'bflex__notifications__customer__new__subject';
    public const CUSTOMER_NEW_ACTIVE          = 'bflex__notifications__customer__new__active';

    public const CUSTOMER_OVERDUE_SUBJECT     = 'bflex__notifications__customer__overdue__subject';
    public const CUSTOMER_OVERDUE_ACTIVE      = 'bflex__notifications__customer__overdue__active';

    public const CUSTOMER_CONFIRMED_SUBJECT   = 'bflex__notifications__customer__confirmed__subject';
    public const CUSTOMER_CONFIRMED_ACTIVE    = 'bflex__notifications__customer__confirmed__active';

    public const CUSTOMER_CANCELLED_SUBJECT   = 'bflex__notifications__customer__cancelled__subject';
    public const CUSTOMER_CANCELLED_ACTIVE    = 'bflex__notifications__customer__cancelled__active';

    // Notifications — Manager
    public const MANAGER_NEW_SUBJECT         = 'bflex__notifications__manager__new__subject';
    public const MANAGER_NEW_ACTIVE          = 'bflex__notifications__manager__new__active';

    public const MANAGER_OVERDUE_SUBJECT     = 'bflex__notifications__manager__overdue__subject';
    public const MANAGER_OVERDUE_ACTIVE      = 'bflex__notifications__manager__overdue__active';

    public const MANAGER_CANCELLED_SUBJECT   = 'bflex__notifications__manager__cancelled__subject';
    public const MANAGER_CANCELLED_ACTIVE    = 'bflex__notifications__manager__cancelled__active';

    public const MANAGER_WAITING_CONFIRMATION_SUBJECT   = 'bflex__notifications__manager__waiting_confirmation__subject';
    public const MANAGER_WAITING_CONFIRMATION_ACTIVE    = 'bflex__notifications__manager__waiting_confirmation__active';

//    public function register(PluginInterface $plugin): void
//    {
//        $this->plugin = $plugin;
//        $this->plugin->addAction('carbon_fields_register_fields', [$this, 'registerFields']);
//    }

	public function getTabName(): string
	{
		return __('Notifications', 'bookiflex');
	}

	private function previewLink(string $event, string $recipient, string $name): \Carbon_Fields\Field\HTML_Field
	{
		$url = add_query_arg([
			'event'     => $event,
			'recipient' => $recipient,
			'_wpnonce'  => wp_create_nonce('wp_rest'),
		], rest_url('bflex/v1/notifications/test'));

		return Field::make('html', $name)
                    ->set_width(20)
		            ->set_html(sprintf(
			            '<p style="margin:27px 0 0"><a href="%s" target="_blank" rel="noopener">%s</a></p>',
			            esc_url($url),
			            esc_html(__('Preview email →', 'bookiflex'))
		            ));
	}

	public function __invoke(): array
	{
		return [
			Field::make('separator', 'bflex__mail__separator',
				__('General Settings', 'bookiflex')),
			Field::make('text', self::MAIL_FROM_EMAIL,
				__('From Email', 'bookiflex'))
			     ->set_attribute('type', 'email')
                 ->set_width(50)
			     ->set_help_text(__('Email address that will appear as the sender. Leave empty to use WordPress admin email.', 'bookiflex')),
			Field::make('text', self::MAIL_FROM_NAME,
				__('From Name', 'bookiflex'))
                 ->set_width(50)
			     ->set_help_text(__('Name that will appear as the sender. Leave empty to use site name.', 'bookiflex')),
//			Field::make('text', self::MAIL_BCC,
//				__('BCC Email (Optional)', 'bookiflex'))
//			     ->set_attribute('type', 'email')
//			     ->set_help_text(__('Blind carbon copy address for all outgoing emails. Leave empty to disable.', 'bookiflex')),
			Field::make('rich_text', self::MAIL_GREETING,
				__('Greeting', 'bookiflex'))
			     ->set_help_text(__('Custom greeting shown at the top of every email. Leave empty to use the default greeting.', 'bookiflex'))
			     ->set_settings([
				     'media_buttons' => false,
				     'tinymce'       => [
					     'toolbar1'      => 'bold italic underline | bullist numlist | removeformat',
					     'toolbar2'      => '',
					     'block_formats' => 'Paragraph=p',
				     ],
				     'quicktags'     => false,
			     ]),
			Field::make('rich_text', self::MAIL_SIGNATURE,
				__('Signature', 'bookiflex'))
			     ->set_help_text(__('Text block shown at the bottom of each email (e.g., your company info, phone, disclaimer). Supports HTML or plain text.', 'bookiflex'))
			     ->set_settings([
				     'media_buttons' => false,
				     'tinymce'       => [
					     'toolbar1'      => 'bold italic underline | bullist numlist | removeformat',
					     'toolbar2'      => '',
					     'block_formats' => 'Paragraph=p',
				     ],
				     'quicktags'     => false,
			     ]),
			Field::make('image', self::MAIL_LOGO,
				__('Logo', 'bookiflex'))
			     ->set_value_type('url')
			     ->set_help_text(__('Upload the logo to be displayed at the top of every email.', 'bookiflex')),

			Field::make('separator', 'bflex__notifications__customer__new__separator',
				__('Guest Emails', 'bookiflex')),
			Field::make('html', 'bflex__notifications__customer__description')
			     ->set_html(sprintf('<p>%s</br>%s; %s; %s</p>',
				     __('Triggered emails sent to the customer. Available placeholders in the subject:', 'bookiflex'),
				     __('<strong>{{number}}</strong> - number of reservation', 'bookiflex'),
				     __('<strong>{{firstName}}</strong> - first name of guest', 'bookiflex'),
				     __('<strong>{{lastName}}</strong> - last name of guest', 'bookiflex'),
			     )),
			Field::make('select', self::CUSTOMER_NEW_ACTIVE,
				__('Sent after the reservation is made', 'bookiflex'))
			     ->set_options([
				     '0' => __('No', 'bookiflex'),
				     '1' => __('Yes', 'bookiflex')
			     ])
			     ->set_width(40),
			Field::make('text', self::CUSTOMER_NEW_SUBJECT,
				__('Subject', 'bookiflex'))
			     ->set_width(40),
			$this->previewLink('new_reservation', 'client', 'bflex__preview__customer_new'),

			Field::make('select', self::CUSTOMER_OVERDUE_ACTIVE,
				__('Sent when payment or confirmation is overdue', 'bookiflex'))
			     ->set_options([
				     '0' => __('No', 'bookiflex'),
				     '1' => __('Yes', 'bookiflex')
			     ])
			     ->set_width(40),
			Field::make('text', self::CUSTOMER_OVERDUE_SUBJECT,
				__('Subject', 'bookiflex'))
			     ->set_width(40),
			$this->previewLink('reservation_overdue', 'client', 'bflex__preview__customer_overdue'),

			Field::make('select', self::CUSTOMER_CONFIRMED_ACTIVE,
				__('Sent when reservation moves to Confirmed status', 'bookiflex'))
			     ->set_options([
				     '0' => __('No', 'bookiflex'),
				     '1' => __('Yes', 'bookiflex')
			     ])
			     ->set_width(40),
			Field::make('text', self::CUSTOMER_CONFIRMED_SUBJECT,
				__('Subject', 'bookiflex'))
			     ->set_width(40),
			$this->previewLink('reservation_confirmed', 'client', 'bflex__preview__customer_confirmed'),

			Field::make('select', self::CUSTOMER_CANCELLED_ACTIVE,
				__('Sent when a booking is cancelled by admin or customer', 'bookiflex'))
			     ->set_options([
				     '0' => __('No', 'bookiflex'),
				     '1' => __('Yes', 'bookiflex')
			     ])
			     ->set_width(40),
			Field::make('text', self::CUSTOMER_CANCELLED_SUBJECT,
				__('Subject', 'bookiflex'))
			     ->set_width(40),
			$this->previewLink('reservation_cancelled', 'client', 'bflex__preview__customer_cancelled'),



			Field::make('separator', 'bflex__notifications__manager__new__separator',
				__('Manager Emails', 'bookiflex')),
			Field::make('html', 'bflex__notifications__manager__description')
			     ->set_html(sprintf('<p>%s</br>%s; %s; %s</p>',
				     __('Notifications sent to the property manager or admin. Available placeholders in the subject:', 'bookiflex'),
				     __('<strong>{{number}}</strong> - number of reservation', 'bookiflex'),
				     __('<strong>{{firstName}}</strong> - first name of guest', 'bookiflex'),
				     __('<strong>{{lastName}}</strong> - last name of guest', 'bookiflex'),
			     )),
			Field::make('select', self::MANAGER_NEW_ACTIVE,
				__('Sent when a new reservation is made', 'bookiflex'))
			     ->set_options([
				     '0' => __('No', 'bookiflex'),
				     '1' => __('Yes', 'bookiflex')
			     ])
			     ->set_width(40),
			Field::make('text', self::MANAGER_NEW_SUBJECT,
				__('Subject', 'bookiflex'))
			     ->set_width(40),
			$this->previewLink('new_reservation', 'manager', 'bflex__preview__manager_new'),

			Field::make('select', self::MANAGER_OVERDUE_ACTIVE,
				__('Sent when a reservation becomes overdue', 'bookiflex'))
			     ->set_options([
				     '0' => __('No', 'bookiflex'),
				     '1' => __('Yes', 'bookiflex')
			     ])
			     ->set_width(40),
			Field::make('text', self::MANAGER_OVERDUE_SUBJECT,
				__('Subject', 'bookiflex'))
			     ->set_width(40),
			$this->previewLink('reservation_overdue', 'manager', 'bflex__preview__manager_overdue'),

			Field::make('select', self::MANAGER_CANCELLED_ACTIVE,
				__('Sent when a customer cancels their reservation', 'bookiflex'))
			     ->set_options([
				     '0' => __('No', 'bookiflex'),
				     '1' => __('Yes', 'bookiflex')
			     ])
			     ->set_width(40),
			Field::make('text', self::MANAGER_CANCELLED_SUBJECT,
				__('Subject', 'bookiflex'))
			     ->set_width(40),
			$this->previewLink('reservation_cancelled', 'manager', 'bflex__preview__manager_cancelled'),

			Field::make('select', self::MANAGER_WAITING_CONFIRMATION_ACTIVE,
				__('Sent when a booking requires manual approval', 'bookiflex'))
			     ->set_options([
				     '0' => __('No', 'bookiflex'),
				     '1' => __('Yes', 'bookiflex')
			     ])
			     ->set_width(40),
			Field::make('text', self::MANAGER_WAITING_CONFIRMATION_SUBJECT,
				__('Subject', 'bookiflex'))
			     ->set_width(40),
			$this->previewLink('waiting_confirmation', 'manager', 'bflex__preview__manager_waiting_confirmation'),
		];
	}

//    public function registerFields(): void
//    {
//        Container::make('theme_options', 'bflex__notifications',
//            __('Notifications', 'bookiflex'))
//            ->set_layout('tabbed-vertical')
//            ->set_page_parent('bookiflex')
//            ->set_page_file( 'bookiflex-notifications' )
//            ->add_tab(__('General', 'bookiflex'), [
//
//            ])
//            ->add_tab(__('Guest Emails', 'bookiflex'), [
//
//            ])
//            ->add_tab(__('Manager Emails', 'bookiflex'), [
//
//            ]);
//    }
}
