<?php
/**
 * affilun Cloud revenue opportunity.
 *
 * @package Affilun\Application\Cloud
 */

declare(strict_types=1);

namespace Affilun\Application\Cloud;

/**
 * One prioritized revenue opportunity returned by affilun Cloud.
 */
final class CloudRevenueOpportunity {

	/**
	 * @param array<string,mixed> $data Raw opportunity row.
	 */
	public function __construct( private array $data ) {}

	/**
	 * Build from response data.
	 *
	 * @param array<string,mixed> $data Data.
	 */
	public static function fromArray( array $data ): self {
		return new self( $data );
	}

	/** Opportunity id. */
	public function id(): string {
		return $this->stringValue( 'id' );
	}

	/** Opportunity type. */
	public function type(): string {
		return $this->stringValue( 'opportunity_type' );
	}

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

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

	/** Reason. */
	public function reason(): string {
		return $this->stringValue( 'reason' );
	}

	/** Suggested action. */
	public function suggestedAction(): string {
		return $this->stringValue( 'suggested_action' );
	}

	/** Suggested microcopy. */
	public function suggestedMicrocopy(): string {
		return $this->stringValue( 'suggested_microcopy' );
	}

	/** Severity. */
	public function severity(): string {
		return $this->stringValue( 'severity' );
	}

	/** Status. */
	public function status(): string {
		return $this->stringValue( 'status' );
	}

	/** Priority score. */
	public function priorityScore(): int {
		return is_numeric( $this->data['priority_score'] ?? null ) ? (int) $this->data['priority_score'] : 0;
	}

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

	/** Product id. */
	public function productId(): string {
		return $this->stringValue( 'product_id' );
	}

	/** Post key. */
	public function postKey(): string {
		return $this->stringValue( 'post_key' );
	}

	/** Source. */
	public function source(): string {
		return $this->stringValue( 'source' );
	}

	/** Start timestamp/date. */
	public function startsAt(): string {
		return $this->stringValue( 'starts_at' );
	}

	/** Expiry timestamp/date. */
	public function expiresAt(): string {
		return $this->stringValue( 'expires_at' );
	}

	/**
	 * Read string field.
	 *
	 * @param string $key Field key.
	 */
	private function stringValue( string $key ): string {
		return is_scalar( $this->data[ $key ] ?? null ) ? (string) $this->data[ $key ] : '';
	}
}
