<?php

namespace App\Charts;

use ArielMejiaDev\LarapexCharts\LarapexChart;

class ExampleChart
{
    protected $chart;

    public function __construct(LarapexChart $chart)
    {
        $this->chart = $chart;
    }

    public function build($options = null)
    {
        $data = \App\Models\ClientCase::query()
            ->selectRaw('EXTRACT(MONTH from created_at) as month, COUNT(id) as count')
            ->limit(6)
            ->whereRaw('EXTRACT(MONTH from created_at) > EXTRACT(MONTH from (NOW() - INTERVAL \'6 month\')) ')
            ->groupByRaw('EXTRACT(MONTH from created_at)')
            ->orderByRaw('EXTRACT(MONTH from created_at)')
            ->get();

        $months = $data->pluck('month')->map(
            fn ($nu) => \DateTime::createFromFormat('!m', $nu)->format('F'))->toArray();

        $newCases = $data->pluck('count')->toArray();

        return $this->chart->areaChart()
            ->setTitle('Novi primeri zadnjih šest mesecev.')
            ->addData('Primeri', $newCases)
            // ->addData('Completed', [7, 2, 7, 2, 5, 4])
            ->setColors(['#ff6384'])
            ->setXAxis($months)
            ->setToolbar(true)
            ->toVue();
    }
}
