<?php

namespace Booktics\Admin;

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

use Booktics\Contracts\Hookable_Service_Contract;
use Ens\Core\SDK;

class Notification implements Hookable_Service_Contract {

    /**
     * Register notification
     *
     * @return void
     */
    public function register() {
        if ( class_exists( SDK::class ) ) {
			add_filter( 'notification_sdk_email_body', [ $this, 'wrap_email_body' ], 10, 1 );

            SDK::get_instance()
                ->setup(
                    array(
						'plugin_name'          => 'Booktics',
						'plugin_slug'          => 'booktics',
						'general_prefix'       => 'bt',
						'hook_prefix'          => 'booktics',
						'text_domain'          => 'booktics',
						'admin_script_handler' => 'booktics-dashboard-scripts',
						'sub_menu_filter_hook' => 'booktics_menu',
						'sub_menu_details'     =>
							array(
								'id'         => 'booktics-automation',
								'title'      => __( 'Automation', 'booktics' ),
								'link'       => '/automation',
								'capability' => apply_filters( 'booktics_menu_permission_automation_dashboard', 'manage_options' ),
								'position'   => apply_filters( 'booktics_menu_position_automation', 11 ),
							),
                    )
                )
                ->init();

			add_filter(
                'ens_bt_available_actions', function ( $actions ) {
					$actions = array(
						array(
							'trigger_label'      => __( 'Order Created', 'booktics' ),
							'trigger_value'      => 'order_created',
							'trigger_data'       => array(
								array(
									'label' => __( 'Order No', 'booktics' ),
									'value' => 'order_no',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Order Status', 'booktics' ),
									'value' => 'order_status',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Order Date', 'booktics' ),
									'value' => 'order_date',
									'type'  => 'date',
								),
								array(
									'label' => __( 'Service Name', 'booktics' ),
									'value' => 'service_name',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Team Name', 'booktics' ),
									'value' => 'team_name',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Appointment Date', 'booktics' ),
									'value' => 'appointment_date',
									'type'  => 'date',
								),
								array(
									'label' => __( 'Appointment Status', 'booktics' ),
									'value' => 'appointment_status',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Start Time', 'booktics' ),
									'value' => 'start_time',
									'type'  => 'string',
								),
								array(
									'label' => __( 'End Time', 'booktics' ),
									'value' => 'end_time',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Location Address', 'booktics' ),
									'value' => 'location_address',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Order Total', 'booktics' ),
									'value' => 'order_total',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Customer Name', 'booktics' ),
									'value' => 'customer_name',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Google meet Url', 'booktics' ),
									'value' => 'google_meet_url',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Appointment Reschedule Url', 'booktics' ),
									'value' => 'appointment_reschedule_url',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Appointment Cancel Url', 'booktics' ),
									'value' => 'appointment_cancel_url',
									'type'  => 'string',
								),
								array(
									'label' => __( 'User Email', 'booktics' ),
									'value' => 'user_email',
									'type'  => 'string',
								),
							),
							'delay_dependencies' => array(
								array(
									'label' => 'Order Date',
									'value' => 'order_date',
								),
							),
							'email_receivers'    => array(
								array(
									'label' => __( 'User Email', 'booktics' ),
									'value' => 'user_email',
								),
							),
						),
						array(
							'trigger_label'      => __( 'Appointment Scheduled', 'booktics' ),
							'trigger_value'      => 'appointment_scheduled',
							'trigger_data'       => array(
								array(
									'label' => __( 'Service Name', 'booktics' ),
									'value' => 'appointment_service',
									'type'  => 'string',
								),
                                array(
                                    'label' => __( 'Team Member Name', 'booktics' ),
                                    'value' => 'appointment_team_member',
                                    'type'  => 'string',
                                ),
								array(
									'label' => __( 'Appointment Date', 'booktics' ),
									'value' => 'appointment_date',
									'type'  => 'date',
								),
								array(
									'label' => __( 'Appointment Start Time', 'booktics' ),
									'value' => 'start_time',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Appointment Time', 'booktics' ),
									'value' => 'appointment_time',
									'type'  => 'date',
								),
								array(
									'label' => __( 'Attendee Count', 'booktics' ),
									'value' => 'attendee_count',
									'type'  => 'string',
								),
                                array(
                                    'label' => __( 'Customer Name', 'booktics' ),
                                    'value' => 'customer_name',
                                    'type'  => 'string',
                                ),
								array(
									'label' => __( 'User Email', 'booktics' ),
									'value' => 'user_email',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Google Meet URL', 'booktics' ),
									'value' => 'google_meet_url',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Extra Services', 'booktics' ),
									'value' => 'extra_services',
									'type'  => 'string',
								),
							),
							'delay_dependencies' => array(
								array(
									'label' => __( 'Appointment Time', 'booktics' ),
									'value' => 'before_appointment_time',
							    ),
							),
							'email_receivers'    => array(
								array(
									'label' => __( 'User Email', 'booktics' ),
									'value' => 'user_email',
								),
								array(
									'label' => __( 'Team Member Email', 'booktics' ),
									'value' => 'appointment_team_member_email',
								),
							),
						),
						array(
							'trigger_label'      => __( 'Appointment Rescheduled', 'booktics' ),
							'trigger_value'      => 'appointment_rescheduled',
							'trigger_data'       => array(
								array(
									'label' => __( 'Service Name', 'booktics' ),
									'value' => 'appointment_service',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Date', 'booktics' ),
									'value' => 'date',
									'type'  => 'date',
								),
								array(
									'label' => __( 'Team Member Name', 'booktics' ),
									'value' => 'appointment_team_member',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Start Time', 'booktics' ),
									'value' => 'start_time',
									'type'  => 'string',
								),
								array(
									'label' => __( 'End Time', 'booktics' ),
									'value' => 'end_time',
									'type'  => 'string',
								),
                                array(
                                    'label' => __( 'Customer Name', 'booktics' ),
                                    'value' => 'customer_name',
                                    'type'  => 'string',
                                ),
								array(
									'label' => __( 'User Email', 'booktics' ),
									'value' => 'user_email',
									'type'  => 'string',
								),
							),
							'delay_dependencies' => array(
								array(
									'label' => __( 'Date', 'booktics' ),
									'value' => 'date',
								),
							),
							'email_receivers'    => array(
								array(
									'label' => __( 'User Email', 'booktics' ),
									'value' => 'user_email',
								),
								array(
									'label' => __( 'Team Member Email', 'booktics' ),
									'value' => 'appointment_team_member_email',
								),
							),
						),
						array(
							'trigger_label'      => __( 'Appointment Cancelled', 'booktics' ),
							'trigger_value'      => 'appointment_cancelled',
							'trigger_data'       => array(
								array(
									'label' => __( 'Service Name', 'booktics' ),
									'value' => 'appointment_service',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Date', 'booktics' ),
									'value' => 'date',
									'type'  => 'date',
								),
								array(
									'label' => __( 'Team Member Name', 'booktics' ),
									'value' => 'appointment_team_member',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Start Time', 'booktics' ),
									'value' => 'start_time',
									'type'  => 'string',
								),
								array(
									'label' => __( 'End Time', 'booktics' ),
									'value' => 'end_time',
									'type'  => 'string',
								),
                                array(
                                    'label' => __( 'Customer Name', 'booktics' ),
                                    'value' => 'customer_name',
                                    'type'  => 'string',
                                ),
								array(
									'label' => __( 'User Email', 'booktics' ),
									'value' => 'user_email',
									'type'  => 'string',
								),
							),
							'delay_dependencies' => array(
								array(
									'label' => __( 'Date', 'booktics' ),
									'value' => 'date',
								),
							),
							'email_receivers'    => array(
								array(
									'label' => __( 'User Email', 'booktics' ),
									'value' => 'user_email',
								),
								array(
									'label' => __( 'Team Member Email', 'booktics' ),
									'value' => 'appointment_team_member_email',
								),
							),
						),
						array(
							'trigger_label'      => __( 'Team Member Created', 'booktics' ),
							'trigger_value'      => 'team_member_created',
							'trigger_data'       => array(
								array(
									'label' => __( 'Team Member Name', 'booktics' ),
									'value' => 'team_member_name',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Team Member Email', 'booktics' ),
									'value' => 'team_member_email',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Password Reset URL', 'booktics' ),
									'value' => 'password_reset_url',
									'type'  => 'string',
								),
							),
							'delay_dependencies' => array(),
							'email_receivers'    => array(
								array(
									'label' => __( 'Team Member Email', 'booktics' ),
									'value' => 'team_member_email',
								),
							),
						),
						array(
							'trigger_label'      => __( 'Customer Created', 'booktics' ),
							'trigger_value'      => 'customer_created',
							'trigger_data'       => array(
								array(
									'label' => __( 'Customer First Name', 'booktics' ),
									'value' => 'customer_first_name',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Customer Last Name', 'booktics' ),
									'value' => 'customer_last_name',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Customer Full Name', 'booktics' ),
									'value' => 'customer_full_name',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Customer Phone', 'booktics' ),
									'value' => 'customer_phone',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Customer User Name', 'booktics' ),
									'value' => 'customer_user_name',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Customer Email', 'booktics' ),
									'value' => 'customer_email',
									'type'  => 'string',
								),
								array(
									'label' => __( 'Password Reset URL', 'booktics' ),
									'value' => 'password_reset_url',
									'type'  => 'string',
								),
							),
							'delay_dependencies' => array(),
							'email_receivers'    => array(
								array(
									'label' => __( 'Customer Email', 'booktics' ),
									'value' => 'customer_email',
								),
							),
						),
					);

					return $actions;
				}
            );
        }
    }

