<?php

namespace GemBoards\Ajax;

use GemBoards\Classes\AbstractAjaxHandler;
use GemBoards\Utils\Helper;

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

class GlobalSearch extends AbstractAjaxHandler {

	public function __construct() {
		$this->actions = [
			'global_search' => [
				'callback' => [ $this, 'global_search' ],
				'fields'   => [
					'query' => 'string',
				],
			],
		];
	}

	/**
	 * Handle global search across boards, cards, and projects.
	 *
	 * @param array $payload
	 *
	 * @return void
	 */
	public function global_search( array $payload ): void {
		$query = $payload['query'] ?? '';

		if ( empty( $query ) ) {
			wp_send_json_error( [
				'message' => __( 'Search query is required.', 'gemboards' ),
			] );
		}

		$path    = Helper::get_settings( 'path', 'workspace' );
		$results = array_merge(
			$this->search_projects( $query, $path ),
			$this->search_boards( $query, $path ),
			$this->search_cards( $query, $path )
		);

		wp_send_json_success( $results );
	}

	/**
	 * Search projects by title.
	 *
	 * @param string $query
	 * @param string $path
	 *
	 * @return array
	 */
	private function search_projects( string $query, string $path ): array {
		global $wpdb;

		$like = '%' . $wpdb->esc_like( $query ) . '%';

		// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$rows = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT id, title FROM {$wpdb->prefix}gemboards_projects WHERE title LIKE %s LIMIT 10",
				$like
			)
		);
		// phpcs:enable

		$items = [];
		foreach ( $rows as $row ) {
			$items[] = [
				'type'  => 'project',
				'title' => $row->title,
				'url'   => home_url( "/{$path}/projects/{$row->id}" ),
			];
		}

		return $items;
	}

	/**
	 * Search boards by title.
	 *
	 * @param string $query
	 * @param string $path
	 *
	 * @return array
	 */
	private function search_boards( string $query, string $path ): array {
		global $wpdb;

		$like = '%' . $wpdb->esc_like( $query ) . '%';

		// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$rows = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT id, title FROM {$wpdb->prefix}gemboards_boards WHERE title LIKE %s LIMIT 10",
				$like
			)
		);
		// phpcs:enable

		$items = [];
		foreach ( $rows as $row ) {
			$items[] = [
				'type'  => 'board',
				'title' => $row->title,
				'url'   => home_url( "/{$path}/boards/{$row->id}" ),
			];
		}

		return $items;
	}

	/**
	 * Search cards by post title.
	 *
	 * @param string $query
	 * @param string $path
	 *
	 * @return array
	 */
	private function search_cards( string $query, string $path ): array {
		global $wpdb;

		$like = '%' . $wpdb->esc_like( $query ) . '%';

		// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$rows = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT card.id, post.ID as task_id, card.board_id, post.post_title AS title
				FROM {$wpdb->prefix}gemboards_cards AS card
				INNER JOIN {$wpdb->posts} AS post ON post.ID = card.task_id
				WHERE post.post_title LIKE %s AND card.archived = 0 LIMIT 10",
				$like
			)
		);
		// phpcs:enable

		$items = [];
		foreach ( $rows as $row ) {
			$items[] = [
				'type'  => 'card',
				'title' => $row->title,
				'url'   => home_url( "/{$path}/boards/{$row->board_id}?task_id={$row->task_id}" ),
			];
		}

		return $items;
	}
}
