<?php

declare(strict_types=1);

namespace App\Support\Governance\Controls;

use InvalidArgumentException;

final readonly class ArtifactSuitability
{
    public function __construct(
        public bool $baseline,
        public bool $drift,
        public bool $finding,
        public bool $exception,
        public bool $evidence,
        public bool $review,
        public bool $report,
    ) {}

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromArray(array $data): self
    {
        foreach (self::requiredKeys() as $key) {
            if (! array_key_exists($key, $data)) {
                throw new InvalidArgumentException(sprintf('Canonical control artifact suitability is missing [%s].', $key));
            }
        }

        return new self(
            baseline: (bool) $data['baseline'],
            drift: (bool) $data['drift'],
            finding: (bool) $data['finding'],
            exception: (bool) $data['exception'],
            evidence: (bool) $data['evidence'],
            review: (bool) $data['review'],
            report: (bool) $data['report'],
        );
    }

    /**
     * @return array{baseline: bool, drift: bool, finding: bool, exception: bool, evidence: bool, review: bool, report: bool}
     */
    public function toArray(): array
    {
        return [
            'baseline' => $this->baseline,
            'drift' => $this->drift,
            'finding' => $this->finding,
            'exception' => $this->exception,
            'evidence' => $this->evidence,
            'review' => $this->review,
            'report' => $this->report,
        ];
    }

    /**
     * @return list<string>
     */
    public static function requiredKeys(): array
    {
        return ['baseline', 'drift', 'finding', 'exception', 'evidence', 'review', 'report'];
    }
}
