<?php

namespace GemBoards\Ajax;

use GemBoards\Classes\AbstractAjaxHandler;
use GemBoards\Classes\Exception\GemboardsException;
use GemBoards\Collections\BookmarkCollection;
use GemBoards\Entities\Bookmark as BookmarkEntity;
use GemBoards\Utils\Helper;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Bookmark extends AbstractAjaxHandler {

	public function __construct() {
		$this->actions = [
			'create_bookmark' => [
				'callback'   => [ $this, 'create_bookmark' ],
				'permission' => 'view_projects',
				'fields'     => [
					'project_id'         => 'integer',
					'target_object_id'   => 'integer',
					'target_object_type' => 'string',
				],
			],
			'remove_bookmark' => [
				'callback' => [ $this, 'remove_bookmark' ],
				'fields'   => [
					'id' => 'integer',
				],
			],
		];
	}

	public function create_bookmark( $request ) {
		$project_id = $request['project_id'] ?? 0;
		if ( ! $project_id ) {
			wp_send_json_error( [
				'message' => __( 'Project ID is required.', 'gemboards' ),
			] );
		}

		$target_object_id = $request['target_object_id'] ?? 0;
		if ( ! $target_object_id ) {
			wp_send_json_error( [
				'message' => __( 'Target object ID is required.', 'gemboards' ),
			] );
		}

		$target_object_type = $request['target_object_type'] ?? 'card';
		if ( ! in_array( $target_object_type, [ 'card', 'board' ], true ) ) {
			wp_send_json_error( [
				'message' => __( 'Invalid target object type.', 'gemboards' ),
			]);
		}

		if ( 'card' === $target_object_type ) {
			$card = Helper::get_card( $target_object_id );
			if ( ! $card ) {
				wp_send_json_error( [
					'message' => __( 'Invalid card ID.', 'gemboards' ),
				]);
			}
			$board = $card->get_board();
			if ( ! $board || $board->get_project_id() !== $project_id ) {
				wp_send_json_error( [
					'message' => __( 'Invalid card ID.', 'gemboards' ),
				]);
			}
		} else {
			$board = Helper::get_board( $target_object_id );
			if ( ! $board || $board->get_project_id() !== $project_id ) {
				wp_send_json_error( [
					'message' => __( 'Invalid board ID.', 'gemboards' ),
				]);
			}
		}

		$existing = ( new BookmarkCollection( [
			'per_page' => 1,
			'fields'   => 'count',
			'where'    => [
				[
					'key'   => 'project_id',
					'value' => $project_id,
				],
				[
					'key'   => 'user_id',
					'value' => get_current_user_id(),
				],
				[
					'key'   => 'target_object_id',
					'value' => $target_object_id,
				],
				[
					'key'   => 'target_object_type',
					'value' => $target_object_type,
				],
			],
		] ) )->get_results();

		if ( $existing[0] > 0 ) {
			wp_send_json_error( [
				'message' => __( 'Bookmark already exists.', 'gemboards' ),
			] );
		}

		$bookmark = new BookmarkEntity();
		$bookmark->set_project_id( $project_id );
		$bookmark->set_user_id( get_current_user_id() );
		$bookmark->set_target_object_id( $target_object_id );
		$bookmark->set_target_object_type( $target_object_type );
		$result = $bookmark->save();

		if ( is_wp_error( $result ) ) {
			wp_send_json_error( [
				'message' => $result->get_error_message(),
			] );
		}

		wp_send_json_success( [
			'message'  => __( 'Bookmark created successfully.', 'gemboards' ),
			'bookmark' => array_merge($bookmark->get_data(), [
				'id' => $bookmark->get_id(),
			]),
		] );
	}

	public function remove_bookmark( $request ) {
		$bookmark_id = $request['id'] ?? 0;
		if ( ! $bookmark_id ) {
			wp_send_json_error( [
				'message' => __( 'Bookmark ID is required.', 'gemboards' ),
			] );
		}

		$bookmark = Helper::get_bookmark( $bookmark_id );
		if ( ! $bookmark ) {
			wp_send_json_error([
				'message' => __( 'Invalid bookmark ID.', 'gemboards' ),
			]);
		}

		if ( $bookmark->get_user_id() !== get_current_user_id() ) {
			wp_send_json_error( [
				'message' => __( 'You do not have permission to remove this bookmark.', 'gemboards' ),
			] );
		}


		try {
			$result = $bookmark->delete();
			if ( ! $result ) {
				wp_send_json_error( [
					'message' => __( 'Error removing bookmark.', 'gemboards' ),
				] );
			}
		} catch ( GemboardsException $e ) {
			wp_send_json_error( [
				'message' => sprintf(
					// translators: %s: error message.
					__( 'Error removing bookmark: %s', 'gemboards' ),
					$e->getMessage()
				),
			] );
		}

		wp_send_json_success( [
			'message' => __( 'Bookmark removed successfully.', 'gemboards' ),
		] );
	}
}
