<?php
/**
 * Cookies class
 *
 * Handles cookie-related REST API endpoints for the SureCookie plugin.
 * Delegates business logic to CookieService.
 *
 * @package SureCookie\Inc\API
 */

namespace SureCookie\Inc\API;

use SureCookie\Inc\Functions\SendJson;
use SureCookie\Inc\Services\CookieService;
use SureCookie\Inc\Traits\GetInstance;
use WP_REST_Server;

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

/**
 * Class Cookies
 *
 * Handles cookie-related REST API endpoints.
 */
class Cookies extends Base {
	use GetInstance;

	/**
	 * Route Get Logs
	 */
	protected const GET_SCANNED_COOKIES = '/cookies/scanned-results';

	/**
	 * Route Get Custom Cookies
	 */
	protected const GET_CUSTOM_COOKIES = '/cookies/custom-cookies';

	/**
	 * Route Create Custom Cookie
	 */
	protected const CREATE_CUSTOM_COOKIE = '/cookies/custom-cookie';

	/**
	 * Route Update Custom Cookie
	 */
	protected const UPDATE_CUSTOM_COOKIE = '/cookies/custom-cookie/(?P<id>[a-zA-Z0-9_-]+)';

	/**
	 * Route Delete Custom Cookie
	 */
	protected const DELETE_CUSTOM_COOKIE = '/cookies/custom-cookie/(?P<id>[a-zA-Z0-9_-]+)';

	/**
	 * Route Update Scanned Cookie Category
	 */
	protected const UPDATE_SCANNED_COOKIE_CATEGORY = '/cookies/scanned-cookie/category';

	/**
	 * Register API routes.
	 *
	 * @since 0.0.1
	 * @return void
	 */
	public function register_routes(): void {
		register_rest_route(
			$this->get_api_namespace(),
			self::GET_SCANNED_COOKIES,
			[
				'methods'             => WP_REST_Server::READABLE, // GET method.
				'callback'            => [ $this, 'get_scanned_cookies' ],
				'permission_callback' => [ $this, 'validate_permission' ],
			]
		);

		register_rest_route(
			$this->get_api_namespace(),
			self::GET_CUSTOM_COOKIES,
			[
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => [ $this, 'get_custom_cookies' ],
				'permission_callback' => [ $this, 'validate_permission' ],
			]
		);

		register_rest_route(
			$this->get_api_namespace(),
			self::CREATE_CUSTOM_COOKIE,
			[
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => [ $this, 'create_custom_cookie' ],
				'permission_callback' => [ $this, 'validate_permission' ],
				'args'                => [
					'name'        => [
						'required' => true,
						'type'     => 'string',
					],
					'category'    => [
						'required' => true,
						'type'     => 'string',
					],
					'description' => [
						'required' => false,
						'type'     => 'string',
						'default'  => '',
					],
					'duration'    => [
						'required' => false,
						'type'     => 'string',
						'default'  => '',
					],
					'provider'    => [
						'required' => false,
						'type'     => 'string',
						'default'  => '',
					],
					'purpose'     => [
						'required' => false,
						'type'     => 'string',
						'default'  => '',
					],
					'domain'      => [
						'required' => false,
						'type'     => 'string',
						'default'  => '',
					],
					'type'        => [
						'required' => false,
						'type'     => 'string',
						'default'  => 'custom',
					],
				],
			]
		);

		register_rest_route(
			$this->get_api_namespace(),
			self::UPDATE_CUSTOM_COOKIE,
			[
				'methods'             => WP_REST_Server::EDITABLE,
				'callback'            => [ $this, 'update_custom_cookie' ],
				'permission_callback' => [ $this, 'validate_permission' ],
				'args'                => [
					'id'          => [
						'required' => true,
						'type'     => 'string',
					],
					'name'        => [
						'required' => false,
						'type'     => 'string',
					],
					'category'    => [
						'required' => false,
						'type'     => 'string',
					],
					'description' => [
						'required' => false,
						'type'     => 'string',
					],
					'duration'    => [
						'required' => false,
						'type'     => 'string',
					],
					'provider'    => [
						'required' => false,
						'type'     => 'string',
					],
					'purpose'     => [
						'required' => false,
						'type'     => 'string',
					],
					'domain'      => [
						'required' => false,
						'type'     => 'string',
					],
				],
			]
		);

		register_rest_route(
			$this->get_api_namespace(),
			self::DELETE_CUSTOM_COOKIE,
			[
				'methods'             => WP_REST_Server::DELETABLE,
				'callback'            => [ $this, 'delete_custom_cookie' ],
				'permission_callback' => [ $this, 'validate_permission' ],
				'args'                => [
					'id' => [
						'required' => true,
						'type'     => 'string',
					],
				],
			]
		);

		register_rest_route(
			$this->get_api_namespace(),
			self::UPDATE_SCANNED_COOKIE_CATEGORY,
			[
				'methods'             => WP_REST_Server::EDITABLE,
				'callback'            => [ $this, 'update_scanned_cookie_category' ],
				'permission_callback' => [ $this, 'validate_permission' ],
				'args'                => [
					'cookie_name'      => [
						'required' => true,
						'type'     => 'string',
					],
					'current_category' => [
						'required' => true,
						'type'     => 'string',
					],
					'new_category'     => [
						'required' => true,
						'type'     => 'string',
					],
				],
			]
		);
	}

