<?php
/**
 * Classified finding.
 *
 * @package LocalGuardScanner
 */

namespace LocalGuardScanner\Domain\Detection;

use InvalidArgumentException;

/**
 * Immutable finding ready for persistence and presentation.
 */
final class Finding {
	/** @var string */
	private $object_type;
	/** @var string */
	private $locator;
	/** @var int */
	private $score;
	/** @var int */
	private $confidence;
	/** @var Evidence[] */
	private $evidence;

	/**
	 * @param string     $object_type Object type.
	 * @param string     $locator     Stable locator.
	 * @param int        $score       Risk score.
	 * @param int        $confidence  Confidence score.
	 * @param Evidence[] $evidence    Evidence list.
	 */
	public function __construct( $object_type, $locator, $score, $confidence, array $evidence ) {
		if ( '' === trim( $object_type ) || '' === trim( $locator ) || empty( $evidence ) ) {
			throw new InvalidArgumentException( 'Finding requires object, locator, and evidence.' );
		}
		$this->object_type = $object_type;
		$this->locator     = $locator;
		$this->score       = max( 0, min( 100, (int) $score ) );
		$this->confidence  = max( 0, min( 100, (int) $confidence ) );
		$this->evidence    = $evidence;
	}

	/** @return string */
	public function object_type() {
		return $this->object_type;
	}

	/** @return string */
	public function locator() {
		return $this->locator;
	}

	/** @return int */
	public function score() {
		return $this->score;
	}

	/** @return int */
	public function confidence() {
		return $this->confidence;
	}

	/** @return string */
	public function severity() {
		return Severity::from_score( $this->score );
	}

	/** @return Evidence[] */
	public function evidence() {
		return $this->evidence;
	}
}
