<?php

namespace ControlScripts\Scopes;

use \SHServ\Middleware\ControlScripts;
use \SHServ\Implements\ControlScriptsInterface;

/**
 * Sleep mode: turns off all indoor lighting except outdoor spotlights.
 */
class SleepScope extends ControlScripts implements ControlScriptsInterface {
	use \ControlScripts\Common;

	public function register_sync_map(): void {
		$this -> register_global_device_sync_map();
	}

	public function register_events_handlers(): void {
		// No event handlers needed; triggered via action script or mode API.
	}

	public function register_actions_scripts(): void {
		$this -> add_action_script([
			"alias" => "activate_sleep",
			"name" => "Режим сна",
			"description" => "Включить режим сна: выключить всё освещение кроме прожекторов",
			"author" => "Eugene Sukhodolskiy",
			"icon" => '<i class="ph ph-moon"></i>',
			"danger_level" => "cautious",
			"state_callback" => function() {
				$isSleep = $this -> mode() -> is('sleep');
				return [
					["label" => $isSleep ? "On" : "Off", "variant" => $isSleep ? "success" : "secondary"]
				];
			},
		], function($params) {
			$this -> mode() -> enable('sleep');

			$results = $this -> group_set_state([
				// Hall / Office / Master bedroom (light_hub_1 channels)
				"light_hub_1:0",
				"light_hub_1:1",
				"light_hub_1:2",

				// Office lamps
				"craft_table_lamp",
				"computer_table_lamp",

				// Bathroom
				"bathroom_2_light",

				// Plants room
				"plants_room_light",
				"italy_lamp_relay",
				"floor_lamp_relay",

				// Server room
				"server_room_light",

				// First floor (kitchen + hall)
				"fisrt_floor_big_relay:0",
				"fisrt_floor_big_relay:1",
				"fisrt_floor_big_relay:2",
				"fisrt_floor_big_relay:3",
				"fisrt_floor_big_relay:4",
			], false);

			return [
				"mode_enabled" => true,
				"lights_turned_off" => $results,
			];
		});

		$this -> add_action_script([
			"alias" => "deactivate_sleep",
			"name" => "Выход из сна",
			"description" => "Отключить режим сна",
			"author" => "Eugene Sukhodolskiy",
			"icon" => '<i class="ph ph-sun"></i>',
			"danger_level" => "safe",
			"state_callback" => function() {
				$isSleep = $this -> mode() -> is('sleep');
				return [
					["label" => $isSleep ? "On" : "Off", "variant" => $isSleep ? "success" : "secondary"]
				];
			},
		], function($params) {
			$this -> mode() -> disable('sleep');

			return [
				"mode_disabled" => true,
			];
		});
	}

	public function register_regular_scripts(): void {}
}