	/**
	 * Get scanned cookies.
	 *
	 * @param \WP_REST_Request<array<string, mixed>> $request Full data about the request.
	 * @return void
	 */
	public function get_scanned_cookies( $request ): void {
		$service = new CookieService();
		$result  = $service->get_scanned_cookies();

		$result['success'] ? SendJson::success( $result ) : SendJson::error( $result );
	}

	/**
	 * Get all custom cookies.
	 *
	 * @param \WP_REST_Request<array<string, mixed>> $request Full data about the request.
	 * @since 0.0.1
	 * @return void
	 */
	public function get_custom_cookies( $request ): void {
		$service = new CookieService();
		$result  = $service->get_custom_cookies();

		SendJson::success( $result );
	}

	/**
	 * Create a new custom cookie.
	 *
	 * @param \WP_REST_Request<array<string, mixed>> $request Full data about the request.
	 * @since 0.0.1
	 * @return void
	 */
	public function create_custom_cookie( $request ): void {
		try {
			$service = new CookieService();
			$result  = $service->create_custom_cookie( $request->get_params() );

			$result['success'] ? SendJson::success( $result ) : SendJson::error( $result );
		} catch ( \Exception $e ) {
			SendJson::error(
				[
					'message' => __( 'Failed to create custom cookie: ', 'surecookie' ) . $e->getMessage(),
				]
			);
		}
	}

	/**
	 * Update an existing custom cookie.
	 *
	 * @param \WP_REST_Request<array<string, mixed>> $request Full data about the request.
	 * @since 0.0.1
	 * @return void
	 */
	public function update_custom_cookie( $request ): void {
		try {
			$cookie_id = sanitize_text_field( $request->get_param( 'id' ) );
			$service   = new CookieService();
			$result    = $service->update_custom_cookie( $cookie_id, $request->get_params() );

			$result['success'] ? SendJson::success( $result ) : SendJson::error( $result );
		} catch ( \Exception $e ) {
			SendJson::error(
				[ 'message' => __( 'Failed to update custom cookie: ', 'surecookie' ) . $e->getMessage() ]
			);
		}
	}

	/**
	 * Delete a custom cookie.
	 *
	 * @param \WP_REST_Request<array<string, mixed>> $request Full data about the request.
	 * @since 0.0.1
	 * @return void
	 */
	public function delete_custom_cookie( $request ): void {
		try {
			$cookie_id = sanitize_text_field( $request->get_param( 'id' ) );
			$service   = new CookieService();
			$result    = $service->delete_custom_cookie( $cookie_id );

			$result['success'] ? SendJson::success( $result ) : SendJson::error( $result );
		} catch ( \Exception $e ) {
			SendJson::error(
				[ 'message' => __( 'Failed to delete custom cookie: ', 'surecookie' ) . $e->getMessage() ]
			);
		}
	}

	/**
	 * Update category of a scanned cookie.
	 *
	 * Moves a cookie from one category to another in the scanned cookies option.
	 *
	 * @param \WP_REST_Request<array<string, mixed>> $request Full data about the request.
	 * @since 0.0.1
	 * @return void
	 */
	public function update_scanned_cookie_category( $request ): void {
		try {
			$cookie_name      = sanitize_text_field( $request->get_param( 'cookie_name' ) );
			$current_category = sanitize_text_field( $request->get_param( 'current_category' ) );
			$new_category     = sanitize_text_field( $request->get_param( 'new_category' ) );

			$service = new CookieService();
			$result  = $service->update_scanned_cookie_category( $cookie_name, $current_category, $new_category );

			$result['success'] ? SendJson::success( $result ) : SendJson::error( $result );
		} catch ( \Exception $e ) {
			SendJson::error(
				[ 'message' => __( 'Failed to update cookie category: ', 'surecookie' ) . $e->getMessage() ]
			);
		}
	}
}
