<?php
namespace Jet_Engine\Modules\Custom_Content_Types\Rest;

use Jet_Engine\Modules\Custom_Content_Types\Module;

class Get_Content_Type extends \Jet_Engine_Base_API_Endpoint {

	/**
	 * Returns route name
	 *
	 * @return string
	 */
	public function get_name() {
		return 'get-content-type';
	}

	/**
	 * API callback
	 *
	 * @return void
	 */
	public function callback( $request ) {

		$params = $request->get_params();
		$id     = isset( $params['id'] ) ? esc_attr( $params['id'] ) : false;

		if ( ! $id ) {

			Module::instance()->manager->add_notice(
				'error',
				__( 'Item ID not found in request', 'jet-engine' )
			);

			return rest_ensure_response( array(
				'success' => false,
				'notices' => Module::instance()->manager->get_notices(),
			) );

		}

		$content_type = Module::instance()->manager->data->get_item_for_edit( $id );

		if ( ! $content_type ) {

			Module::instance()->manager->add_notice(
				'error',
				__( 'Post type not found', 'jet-engine' )
			);

			return rest_ensure_response( array(
				'success' => false,
				'notices' => Module::instance()->manager->get_notices(),
			) );

		}

		$table_exists = \Jet_Engine\Modules\Custom_Content_Types\DB::custom_table_exists( $content_type['slug'] );
		$type_object  = \Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types( $content_type['slug'] );
		$schema_valid = $type_object->db->has_columns_by_schema();

		$table_notice = false;

		if ( ! $table_exists ) {
			$table_notice = esc_html__( 'DB table does not exist. Please, check the field names for MySQL reserved words and update this CCT.', 'jet-engine' );
		} elseif( ! $schema_valid ) {
			$table_notice = esc_html__( 'DB schema does not correspond to CCT settings. Please, check the field names for MySQL reserved words and update this CCT.', 'jet-engine' );
		}

		return rest_ensure_response( array(
			'success'      => true,
			'data'         => $content_type,
			'table_notice' => $table_notice,
		) );

	}

	/**
	 * Returns endpoint request method - GET/POST/PUT/DELTE
	 *
	 * @return string
	 */
	public function get_method() {
		return 'GET';
	}

	/**
	 * Check user access to current end-popint
	 *
	 * @return bool
	 */
	public function permission_callback( $request ) {
		return current_user_can( 'manage_options' );
	}

	/**
	 * Get query param. Regex with query parameters
	 *
	 * @return string
	 */
	public function get_query_params() {
		return '(?P<id>[a-z\-\d]+)';
	}

}