<?php

namespace App\Livewire\Chart;

use Illuminate\Support\Facades\DB;
use Livewire\Component;

class OutletRevenue extends Component
{
    public array $outlets = [];

    public array $revenues = [];

    public array $backgroundColors = [];

    public array $borderColors = [];

    public function mount(array $outlets)
    {
        $revenues = DB::table('orders')
            ->select('outlet_id', DB::raw('SUM(total) as total_revenue'))
            ->whereIn('outlet_id', array_keys($outlets))
            ->groupBy('outlet_id')
            ->pluck('total_revenue', 'outlet_id');

        $this->outlets = array_values($outlets);

        $this->revenues = collect($outlets)->map(fn ($name, $id) => $revenues[$id] ?? 0)->values()->toArray();

        // random colors
        $count = count($this->outlets);
        $this->backgroundColors = randomColors($count, 0.2);
        $this->borderColors = randomColors($count, 1.0);
    }

    public function render()
    {
        return view('livewire.chart.outlet-revenue');
    }
}
