<?php
/**
 * Fluent field-definition construction.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Field;

/**
 * Builds field definitions without exposing the legacy positional constructor.
 */
final class FieldDefinitionBuilder {
	private string $id;
	private string $label;
	private string $description = '';
	private string $owner;
	private string $provider;
	/** @var string[] */
	private array $scope;
	/** @var string[] */
	private array $target_subtypes = array();
	private string $data_type;
	private string $ui_component;
	/** @var array<string,bool|string|string[]> */
	private array $behavior = array();
	/** @var array<string,mixed> */
	private array $validation  = array();
	private string $capability = 'edit_post';
	/** @var array<string,mixed> */
	private array $display = array();

	/**
	 * Starts a definition with its required identity and presentation fields.
	 *
	 * @param string[] $scope Supported target types.
	 */
	public static function create( string $id, string $label, string $owner, string $provider, array $scope, string $data_type, string $ui_component ): self {
		$builder               = new self();
		$builder->id           = $id;
		$builder->label        = $label;
		$builder->owner        = $owner;
		$builder->provider     = $provider;
		$builder->scope        = $scope;
		$builder->data_type    = $data_type;
		$builder->ui_component = $ui_component;

		return $builder;
	}

	public function description( string $description ): self {
		$this->description = $description;

		return $this;
	}

	/**
	 * @param string[] $target_subtypes Supported target subtypes.
	 */
	public function subtypes( array $target_subtypes ): self {
		$this->target_subtypes = $target_subtypes;

		return $this;
	}

	/**
	 * @param array<string,bool|string|string[]> $behavior Read/write/scheduling behavior flags.
	 */
	public function behavior( array $behavior ): self {
		$this->behavior = $behavior;

		return $this;
	}

	/**
	 * @param array<string,mixed> $validation Declarative validation rules.
	 */
	public function validation( array $validation ): self {
		$this->validation = $validation;

		return $this;
	}

	public function capability( string $capability ): self {
		$this->capability = $capability;

		return $this;
	}

	/**
	 * @param array<string,mixed> $display Display metadata.
	 */
	public function display( array $display ): self {
		$this->display = $display;

		return $this;
	}

	public function build(): FieldDefinition {
		return new FieldDefinition(
			$this->id,
			$this->label,
			$this->description,
			$this->owner,
			$this->provider,
			$this->scope,
			$this->target_subtypes,
			$this->data_type,
			$this->ui_component,
			(bool) ( $this->behavior['readable'] ?? true ),
			(bool) ( $this->behavior['writable'] ?? false ),
			(bool) ( $this->behavior['schedulable'] ?? false ),
			(bool) ( $this->behavior['reversible'] ?? false ),
			(bool) ( $this->behavior['sensitive'] ?? false ),
			array_values( (array) ( $this->behavior['dependencies'] ?? array() ) ),
			(string) ( $this->behavior['conflict_strategy'] ?? 'fail' ),
			$this->validation,
			$this->capability,
			array(),
			array(),
			array(),
			$this->display
		);
	}

	private function __construct() {
	}
}
