<?php

declare(strict_types=1);

namespace App\Eval\Dto;

final readonly class EvalCase
{
    /**
     * @param array<string, mixed> $assert
     */
    public function __construct(
        public string $id,
        public string $type,
        public string $prompt,
        public array $assert = [],
    ) {
    }

    /**
     * @param array<string, mixed> $row
     */
    public static function fromArray(array $row): self
    {
        $id = trim((string) ($row['id'] ?? ''));
        $type = trim((string) ($row['type'] ?? ''));
        $prompt = trim((string) ($row['prompt'] ?? ''));
        $assert = is_array($row['assert'] ?? null) ? $row['assert'] : [];

        if ($id === '') {
            throw new \InvalidArgumentException('Eval case id must not be empty.');
        }

        if ($type === '') {
            throw new \InvalidArgumentException(sprintf(
                'Eval case "%s" has an empty type.',
                $id
            ));
        }

        if ($prompt === '') {
            throw new \InvalidArgumentException(sprintf(
                'Eval case "%s" has an empty prompt.',
                $id
            ));
        }

        return new self(
            id: $id,
            type: $type,
            prompt: $prompt,
            assert: $assert,
        );
    }

    public function isRetrievalCase(): bool
    {
        return $this->type === 'retrieval';
    }
}