<?php

namespace Ysv\NovaLeader;

use App\Domain\Subscriptions\Service\SubscriptionService;
use App\Models\User;
use Laravel\Nova\Card;

class NovaLeader extends Card
{
    /**
     * The width of the card (1/3, 1/2, or full).
     *
     * @var string
     */
    public $width = '1/3';

    public $height = 'fixed';

    public function currentLeader()
    {
        $mode = nova_get_setting('vote_paid_mode');
        $leader_count = nova_get_setting('vote_leader_count');

        if($mode){
            $leaders = collect(SubscriptionService::leaders())->take($leader_count);
            $ids = $leaders->pluck('user_id');
            $users = User::whereIn('id', $ids)->get();
            $leaderUsers = $leaders->map(function($item) use($users) {
                $item->user = $users->where('id', $item->user_id)->first()->only(['id', 'first_name', 'last_name', 'username']);
                return $item;
            });
        }else{
            $leaderUsers = SubscriptionService::freeLeaders();
        }


        $amount = SubscriptionService::amount();

        return $this->withMeta(['leaders' => $leaderUsers, 'amount' => $amount, 'mode' => (boolean) $mode]);
    }

    /**
     * Get the component name for the element.
     *
     * @return string
     */
    public function component()
    {
        return 'nova-leader';
    }
}
