<?php

use PHPUnit\Framework\TestCase;
use SHServ\Controllers\ScriptsRESTAPIController;

class ScriptsRESTAPIControllerValidationTest extends TestCase {
	protected $controller;

	protected function setUp(): void {
		$this -> controller = new ScriptsRESTAPIController();
	}

	private function decode(string $json): array {
		return json_decode($json, true);
	}

	public function test_run_action_script_rejects_invalid_alias(): void {
		$result = $this -> controller -> run_action_script('BAD!', []);
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('invalid_alias', $data['error_alias']);
		$this -> assertContains('alias', $data['failed_fields']);
	}

	public function test_run_action_script_rejects_invalid_json_params(): void {
		$result = $this -> controller -> run_action_script('toggle', '{bad}');
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('invalid_json', $data['error_alias']);
		$this -> assertContains('params', $data['failed_fields']);
	}

	public function test_run_action_script_rejects_non_array_params(): void {
		$result = $this -> controller -> run_action_script('toggle', 123);
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('invalid_params', $data['error_alias']);
		$this -> assertContains('params', $data['failed_fields']);
	}

	public function test_run_action_script_returns_not_found_for_valid_unregistered_alias(): void {
		$result = $this -> controller -> run_action_script('toggle', []);
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('action_script_not_found', $data['error_alias']);
	}

	public function test_set_scope_state_rejects_invalid_state(): void {
		$result = $this -> controller -> set_scope_state('scope1', 'invalid');
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('invalid_state', $data['error_alias']);
		$this -> assertContains('state', $data['failed_fields']);
	}

	public function test_set_regular_script_state_rejects_invalid_state(): void {
		$result = $this -> controller -> set_regular_script_state('script1', 'invalid');
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('invalid_state', $data['error_alias']);
		$this -> assertContains('state', $data['failed_fields']);
	}

	public function test_set_action_script_state_rejects_invalid_state(): void {
		$result = $this -> controller -> set_action_script_state('action1', 'invalid');
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('invalid_state', $data['error_alias']);
		$this -> assertContains('state', $data['failed_fields']);
	}

	public function test_place_in_area_rejects_invalid_target_id(): void {
		$result = $this -> controller -> place_in_area(0, 1);
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('invalid_id', $data['error_alias']);
		$this -> assertContains('target_id', $data['failed_fields']);
	}

	public function test_place_in_area_rejects_invalid_place_in_area_id(): void {
		$result = $this -> controller -> place_in_area(1, 0);
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('invalid_id', $data['error_alias']);
		$this -> assertContains('place_in_area_id', $data['failed_fields']);
	}

	public function test_unassign_from_area_rejects_invalid_id(): void {
		$result = $this -> controller -> unassign_from_area(0);
		$data = $this -> decode($result);
		$this -> assertFalse($data['status']);
		$this -> assertSame('invalid_id', $data['error_alias']);
		$this -> assertContains('target_id', $data['failed_fields']);
	}
}
