<?php
/**
 * Product CSV import preview row.
 *
 * @package Affilun\Application\Product\Csv
 */

declare(strict_types=1);

namespace Affilun\Application\Product\Csv;

/**
 * Immutable row-level preview for a saved-product CSV import.
 */
final class ProductCsvImportRow {

	/**
	 * Row action for a new product.
	 */
	public const ACTION_CREATE = 'create';

	/**
	 * Row action for an existing provider/item pair.
	 */
	public const ACTION_UPDATE = 'update';

	/**
	 * Physical CSV row number, including header row.
	 */
	private int $rowNumber;

	/**
	 * Planned action.
	 */
	private string $action;

	/**
	 * Provider key.
	 */
	private string $provider;

	/**
	 * Provider-side product identifier.
	 */
	private string $providerItemId;

	/**
	 * Product title.
	 */
	private string $title;

	/**
	 * Lifecycle state.
	 */
	private string $lifecycleState;

	/**
	 * Preview categories.
	 *
	 * @var list<string>
	 */
	private array $categories;

	/**
	 * Preview tags.
	 *
	 * @var list<string>
	 */
	private array $tags;

	/**
	 * Validation errors for this row.
	 *
	 * @var list<string>
	 */
	private array $errors;

	/**
	 * @param int          $rowNumber       CSV row number.
	 * @param string       $action          Planned action.
	 * @param string       $provider        Provider key.
	 * @param string       $providerItemId  Provider-side identifier.
	 * @param string       $title           Product title.
	 * @param string       $lifecycleState  Lifecycle state.
	 * @param list<string> $categories      Categories.
	 * @param list<string> $tags            Tags.
	 * @param list<string> $errors          Validation errors.
	 */
	public function __construct(
		int $rowNumber,
		string $action,
		string $provider,
		string $providerItemId,
		string $title,
		string $lifecycleState,
		array $categories = array(),
		array $tags = array(),
		array $errors = array()
	) {
		$this->rowNumber      = $rowNumber;
		$this->action         = $action;
		$this->provider       = $provider;
		$this->providerItemId = $providerItemId;
		$this->title          = $title;
		$this->lifecycleState = $lifecycleState;
		$this->categories     = array_values( $categories );
		$this->tags           = array_values( $tags );
		$this->errors         = array_values( $errors );
	}

	/**
	 * CSV row number.
	 */
	public function rowNumber(): int {
		return $this->rowNumber;
	}

	/**
	 * Planned action.
	 */
	public function action(): string {
		return $this->action;
	}

	/**
	 * Provider key.
	 */
	public function provider(): string {
		return $this->provider;
	}

	/**
	 * Provider item id.
	 */
	public function providerItemId(): string {
		return $this->providerItemId;
	}

	/**
	 * Product title.
	 */
	public function title(): string {
		return $this->title;
	}

	/**
	 * Lifecycle state.
	 */
	public function lifecycleState(): string {
		return $this->lifecycleState;
	}

	/**
	 * Categories.
	 *
	 * @return list<string>
	 */
	public function categories(): array {
		return $this->categories;
	}

	/**
	 * Tags.
	 *
	 * @return list<string>
	 */
	public function tags(): array {
		return $this->tags;
	}

	/**
	 * Row validation errors.
	 *
	 * @return list<string>
	 */
	public function errors(): array {
		return $this->errors;
	}

	/**
	 * Whether the row passed validation.
	 */
	public function isValid(): bool {
		return $this->errors === array();
	}
}
