<?php
/**
 * Email Settings REST API Controller
 *
 * @package WooNooW
 */

namespace WooNooW\Api;

use WP_REST_Controller;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

class EmailController extends WP_REST_Controller {

	/**
	 * Register routes
	 */
	public function register_routes() {
		$namespace = 'woonoow/v1';

		// Get email settings
		register_rest_route(
			$namespace,
			'/settings/emails',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => array( $this, 'get_settings' ),
				'permission_callback' => array( $this, 'check_permission' ),
			)
		);

		// Toggle email
		register_rest_route(
			$namespace,
			'/settings/emails/(?P<email_id>[a-zA-Z0-9_-]+)/toggle',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $this, 'toggle_email' ),
				'permission_callback' => array( $this, 'check_permission' ),
			)
		);
	}

	/**
	 * Check permission
	 */
	public function check_permission() {
		return current_user_can( 'manage_woocommerce' );
	}

	/**
	 * Get email settings
	 */
	public function get_settings( WP_REST_Request $request ) {
		try {
			$email_settings = array();
			$emails         = \WC()->mailer()->get_emails();

			foreach ( $emails as $email ) {
				$recipient = 'customer';
				
				// Determine recipient type
				if ( strpos( $email->id, 'admin' ) !== false || 
				     strpos( $email->id, 'new_order' ) !== false ||
				     strpos( $email->id, 'cancelled_order' ) !== false ||
				     strpos( $email->id, 'failed_order' ) !== false ) {
					$recipient = 'admin';
				}

				$email_settings[] = array(
					'id'          => $email->id,
					'title'       => $email->get_title(),
					'description' => $email->get_description(),
					'enabled'     => $email->is_enabled() ? 'yes' : 'no',
					'recipient'   => $recipient,
				);
			}

			$settings = array(
				'emails'     => $email_settings,
				'from_name'  => get_option( 'woocommerce_email_from_name' ),
				'from_email' => get_option( 'woocommerce_email_from_address' ),
			);

			return new WP_REST_Response( $settings, 200 );
		} catch ( \Exception $e ) {
			return new WP_REST_Response(
				array(
					'error'   => 'fetch_failed',
					'message' => $e->getMessage(),
				),
				500
			);
		}
	}

	/**
	 * Toggle email
	 */
	public function toggle_email( WP_REST_Request $request ) {
		try {
			$email_id = $request->get_param( 'email_id' );
			$enabled  = rest_sanitize_boolean( $request->get_param( 'enabled' ) );
			$emails   = \WC()->mailer()->get_emails();

			if ( ! isset( $emails[ $email_id ] ) ) {
				return new WP_REST_Response(
					array(
						'error'   => 'not_found',
						'message' => __( 'Email not found', 'woonoow' ),
					),
					404
				);
			}

			$email = $emails[ $email_id ];
			
			// Update email settings
			$email->update_option( 'enabled', $enabled ? 'yes' : 'no' );

			return new WP_REST_Response(
				array(
					'success' => true,
					'enabled' => $enabled,
					'message' => $enabled 
						? __( 'Email enabled', 'woonoow' )
						: __( 'Email disabled', 'woonoow' ),
				),
				200
			);
		} catch ( \Exception $e ) {
			return new WP_REST_Response(
				array(
					'error'   => 'toggle_failed',
					'message' => $e->getMessage(),
				),
				500
			);
		}
	}
}
