<?php

declare(strict_types=1);

namespace Autogedal\FeedMonitor\Model\Fetch;

/**
 * Outcome of reading and validating a feed: OK with its rows, or the problem (fetch / empty / invalid) and why.
 */
class FeedResult
{
    /**
     * @param array<string, array<int|string, mixed>> $rows
     */
    private function __construct(
        private readonly ?string $problem,
        private readonly string $message,
        private readonly array $rows,
        private readonly int $skipped,
        private readonly int $attempts
    ) {
    }

    /**
     * @param array<string, array<int|string, mixed>> $rows
     */
    public static function ok(array $rows, int $skipped, int $attempts): self
    {
        return new self(null, '', $rows, $skipped, $attempts);
    }

    public static function failed(string $problem, string $message, int $attempts): self
    {
        return new self($problem, $message, [], 0, $attempts);
    }

    public function isOk(): bool
    {
        return $this->problem === null;
    }

    public function getProblem(): ?string
    {
        return $this->problem;
    }

    public function getMessage(): string
    {
        return $this->message;
    }

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

    public function getCount(): int
    {
        return count($this->rows);
    }

    public function getSkipped(): int
    {
        return $this->skipped;
    }

    public function getAttempts(): int
    {
        return $this->attempts;
    }
}
