<?php declare(strict_types=1);

namespace App\Http\Resources\Private\Dashboard;

use App\Enums\CaptchaLogType;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

final class ChartCaptchaActivity extends JsonResource
{
    /**
     * @var \App\ServiceResults\Private\Dashboard\ChartCaptchaActivity
     */
    public $resource;

    /**
     * Transform the resource into an array.
     *
     * @param  Request $request
     * @return array
     */
    public function toArray(Request $request): array
    {
        $period = $this->resource->getFrom()->diff($this->resource->getTo())->stepBy('day');
        $days = [];
        $values = [];
        $types = [];
        foreach (CaptchaLogType::cases() as $type) {
            $values[$type->value] = [];
            $types[$type->value] = [
                'meta'  => $type->getTitle(),
                'value' => 0,
            ];
        }
        foreach ($period as $item) {
            $day = $item->format('Y-m-d');
            $days[] = $day;
            $quantity = $this->resource->getQuantityByDays()->getDay($day);
            foreach (CaptchaLogType::cases() as $type) {
                $values[$type->value][] = [
                    'meta'  => $type->getTitle(),
                    'value' => $quantity[$type->value] ?? 0,
                ];
            }
        }

        return [
            'days'   => $days,
            'values' => $values,
            'types'  => $types,
        ];
    }
}
