<?php

namespace App\Services\PlanDigitizer;

/** The mandatory coverage audit: every lot label must end up with a polygon. */
class DigitizationReport
{
    /**
     * @param  array<string, array{expected: int, rendered: int, missing: list<string>}>  $blocks
     * @param  array<string, int>  $statuses
     * @param  list<string>  $missing
     * @param  list<string>  $review
     */
    public function __construct(
        public readonly int $labelsFound,
        public readonly int $rendered,
        public readonly int $withNumber,
        public readonly int $withBlock,
        public readonly int $withArea,
        public readonly int $withDimensions,
        public readonly array $blocks,
        public readonly array $statuses,
        public readonly array $missing,
        public readonly array $review,
    ) {}

    public function coverage(): float
    {
        return $this->labelsFound === 0
            ? 0.0
            : round($this->rendered / $this->labelsFound * 100, 2);
    }

    public function isComplete(): bool
    {
        return $this->labelsFound > 0
            && $this->rendered === $this->labelsFound
            && $this->missing === [];
    }

    /** @return array<string, mixed> */
    public function toArray(): array
    {
        return [
            'labels_found' => $this->labelsFound,
            'rendered' => $this->rendered,
            'with_number' => $this->withNumber,
            'with_block' => $this->withBlock,
            'with_area' => $this->withArea,
            'with_dimensions' => $this->withDimensions,
            'missing_count' => count($this->missing),
            'missing' => $this->missing,
            'review' => $this->review,
            'blocks' => $this->blocks,
            'statuses' => $this->statuses,
            'coverage' => $this->coverage(),
            'complete' => $this->isComplete(),
        ];
    }
}
