<?php

namespace App\Http\Controllers\Admin\Contest;

use App\Http\Controllers\Controller;
use App\Models\ContestWinner;
use App\Services\ModalIndexQuey;

class WinnerController extends Controller
{
    public function index()
    {
        adminUserHasPermission(permission: 'read');

        $contest_id = request()->query('contest_id');

        $winners = ModalIndexQuey::get(model: ContestWinner::query()->when($contest_id, fn ($query, $contest_id) => $query->where('contest_id', $contest_id)), with: ['contest', 'user:id,first_name,last_name,username']);

        $columns = [
            [
                'label' => __('Contest'),
                'key' => 'contest.translation.title',
                'header_class' => 'lg:w-[350px]',
            ],
            [
                'label' => __('User'),
                'key' => 'user.full_name',
                'render' => function ($winner) {
                    return '<a href="'.route('admin.users.edit', $winner?->user?->id).'" class="flex max-lg:justify-end items-center gap-3">
                                <img src="'.placeAvatar($winner?->user?->avatar, $winner?->user?->full_name).'" width="32" height="32"
                                    class="rounded-full max-md:hidden" alt="'.$winner?->user?->full_name.'" />
                                <div>
                                    <p class="s-text mb-1 font-medium">'.$winner?->user?->full_name.'</p>
                                    <span class="text-xs">@'.$winner?->user?->username.'</span>
                                </div>
                            </a>';
                },
            ],
            [
                'label' => __('Prize Name'),
                'key' => 'prize_name',
            ],
            [
                'label' => __('Prize Coins'),
                'key' => 'prize_amount',
                'render' => function ($winner) {
                    return round($winner->prize_amount);
                },
            ],
            [
                'label' => __('Rank'),
                'key' => 'rank',
                'render' => function ($winner) {
                    return '<span class="badge badge-primary">'.__('Rank').' '.$winner->rank.'</span>';
                },
            ],
            [
                'label' => __('Action'),
                'render' => function ($winner) {

                    $action_buttons = [
                        [
                            'label' => __('Details'),
                            'icon' => 'ph ph-eye',
                            'type' => 'link',
                            'href' => route('admin.winners.show', $winner->id),
                        ],
                    ];

                    return view('admin.components.table-action', compact('action_buttons'))->render();
                },
            ],
        ];

        return view('admin.pages.contests.winners.index', compact('winners', 'columns'));
    }

    public function show(ContestWinner $winner)
    {
        adminUserHasPermission(permission: 'read');

        return view('admin.pages.contests.winners.show', compact('winner'));
    }
}
