<?php

namespace SugarCalendarBookings\Admin\Settings;

use SugarCalendarBookings\Notifications\Emails\EmailService;
use SugarCalendarBookings\Options;
use SugarCalendarBookings\Plugin;
use SugarCalendarBookings\Support\Admin\AbstractTabPage;
use SugarCalendarBookings\Support\Admin\Notices;
use SugarCalendarBookings\Support\DataProcessing\InputProcessor;
use SugarCalendarBookings\Support\Helpers\UI;
use SugarCalendarBookings\Admin\Settings\EmailEditPage;

/**
 * Emails Settings tab.
 *
 * @since 1.0.0
 */
class EmailsSettingsTab extends AbstractTabPage {

	/**
	 * Page title.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_title() {

		return esc_html__( 'Emails', 'sugar-calendar-bookings-scheduling-appointments-lite' );
	}

	/**
	 * Page slug.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_slug() {

		return 'settings';
	}

	/**
	 * Page view slug.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_view_slug() {

		return 'emails';
	}

	/**
	 * Priority in menu position.
	 *
	 * @since 1.0.0
	 *
	 * @return int
	 */
	public static function get_priority() {

		return 40;
	}

	/**
	 * Get default email address from WordPress settings.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	private function get_default_email_address() {

		return get_option( 'admin_email', '' );
	}

	/**
	 * Get default email name from WordPress settings.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	private function get_default_email_name() {

		return get_option( 'blogname', '' );
	}

	/**
	 * Display page.
	 *
	 * @since 1.0.0
	 */
	public function display_content() {

		ob_start();

		// Display Email Sender heading.
		UI::heading(
			[
				'title' => esc_html__( 'Email Sender', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			]
		);

		$options = Plugin::get( Options::class );

		// From Email Address.
		UI::text_input(
			[
				'id'          => 'emails_from_email',
				'name'        => [ 'emails', 'from_email' ],
				'value'       => $options->get( 'emails.from_email', $this->get_default_email_address() ),
				'label'       => esc_html__( 'From Email Address', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'description' => esc_html__( 'The email address that will be used as the sender for all booking-related emails.', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'type'        => 'email',
			]
		);

		// From Email Name.
		UI::text_input(
			[
				'id'          => 'emails_from_name',
				'name'        => [ 'emails', 'from_name' ],
				'value'       => $options->get( 'emails.from_name', $this->get_default_email_name() ),
				'label'       => esc_html__( 'From Email Name', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'description' => esc_html__( 'The name that will be displayed as the sender for all booking-related emails.', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			]
		);

		UI::heading(
			[
				'title' => esc_html__( 'Configure Emails', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			]
		);

		// Display email notifications list.
		$this->display_email_notifications( $options );

		UI::form_wrapper( ob_get_clean() );
	}

	/**
	 * Display email notifications list with sections.
	 *
	 * @since 1.0.0
	 *
	 * @param Options $options Plugin options instance.
	 */
	private function display_email_notifications( $options ) {

		$email_notifications = Plugin::get( EmailService::class )->get_email_notifications();

		?>
		<div class="sugar-calendar-bookings-email-notifications-list">
			<?php foreach ( $email_notifications as $email_type => $email_data ) : ?>
				<?php
				$toggle_id = 'emails_' . $email_type . '_enabled';
				?>
				<div class="sugar-calendar-bookings-email-notification-section">
					<!-- Column 1: Title & Description -->
					<div class="email-notification-info">
						<div class="email-notification-title">
							<?php echo esc_html( $email_data['label'] ); ?>
						</div>
						<div class="email-notification-description">
							<?php echo esc_html( $email_data['description'] ); ?>
						</div>
					</div>

					<!-- Column 2: Edit Button -->
					<div class="email-notification-actions">
						<a
							href="<?php echo esc_url( add_query_arg( [ 'email_type' => $email_type ], EmailEditPage::get_url() ) ); ?>"
							class="email-edit-btn"
						>
							<?php esc_html_e( 'Edit', 'sugar-calendar-bookings-scheduling-appointments-lite' ); ?>
						</a>
					</div>

					<!-- Column 3: Toggle -->
					<div class="email-notification-toggle">
						<?php
						UI::toggle_control(
							[
								'id'    => $toggle_id,
								'name'  => [ 'emails', $email_type, 'enabled' ],
								'value' => $options->get( "emails.{$email_type}.enabled", true ),
								'label' => '',
							]
						);
						?>
					</div>
				</div>
			<?php endforeach; ?>
		</div>
		<?php
	}

	/**
	 * Get post data processing rules.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_post_data_processing_rules() {

		$rules = [
			'emails.from_email' => 'required|email',
			'emails.from_name'  => 'required|string|max:255',
		];

		// Dynamically add validation rules for each email notification.
		$email_types = array_keys( Plugin::get( EmailService::class )->get_email_notifications() );

		foreach ( $email_types as $email_type ) {
			$rules[ "emails.{$email_type}.enabled" ] = 'required|form_boolean';
		}

		return $rules;
	}

	/**
	 * Handle POST requests.
	 *
	 * @since 1.0.0
	 *
	 * @param array $post_data Post data.
	 */
	public function handle_post( $post_data = [] ) {

		Plugin::get( Options::class )->update( $post_data );

		Plugin::get( Notices::class )->success( __( 'Settings successfully saved.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
	}
}
