<?php
/**
 * Onboarding Tour REST API Controller.
 *
 * @package Ugcify\Admin\API
 */

namespace Ugcify\Admin\API;

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

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class OnboardingTourAPI
 */
class OnboardingTourAPI extends WP_REST_Controller {

	private const OPTION_NAME = 'ugcify_onboarding_tour_status';

	/**
	 * Namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'ugcify/v1';

	/**
	 * Rest base.
	 *
	 * @var string
	 */
	protected $rest_base = 'onboarding-tour';

	/**
	 * Register routes.
	 */
	public function register_routes() {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/status',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'status' ),
					'permission_callback' => array( $this, 'check_permission' ),
				),
			)
		);

		foreach ( array( 'complete', 'skip', 'restart' ) as $action ) {
			register_rest_route(
				$this->namespace,
				'/' . $this->rest_base . '/' . $action,
				array(
					array(
						'methods'             => WP_REST_Server::CREATABLE,
						'callback'            => array( $this, $action ),
						'permission_callback' => array( $this, 'check_permission' ),
					),
				)
			);
		}
	}

	/**
	 * Permission callback — requires manage_options.
	 *
	 * @return bool|WP_Error
	 */
	public function check_permission() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return new WP_Error(
				'rest_forbidden',
				__( 'Sorry, you are not allowed to access this resource.', 'ugcify' ),
				array( 'status' => 403 )
			);
		}
		return true;
	}

	/**
	 * Get current tour status.
	 *
	 * @return WP_REST_Response
	 */
	public function status() {
		return rest_ensure_response(
			array(
				'success' => true,
				'data'    => self::get_status(),
			)
		);
	}

	/**
	 * Mark tour as completed.
	 *
	 * @return WP_REST_Response
	 */
	public function complete() {
		$status = $this->save_status(
			array(
				'status'       => 'completed',
				'completed_at' => current_time( 'mysql' ),
				'version'      => defined( 'UGCIFY_VERSION' ) ? UGCIFY_VERSION : '',
			)
		);

		return rest_ensure_response(
			array(
				'success' => true,
				'data'    => $status,
			)
		);
	}

	/**
	 * Mark tour as skipped.
	 *
	 * @return WP_REST_Response
	 */
	public function skip() {
		$status = $this->save_status(
			array(
				'status'     => 'skipped',
				'skipped_at' => current_time( 'mysql' ),
				'version'    => defined( 'UGCIFY_VERSION' ) ? UGCIFY_VERSION : '',
			)
		);

		return rest_ensure_response(
			array(
				'success' => true,
				'data'    => $status,
			)
		);
	}

	/**
	 * Restart tour.
	 *
	 * @return WP_REST_Response
	 */
	public function restart() {
		$status = $this->save_status(
			array(
				'status'  => 'not_started',
				'version' => defined( 'UGCIFY_VERSION' ) ? UGCIFY_VERSION : '',
			)
		);

		return rest_ensure_response(
			array(
				'success' => true,
				'data'    => $status,
			)
		);
	}

	/**
	 * Get normalized tour status.
	 *
	 * @return array
	 */
	public static function get_status(): array {
		$status = get_option( self::OPTION_NAME, array() );
		$status = is_array( $status ) ? $status : array();

		if ( ! in_array( $status['status'] ?? '', array( 'not_started', 'completed', 'skipped' ), true ) ) {
			$status['status'] = 'not_started';
		}

		if ( empty( $status['version'] ) && defined( 'UGCIFY_VERSION' ) ) {
			$status['version'] = UGCIFY_VERSION;
		}

		return $status;
	}

	/**
	 * Save normalized tour status.
	 *
	 * @param array $status Status payload.
	 * @return array
	 */
	private function save_status( array $status ): array {
		update_option( self::OPTION_NAME, $status );

		return self::get_status();
	}
}
