<?php

namespace App\Livewire\Chart;

use App\Enums\OrderChannel;
use App\Models\Order;
use Livewire\Component;

class ChannelRevenue extends Component
{
    public array $channels = [];

    public array $revenues = [];

    public array $backgroundColors = [];

    public array $borderColors = [];

    public function mount(array $outlets)
    {
        $orders = Order::select('channel', 'total')
            ->whereIn('outlet_id', array_keys($outlets))
            ->get()
            ->groupBy('channel');

        $data = [];

        foreach (OrderChannel::cases() as $channel) {
            $channelOrders = $orders[$channel->value] ?? collect();
            $total = $channelOrders->sum('total');

            $data[] = $total;
            $this->channels[] = $channel->label();
        }

        $this->revenues = $data;

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

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