<?php
/**
 * Plugin Name: Vachr Short-Term Rental Reservations
 * Description: A professional short-term rental booking engine for WordPress. Includes advanced seasonal pricing (Summer, Winter, Holidays), dynamic discounts, and multi-currency support. Sync effortlessly with Airbnb, Booking.com, and more via two-way iCal integration. Features a responsive frontend calendar, customizable contact fields, additional services, automated staff task notifications with confirmation and escalation, and email notifications. Lightweight, collision-protected, and fully translatable.
 * Version: 2.1.8
 * Author: Radomir Vachr
 * Author URI: https://vachr.cz
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: vachr-short-term-rental-reservations
 * Domain Path: /languages
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

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

define('VSTRB_VERSION', '2.1.8');
define('VSTRB_PLUGIN_FILE', __FILE__);
define('VSTRB_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('VSTRB_PLUGIN_URL', plugin_dir_url(__FILE__));

add_action('init', function() {
	$domain = 'vachr-short-term-rental-reservations';
	$locale = determine_locale();
	$mofile = VSTRB_PLUGIN_DIR . 'languages/' . $domain . '-' . $locale . '.mo';

	if (is_readable($mofile)) {
		unload_textdomain($domain, true);
		load_textdomain($domain, $mofile);
		return;
	}
}, 0);

require_once VSTRB_PLUGIN_DIR . 'includes/db.php';
require_once VSTRB_PLUGIN_DIR . 'includes/countries.php';
require_once VSTRB_PLUGIN_DIR . 'includes/staff-access.php';
require_once VSTRB_PLUGIN_DIR . 'includes/staff-tasks.php';
require_once VSTRB_PLUGIN_DIR . 'includes/cpt.php';
require_once VSTRB_PLUGIN_DIR . 'includes/shortcodes.php';
require_once VSTRB_PLUGIN_DIR . 'includes/payment-qr.php';
require_once VSTRB_PLUGIN_DIR . 'includes/rest.php';
require_once VSTRB_PLUGIN_DIR . 'includes/admin.php';
require_once VSTRB_PLUGIN_DIR . 'includes/ical.php';
require_once VSTRB_PLUGIN_DIR . 'includes/emails.php';

register_activation_hook(__FILE__, ['VSTRB_DB', 'activate']);
register_deactivation_hook(__FILE__, ['VSTRB_DB', 'deactivate']);

add_action('init', function() {
	VSTRB_CPT::register();
});

add_action('rest_api_init', ['VSTRB_REST', 'register']);

add_action('plugins_loaded', function() {
	if (get_option('vstrb_db_version') !== VSTRB_VERSION) {
		VSTRB_DB::activate();
		update_option('vstrb_db_version', VSTRB_VERSION);
	}
});

add_action('wp_enqueue_scripts', function() {
	wp_register_style('vstrb-theme-integration', VSTRB_PLUGIN_URL . 'assets/theme-integration.css', [], VSTRB_VERSION);
	wp_register_style('vstrb-tom-select', VSTRB_PLUGIN_URL . 'assets/vendor/tom-select/tom-select.default.min.css', [], '2.6.1');
	wp_register_style('vstrb-frontend', VSTRB_PLUGIN_URL . 'assets/frontend.css', ['vstrb-theme-integration', 'vstrb-tom-select'], VSTRB_VERSION);
	wp_register_script('vstrb-qrcode', VSTRB_PLUGIN_URL . 'assets/vendor/qrcodejs/qrcode.min.js', [], '1.0.0', true);
	wp_register_script('vstrb-payment-qr', VSTRB_PLUGIN_URL . 'assets/payment-qr.js', ['vstrb-qrcode'], VSTRB_VERSION, true);
	wp_register_script('vstrb-tom-select', VSTRB_PLUGIN_URL . 'assets/vendor/tom-select/tom-select.complete.min.js', [], '2.6.1', true);
	wp_register_script('vstrb-frontend', VSTRB_PLUGIN_URL . 'assets/frontend.js', ['wp-api-fetch', 'vstrb-payment-qr', 'vstrb-tom-select'], VSTRB_VERSION, true);

	wp_localize_script('vstrb-frontend', 'VSTRB', VSTRB_Shortcodes::get_localization_data());
});

add_action('admin_enqueue_scripts', function($hook) {
	$is_plugin_page = (strpos($hook, 'vstrb') !== false);
	$is_vstrb_unit_screen = false;
	if (in_array($hook, ['post.php', 'post-new.php', 'edit.php'], true)) {
		$screen = function_exists('get_current_screen') ? get_current_screen() : null;
		$is_vstrb_unit_screen = ($screen && isset($screen->post_type) && $screen->post_type === 'vstrb_unit');
	}
	if (!$is_plugin_page && !$is_vstrb_unit_screen) return;

	wp_enqueue_style('vstrb-admin', VSTRB_PLUGIN_URL . 'assets/admin.css', [], VSTRB_VERSION);
	wp_enqueue_script('vstrb-admin', VSTRB_PLUGIN_URL . 'assets/admin.js', ['jquery'], VSTRB_VERSION, true);
});
