<?php

namespace WeDevs\AppBridge\Core\REST;

use WP_REST_Controller;

abstract class BaseController extends WP_REST_Controller {

	/**
	 * Endpoint namespace
	 *
	 * @var string
	 */
	protected $namespace = 'app-bridge/v1';

	/**
	 * Check if the current user has manage_options capability.
	 *
	 * @return bool
	 */
	protected function check_manage_options(): bool {
		return current_user_can( 'manage_options' );
	}

	/**
	 * Shortcut for local site time only.
	 *
	 * @param string $gmt_datetime UTC datetime.
	 * @return string|null
	 */
	protected function prepare_local_date_response( $gmt_datetime ) {
		if ( empty( $gmt_datetime ) || '0000-00-00 00:00:00' === $gmt_datetime ) {
			return null;
		}

		return wp_date( 'c', strtotime( $gmt_datetime ) );
	}

	/**
	 * Shortcut for GMT only.
	 *
	 * @param string $gmt_datetime UTC datetime.
	 * @return string|null
	 */
	protected function prepare_gmt_date_response( $gmt_datetime ) {
		if ( empty( $gmt_datetime ) || '0000-00-00 00:00:00' === $gmt_datetime ) {
			return null;
		}

		return mysql_to_rfc3339( $gmt_datetime );
	}
}
