<?php
/**
 * ============================================
 * FILE: includes/Emails/Employee/AppointmentReminder.php
 * Employee Appointment Reminder Email
 * ============================================
 */

namespace RadiusTheme\RadiusBooking\Emails\Employee;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
use RadiusTheme\RadiusBooking\Abstracts\BaseEmail;
use RadiusTheme\RadiusBooking\Models\Appointment;
use RadiusTheme\RadiusBooking\Resources\AppointmentResource;

/**
 * Class AppointmentReminder
 *
 * Handles the "Appointment Reminder" email notification sent to employees
 * before their scheduled appointment.
 *
 * @package RadiusTheme\RadiusBooking\Emails\Employee
 */
class AppointmentReminder extends BaseEmail {

	/**
	 * Constructor to initialize the email properties.
	 */
	public function __construct() {
		$this->id             = 'employee_appointment_reminder';
		$this->title          = __( 'Appointment Reminder - Employee', 'radius-booking' );
		$this->description    = __( 'Sent to employees as a reminder before their appointment', 'radius-booking' );
		$this->tooltip        = __( 'This email is sent automatically before an appointment as a reminder to the staff member', 'radius-booking' );
		$this->recipient_type = 'employee';
		$this->template_html  = 'employee/appointment-reminder.php';
		$this->status         = 'reminder';

		parent::__construct();
	}

	/**
	 * Get the unique identifier for the email.
	 *
	 * @return string
	 */
	public function get_id() {
		return $this->id;
	}

	/**
	 * Get the trigger action for the email.
	 *
	 * Reminders are not triggered by a WordPress action hook.
	 * They are scheduled via Action Scheduler by the ReminderService.
	 *
	 * @return string
	 */
	public function get_trigger_action() {
		return '';
	}

	/**
	 * Get the default email notification settings for appointment reminder.
	 *
	 * @return array The default email settings.
	 */
	public function get_default_settings() {
		return array(
			'enabled'            => true,
			'subject'            => __( 'Reminder: Upcoming Appointment - {service_name}', 'radius-booking' ),
			'heading'            => __( 'Appointment Reminder', 'radius-booking' ),
			'additional_content' => __( "Hello {staff_name},\n\nThis is a reminder about an upcoming appointment on your schedule.\n\nPlease review the details below and prepare accordingly.", 'radius-booking' ),
			'footer_text'        => __( 'Please ensure you are ready for this appointment. Contact the customer if needed.', 'radius-booking' ),
			'button_text'        => '',
			'button_url'         => '',
			'use_queue'          => false,
			'send_reminder'      => true,
			'reminder_minutes'   => 30,
			'bcc_emails'         => array(),
			'attachments'        => array(),
			'include_calendar'   => true,
		);
	}

	/**
	 * Prepare the email data for sending.
	 *
	 * @param array $args The arguments passed to the email trigger.
	 * @return array|false The prepared email data or false on failure.
	 */
	protected function prepare_email_data( $args ) {
		if ( empty( $args[0] ) ) {
			return false;
		}
		$appointment_id = $args[0];
		$booking        = $this->get_booking( $appointment_id );

		if ( ! $booking ) {
			return false;
		}

		$to           = isset( $booking['employee_email'] ) ? $booking['employee_email'] : get_option( 'admin_email' );
		$customer     = $booking['customer']->customer_info;
		$get_currency = get_rtrb_currency_symbol();

		$merge_tags = array(
			'staff_name'     => $booking['employee_name'],
			'customer_name'  => $customer['name'],
			'customer_email' => $customer['email'],
			'customer_phone' => $customer['phone'],
			'service_name'   => $booking['service_name'],
			'booking_id'     => $booking['customer']->id,
			'booking_date'   => date_i18n( 'l, F j, Y', strtotime( $booking['appointment_date'] ) ),
			'time'           => date_i18n( 'g:i A', strtotime( $booking['start_time'] ) ),
			'duration'       => $booking['customer']->duration / 60 . ' minutes',
			'location'       => ! empty( $booking['location_info']['address'] ) ? $booking['location_info']['address'] : '',
			'price'          => $get_currency . number_format( ! empty( $booking['dynamic_pricing']['final_price'] ) ? $booking['dynamic_pricing']['final_price'] : $booking['customer']->price, 2 ),
			'status'         => $booking['status'],
			'notes'          => $booking['notes'],
			'booking_url'    => admin_url( 'admin.php?page=radius-bookings&booking_id=' . $booking['customer']->id ),
		);

		return array(
			'to'          => $to,
			'cc'          => array(),
			'bcc'         => isset( $this->settings['bcc_emails'] ) ? $this->settings['bcc_emails'] : array(),
			'merge_tags'  => $merge_tags,
			'data'        => $booking,
			'attachments' => array(),
		);
	}

	/**
	 * Retrieve and transform the booking data for the given appointment ID.
	 *
	 * @param int|null $appointment_id The appointment ID.
	 * @return array|null The transformed booking data or null if not found.
	 */
	private function get_booking( $appointment_id = null ) {
		$appointment = Appointment::find( $appointment_id );
		return ( new AppointmentResource() )->transform( $appointment );
	}
}
