<?php
namespace SmartSlotAppointmentScheduler;

/**
 * Plugin Name:       SmartSlot Appointment Scheduler
 * Description:       A flexible WordPress appointment booking plugin for managing schedules, dynamic time slots, availability, and email notifications.
 * Version:           1.0.2
 * Requires at least: 6.7
 * Requires PHP:      8.0
 * Tested up to:      7.1
 * Author:            PluginsKart
 * Author URI:        https://www.pluginskart.com/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       smartslot-appointment-scheduler
 */

// Exit if accessed directly.
if (!defined('ABSPATH')) {
	exit;
}

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

	class Book_An_Appointment {

		/**
		 * The main plugin file.
		 *
		 * @var string
		 */
		public $file;

		/**
		 * Constructor.
		 */
		public function __construct() {

			$this->file = plugin_dir_path(__FILE__);

			register_activation_hook(
				__FILE__,
				[__CLASS__, 'plugin_activated']
			);

			register_deactivation_hook(
				__FILE__,
				[__CLASS__, 'plugin_deactivated']
			);
		}

		/**
		 * Performs necessary actions upon plugin activation.
		 *
		 * @return void
		 */
		public static function plugin_activated() {

			/*
			 * Load the Schedule Settings class.
			 */
			require_once __DIR__ . '/includes/posttypes/schedule-settings.php';

			/*
			 * Save default settings.
			 */
			\SmartSlotAppointmentScheduler\Schedule_Settings::save_default_settings();

			// flush_rewrite_rules();
		}

		/**
		 * Performs necessary actions upon plugin deactivation.
		 *
		 * @return void
		 */
		public static function plugin_deactivated() {

			// flush_rewrite_rules();
		}

		/**
		 * Initialize the plugin.
		 *
		 * @return void
		 */
		public static function init() {

			// Register post types and taxonomies.
			add_action(
				'init',
				[__CLASS__, 'add_posttype']
			);

			// Add database tables.
			add_action(
				'init',
				[__CLASS__, 'add_database_tables']
			);

			// Include assets.
			add_action(
				'init',
				[__CLASS__, 'add_assets']
			);

			// Get custom settings fields.
			add_action(
				'init',
				[__CLASS__, 'get_custom_fields']
			);

			// Register custom APIs.
			add_action(
				'init',
				[__CLASS__, 'set_custom_api']
			);

			// Add reports.
			add_action(
				'init',
				[__CLASS__, 'set_reports']
			);

			// Add dashboard.
			add_action(
				'init',
				[__CLASS__, 'set_dashboard']
			);

			// Add settings link.
			$plugin_basename = plugin_basename(__FILE__);

			add_filter(
				'plugin_action_links_' . $plugin_basename,
				[__CLASS__, 'plugin_settings_link']
			);
		}

		/**
		 * Add plugin settings link.
		 *
		 * @param array $links Existing plugin action links.
		 * @return array
		 */
		public static function plugin_settings_link($links) {

			$url = admin_url(
				'edit.php?post_type=ssas_schedule&page=schedule-settings'
			);

			$settings_link =
				'<a href="' . esc_url($url) . '">' .
				esc_html__(
					'Settings',
					'smartslot-appointment-scheduler'
				) .
				'</a>';

			$links[] = $settings_link;

			return $links;
		}

		/**
		 * Register post types and related admin pages.
		 *
		 * @return void
		 */
		public static function add_posttype() {

			// Custom Post Type.
			require_once __DIR__ .
				'/includes/posttypes/register-posttype.php';

			// Taxonomies for CPT.
			require_once __DIR__ .
				'/includes/posttypes/register-taxonomies.php';

			// Add meta fields to the post type.
			require_once __DIR__ .
				'/includes/posttypes/add-field-for-posttype.php';

			// Settings page fields.
			require_once __DIR__ .
				'/includes/posttypes/schedule-settings.php';

			// Register submenu page under CPT.
			require_once __DIR__ .
				'/includes/posttypes/register-submenu-page.php';
		}

		/**
		 * Install database tables.
		 *
		 * @return void
		 */
		public static function add_database_tables() {

			require_once __DIR__ .
				'/includes/installer/setup-database.php';
		}

		/**
		 * Include frontend/admin assets.
		 *
		 * @return void
		 */
		public static function add_assets() {

			require_once __DIR__ .
				'/includes/enqueuer.php';
		}

		/**
		 * Load custom settings fields.
		 *
		 * @return void
		 */
		public static function get_custom_fields() {

			require_once __DIR__ .
				'/controlls/setting-fields/get-sub-menu-fields.php';
		}

		/**
		 * Register custom APIs and mailer.
		 *
		 * @return void
		 */
		public static function set_custom_api() {

			// Register custom APIs.
			require_once __DIR__ .
				'/controlls/custom-api/register-custom-api.php';

			// Post user details to database.
			require_once __DIR__ .
				'/controlls/custom-api/post-user-api.php';

			// Get booked time slots.
			require_once __DIR__ .
				'/controlls/custom-api/update-time-api.php';

			// Email notifications.
			require_once __DIR__ .
				'/controlls/mailer/mail-trigger.php';
		}

		/**
		 * Load appointment reports.
		 *
		 * @return void
		 */
		public static function set_reports() {

			require_once __DIR__ .
				'/includes/reports/reports-page.php';
		}

		/**
		 * Load booking dashboard.
		 *
		 * @return void
		 */
		public static function set_dashboard() {

			require_once __DIR__ .
				'/includes/dashboard/dashboard.php';
		}
	}

	/**
	 * Create the main plugin instance.
	 */
	new Book_An_Appointment();

	/**
	 * Initialize the plugin.
	 */
	add_action(
		'init',
		['\SmartSlotAppointmentScheduler\Book_An_Appointment', 'init'],
		0
	);
}
