<?php
/**
 * Recording plan compilation request.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Application;

use ScheduledChanges\Domain\Recording\RecordingSession;
use ScheduledChanges\Domain\Shared\NamedTimezone;

/**
 * Carries normalized selected recording intent into application compilation.
 */
final class RecordingPlanCompilationRequest {
	private RecordingSession $session;
	/**
	 * @var array<int,array<string,mixed>>
	 */
	private array $selected_changes;
	private string $launch_name;
	private string $launch_description;
	private NamedTimezone $timezone;
	private int $actor_id;

	/**
	 * @param array<int,array<string,mixed>> $selected_changes Normalized selected change rows.
	 */
	public function __construct(
		RecordingSession $session,
		array $selected_changes,
		string $launch_name,
		string $launch_description,
		NamedTimezone $timezone,
		int $actor_id
	) {
		if ( array() === $selected_changes ) {
			throw new \InvalidArgumentException( 'At least one recorded change must be selected.' );
		}

		if ( '' === trim( $launch_name ) ) {
			throw new \InvalidArgumentException( 'Recording compilation requires a launch name.' );
		}

		if ( $actor_id <= 0 ) {
			throw new \InvalidArgumentException( 'Recording compilation requires a valid actor.' );
		}

		foreach ( $selected_changes as $change ) {
			if ( ! is_array( $change ) ) {
				throw new \InvalidArgumentException( 'Selected recording intent must contain structured change rows.' );
			}
		}

		$this->session            = $session;
		$this->selected_changes   = array_values( $selected_changes );
		$this->launch_name        = $launch_name;
		$this->launch_description = $launch_description;
		$this->timezone           = $timezone;
		$this->actor_id           = $actor_id;
	}

	/**
	 * Returns the source recording session.
	 */
	public function session(): RecordingSession {
		return $this->session;
	}

	/**
	 * @return array<int,array<string,mixed>>
	 */
	public function selectedChanges(): array {
		return $this->selected_changes;
	}

	/**
	 * Returns the selected change count.
	 */
	public function selectedChangeCount(): int {
		return count( $this->selected_changes );
	}

	/**
	 * Returns the launch name.
	 */
	public function launchName(): string {
		return $this->launch_name;
	}

	/**
	 * Returns the launch description.
	 */
	public function launchDescription(): string {
		return $this->launch_description;
	}

	/**
	 * Returns the launch timezone.
	 */
	public function timezone(): NamedTimezone {
		return $this->timezone;
	}

	/**
	 * Returns the actor compiling the plan.
	 */
	public function actorId(): int {
		return $this->actor_id;
	}
}