	/**
	 * Wrap email body with custom template
	 *
	 * @param string $message
	 * @return string
	 */
	public function wrap_email_body( $message ) {

        $template_path = BOOKTICS_DIR . '/templates/email/team-member-create.html';

        if ( ! file_exists( $template_path ) ) {
            return $message;
        }
        $template      = file_get_contents( $template_path );
        $default_image = get_template_directory_uri() . '/assets/images/placeholder.jpg';
		$plugin_name  = apply_filters(
			'booktics_plugin_name',
			BOOKTICS_PLUGIN_NAME
		);
		$show_powered_by = apply_filters( 'booktics_show_email_powered_by', true );

		// Build powered-by HTML if enabled
		$powered_by_html = '';
		if ( $show_powered_by ) {
			$powered_by_html = '<p class="bkt-powered-by">Powered by ' . esc_html( $plugin_name ) . '</p>';
		}
        $variables     = array(
            '{{MESSAGE}}'         => wp_kses_post( $message ),
            '{{COMPANY_NAME}}'    => booktics_get_option( 'business_name', get_bloginfo( 'name' ) ),
            '{{COMPANY_LOGO}}'    => booktics_get_option( 'business_logo', $default_image ),
            '{{COMPANY_ADDRESS}}' => booktics_get_option( 'business_address', 'California, USA' ),
			'{%powered_by_section%}'  => $powered_by_html,
        );

        return str_replace( array_keys( $variables ), array_values( $variables ), $template );
    }
}
