<?php

namespace GemBoards\utils\traits;

use Exception;
use WP_Error;
use GemBoards\Classes\Exception\GemboardsException;
use GemBoards\entities\CardAssignee as AssigneeEntity;
use GemBoards\Entities\TaskAttachment;
use GemBoardsPro\Entities\TaskChecklist;
use GemBoardsPro\Entities\TaskChecklistGroup;

trait Card {

	public static function get_card_by_task_id( int $task_id ) {
		$cache_key   = "card_by_task_$task_id";
		$cache_group = 'gemboards_cards';
		$data        = wp_cache_get( $cache_key, $cache_group );

		if ( ! $data ) {
			global $wpdb;
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
			$data = $wpdb->get_row(
				$wpdb->prepare(
					"SELECT * FROM {$wpdb->prefix}gemboards_cards WHERE task_id = %d", $task_id
				)
			);
			// phpcs:enable
			wp_cache_set( $cache_key, $data, $cache_group );
		}

		if ( ! $data ) {
			return false;
		}

		$card = new \GemBoards\Entities\Card();
		$card->set_props( $data );

		return $card;
	}

	public static function get_card( int $id ) {
		try {
			return ( new \GemBoards\Entities\Card( $id ) );
		} catch ( GemboardsException $e ) {
			return false;
		}
	}

	/**
	 * @param int $id
	 *
	 * @return AssigneeEntity|WP_Error
	 */
	public static function get_assignee( int $id ) {
		if ( ! $id ) {
			return new WP_Error( 'assignee-not-found', __( 'Assignee not found.', 'gemboards' ), [ 'status' => 404 ] );
		}

		$assignee = wp_cache_get( 'card_assignees_' . $id, 'gemboards_card_assignees' );

		if ( $assignee ) {
			if ( 0 === $assignee->get_id() ) {
				wp_cache_delete( 'card_assignees_' . $id, 'gemboards_card_assignees' );

				return new WP_Error( 'assignee-not-found', __( 'Assignee not found.', 'gemboards' ), [ 'status' => 404 ] );
			}

			return $assignee;
		}


		try {
			$assignee = new AssigneeEntity( $id );
			if ( $assignee instanceof AssigneeEntity ) {
				wp_cache_set( 'card_assignees_' . $id, $assignee, 'gemboards_card_assignees', HOUR_IN_SECONDS );
			}

			return $assignee;
		} catch ( GemboardsException $e ) {
			return $e->toWpError();
		} catch ( Exception $e ) {
			return new WP_Error( 'unknown-error', $e->getMessage(), [ 'status' => 500 ] );
		}
	}

	public static function get_user_assignee( int $card_id, int $user_id = 0 ) {
		if ( ! $card_id || ! $user_id ) {
			return false;
		}

		global $wpdb;

		$cache_key = "card_assignee:$card_id:$user_id";
		$id        = wp_cache_get( $cache_key, 'gemboards_card_assignees' );

		if ( false !== $id ) {
			return self::get_assignee( $id );
		}

		// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
		$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}gemboards_card_assignees WHERE card_id = %d AND user_id = %d LIMIT 1;", $card_id, $user_id ) );
		// phpcs:enable

		wp_cache_set( $cache_key, $id, 'gemboards_card_assignees', DAY_IN_SECONDS );

		$assignee = self::get_assignee( $id );
		if ( ! $assignee || is_wp_error( $assignee ) ) {
			return false;
		}

		return $assignee;
	}

	public static function get_checklist( int $id ) {
		try {
			return ( new TaskChecklist( $id ) );
		} catch ( GemboardsException $e ) {
			return false;
		}
	}

	public static function get_checklist_group( int $id ) {
		try {
			return ( new TaskChecklistGroup( $id ) );
		} catch ( GemboardsException $e ) {
			return false;
		}
	}

	public static function get_task_attachment( int $task_attachment_id ) {
		try {
			return ( new TaskAttachment( $task_attachment_id ) );
		} catch ( GemboardsException $e ) {
			return false;
		}
	}

	public static function get_card_comments( int $task_id ) {
		return get_comments( [
			'post_id' => $task_id,
			'type'    => 'task_comment',
			'status'  => 'approve',
			'orderby' => 'comment_ID',
			'order'   => 'DESC',
		] );
	}

	public static function get_card_activities( int $task_id ) {
		return get_comments( [
			'post_id' => $task_id,
			'type'    => 'activity',
			'status'  => 'approve',
			'orderby' => 'comment_ID',
			'order'   => 'DESC',
		] );
	}
}
