<?php

namespace SmartSlotAppointmentScheduler;

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

if (! class_exists('\SmartSlotAppointmentScheduler\Schedule_Posttype')) {

	class Schedule_Posttype {

		const SLUG = 'ssas_schedule';

		/**
		 * Register Custom Post Type.
		 *
		 * @return void
		 */
		public static function register() {

			register_post_type(
				self::SLUG,
				[
					'labels' => [
						'name'               => __('Schedules', 'smartslot-appointment-scheduler'),
						'singular_name'      => __('Schedule', 'smartslot-appointment-scheduler'),
						'menu_name'          => _x('Schedules', 'Admin Menu text', 'smartslot-appointment-scheduler'),
						'name_admin_bar'     => _x('Schedule', 'Add New on Toolbar', 'smartslot-appointment-scheduler'),
						'add_new'            => __('Add New', 'smartslot-appointment-scheduler'),
						'add_new_item'       => __('Add New Schedule', 'smartslot-appointment-scheduler'),
						'edit_item'          => __('Edit Schedule', 'smartslot-appointment-scheduler'),
						'new_item'           => __('New Schedule', 'smartslot-appointment-scheduler'),
						'view_item'          => __('View Schedule', 'smartslot-appointment-scheduler'),
						'view_items'         => __('View Schedules', 'smartslot-appointment-scheduler'),
						'search_items'       => __('Search Schedules', 'smartslot-appointment-scheduler'),
						'not_found'          => __('No schedules found', 'smartslot-appointment-scheduler'),
						'not_found_in_trash' => __('No schedule found in Trash', 'smartslot-appointment-scheduler'),
						'all_items'          => __('All Schedules', 'smartslot-appointment-scheduler'),
					],

					'has_archive'  => 'ssas-schedules',
					'public'       => true,
					'show_ui'      => true,
					'show_in_rest' => true,
					'hierarchical' => true,

					'menu_icon' => 'dashicons-schedule',

					'rewrite' => [
						'slug'       => 'ssas-schedules',
						'with_front' => true,
					],

					'supports' => [
						'title',
						'custom-fields',
						'editor',
						'thumbnail',
						'excerpt',
						'author',
						'page-attributes',
					],
				]
			);
		}

		/**
		 * Register schedule meta fields for REST API.
		 *
		 * @return void
		 */
		public static function register_meta() {

			register_post_meta(
				self::SLUG,
				'_organiser_name',
				[
					'type'          => 'string',
					'single'        => true,
					'show_in_rest'  => true,
					'auth_callback' => function () {
						return current_user_can('edit_posts');
					},
				]
			);

			register_post_meta(
				self::SLUG,
				'_famous_service',
				[
					'type'          => 'array',
					'single'        => true,
					'show_in_rest'  => [
						'schema' => [
							'type'  => 'array',
							'items' => [
								'type' => 'integer',
							],
						],
					],
					'auth_callback' => function () {
						return current_user_can('edit_posts');
					},
				]
			);

			register_post_meta(
				self::SLUG,
				'_venue',
				[
					'type'          => 'string',
					'single'        => true,
					'show_in_rest'  => true,
					'auth_callback' => function () {
						return current_user_can('edit_posts');
					},
				]
			);

			register_post_meta(
				self::SLUG,
				'_provider_availability',
				[
					'type'          => 'array',
					'single'        => true,
					'show_in_rest'  => [
						'schema' => [
							'type'  => 'array',
							'items' => [
								'type' => 'string',
							],
						],
					],
					'auth_callback' => function () {
						return current_user_can('edit_posts');
					},
				]
			);
		}
	}

	add_action(
		'init',
		['\SmartSlotAppointmentScheduler\Schedule_Posttype', 'register'],
		11
	);

	add_action(
		'init',
		['\SmartSlotAppointmentScheduler\Schedule_Posttype', 'register_meta'],
		12
	);
}
