<?php

namespace App\Dashboard\Stats;

use InvalidArgumentException;

class SeriesResult
{
    private bool $tooWide = false;

    /**
     * @param  array<int, string>  $labels
     * @param  array<int, Series>  $series
     */
    public function __construct(
        public readonly array $labels,
        public readonly array $series,
    ) {
        foreach ($series as $one) {
            if (count($one->values) !== count($labels)) {
                throw new InvalidArgumentException(
                    "Series [{$one->name}] has ".count($one->values).' values for '.count($labels).' labels.'
                );
            }
        }
    }

    public static function tooWide(): self
    {
        $result = new self([], []);
        $result->tooWide = true;

        return $result;
    }

    public function isTooWide(): bool
    {
        return $this->tooWide;
    }

    /** True only when no axis was built; a real range always has one label per day. */
    public function isEmpty(): bool
    {
        return ! $this->tooWide && $this->labels === [];
    }

    /** True when no series carries a measurement — an axis exists but nothing happened on it. */
    public function hasNoData(): bool
    {
        foreach ($this->series as $one) {
            if ($one->hasData()) {
                return false;
            }
        }

        return true;
    }
}
