<?php

namespace App\Livewire;

use Illuminate\Support\Carbon;
use Illuminate\Support\Number;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('layouts.portal-app')]
class Backups extends Component
{
    public function render()
    {
        $locale = app()->getLocale();
        $gb = fn (float $v) => Number::format($v, precision: 1, locale: $locale).' GB';
        $date = fn (string $iso, string $fmt) => Carbon::parse($iso)->local()->locale($locale)->isoFormat($fmt);

        // 14 days of backup sizes for the bar chart.
        $sizes = [18.1, 18.1, 18.2, 18.2, 18.2, 18.3, 18.3, 18.3, 18.3, 18.4, 18.4, 18.3, 18.4, 18.4];

        $rows = [];
        foreach (range(0, 6) as $i) {
            $rows[] = [
                'when' => $date(Carbon::parse('2026-07-24')->subDays($i)->toDateString(), 'dd, D. MMMM').', 03:1'.($i % 3 + 1),
                'size' => $gb(18.4 - $i * 0.02),
                'status' => 'ok',
            ];
        }

        return view('livewire.backups', [
            'rows' => $rows,
            'lastTest' => $date('2026-07-01', 'LL'),
            'sizeChart' => [
                'type' => 'line',
                'data' => [
                    'labels' => array_map(fn ($i) => $date(Carbon::parse('2026-07-24')->subDays(13 - $i)->toDateString(), 'D.'), range(0, 13)),
                    'datasets' => [[
                        'label' => 'GB',
                        'data' => $sizes,
                        'borderColor' => 'token:accent',
                        'fill' => true,
                        'tension' => 0.35,
                        'pointRadius' => 0,
                        'borderWidth' => 2,
                    ]],
                ],
                'options' => [
                    'scales' => [
                        'x' => ['grid' => ['display' => false]],
                        'y' => ['beginAtZero' => false, 'grid' => ['color' => 'token:border']],
                    ],
                    'plugins' => ['legend' => ['display' => false]],
                ],
            ],
        ]);
    }
}
