<?php

namespace SugarCalendarBookings\Admin\Settings;

use SugarCalendarBookings\Notifications\Emails\EmailDefaults;
use SugarCalendarBookings\Support\Ajax\AbstractController;
use SugarCalendarBookings\Support\Helpers\Date;

/**
 * Settings controller.
 *
 * @since 1.0.0
 */
class SettingsController extends AbstractController {

	/**
	 * Handle datetime format request.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function format_date_time() {

		$data = $this->get_input_data(
			[
				'format' => 'required|string',
			]
		);

		wp_send_json_success( wp_date( $data['format'], null, Date::get_default_timezone() ) );
	}

	/**
	 * Handle get email defaults request.
	 *
	 * @since 1.4.0
	 */
	public function get_email_defaults() {

		$data = $this->get_input_data(
			[
				'email_type' => 'required|string',
			]
		);

		$defaults   = EmailDefaults::to_array();
		$email_type = $data['email_type'];

		if (
			! isset( $defaults[ $email_type ] )
			|| ! is_array( $defaults[ $email_type ] )
			|| ! isset( $defaults[ $email_type ]['subject'], $defaults[ $email_type ]['content'] )
		) {
			wp_send_json_error(
				esc_html__( 'Invalid email type.', 'sugar-calendar-bookings-scheduling-appointments-lite' )
			);
		}

		wp_send_json_success(
			[
				'subject' => $defaults[ $email_type ]['subject'],
				'content' => $defaults[ $email_type ]['content'],
			]
		);
	}
}
