<?php
/**
 * @package Hafenstudios\Kurato
 */

namespace Hafenstudios\Kurato\Rest;

use Hafenstudios\Kurato\Db\ContactRepository;
use Hafenstudios\Kurato\Db\TagRepository;

defined( 'ABSPATH' ) || exit;

/**
 * REST: Tags (`kurato/v1/tags` und Kontakt-Tags). Lesen hs_view_contacts,
 * Aendern hs_manage_contacts.
 */
final class TagsController extends AbstractController {

	public const REST_NAMESPACE = 'kurato/v1';

	private ContactRepository $contacts;

	private TagRepository $tags;

	/**
	 * @param ContactRepository $contacts Kontakt-Repository.
	 * @param TagRepository     $tags     Tag-Repository.
	 */
	public function __construct( ContactRepository $contacts, TagRepository $tags ) {
		$this->contacts = $contacts;
		$this->tags     = $tags;
	}

	/**
	 * Registriert die Routen.
	 *
	 * @return void
	 */
	public function register_routes(): void {
		register_rest_route(
			self::REST_NAMESPACE,
			'/tags',
			array(
				'methods'             => \WP_REST_Server::READABLE,
				'callback'            => array( $this, 'list_tags' ),
				'permission_callback' => array( $this, 'can_view' ),
			)
		);

		register_rest_route(
			self::REST_NAMESPACE,
			'/contacts/(?P<id>\d+)/tags',
			array(
				array(
					'methods'             => \WP_REST_Server::READABLE,
					'callback'            => array( $this, 'contact_tags' ),
					'permission_callback' => array( $this, 'can_view' ),
				),
				array(
					'methods'             => \WP_REST_Server::CREATABLE,
					'callback'            => array( $this, 'attach_tag' ),
					'permission_callback' => array( $this, 'can_manage' ),
					'args'                => array(
						'label' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field' ),
					),
				),
			)
		);

		register_rest_route(
			self::REST_NAMESPACE,
			'/contacts/(?P<id>\d+)/tags/(?P<tag_id>\d+)',
			array(
				'methods'             => \WP_REST_Server::DELETABLE,
				'callback'            => array( $this, 'detach_tag' ),
				'permission_callback' => array( $this, 'can_manage' ),
			)
		);
	}

	/**
	 * GET /tags.
	 *
	 * @return \WP_REST_Response
	 */
	public function list_tags(): \WP_REST_Response {
		return new \WP_REST_Response( $this->tags->all(), 200 );
	}

	/**
	 * GET /contacts/{id}/tags.
	 *
	 * @param \WP_REST_Request $request Request.
	 * @return \WP_REST_Response|\WP_Error
	 */
	public function contact_tags( \WP_REST_Request $request ) {
		$id = (int) $request->get_param( 'id' );
		if ( null === $this->contacts->get( $id ) ) {
			return $this->not_found();
		}
		return new \WP_REST_Response( $this->tags->tags_for( $id ), 200 );
	}

	/**
	 * POST /contacts/{id}/tags - Tag anlegen/zuweisen.
	 *
	 * @param \WP_REST_Request $request Request.
	 * @return \WP_REST_Response|\WP_Error
	 */
	public function attach_tag( \WP_REST_Request $request ) {
		$id = (int) $request->get_param( 'id' );
		if ( null === $this->contacts->get( $id ) ) {
			return $this->not_found();
		}
		$tag_id = $this->tags->ensure( (string) $request->get_param( 'label' ) );
		if ( $tag_id > 0 ) {
			$this->tags->attach( $id, $tag_id );
		}
		return new \WP_REST_Response( $this->tags->tags_for( $id ), 200 );
	}

	/**
	 * DELETE /contacts/{id}/tags/{tag_id}.
	 *
	 * @param \WP_REST_Request $request Request.
	 * @return \WP_REST_Response|\WP_Error
	 */
	public function detach_tag( \WP_REST_Request $request ) {
		$id = (int) $request->get_param( 'id' );
		if ( null === $this->contacts->get( $id ) ) {
			return $this->not_found();
		}
		$this->tags->detach( $id, (int) $request->get_param( 'tag_id' ) );
		return new \WP_REST_Response( $this->tags->tags_for( $id ), 200 );
	}

	/**
	 * @return \WP_Error
	 */
	private function not_found() {
		return new \WP_Error(
			'kurato_contact_not_found',
			__( 'Der Kontakt wurde nicht gefunden.', 'kurato' ),
			array( 'status' => 404 )
		);
	}
}
