<?php

namespace App\Http\Controllers\Admin\Report;

use App\Exports\ContestParticipantExport;
use App\Http\Controllers\Controller;
use App\Models\ContestParticipant;
use App\Services\Helper\PdfGenerator;
use App\Services\ModalIndexQuey;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;

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

        $contestParticipants = ModalIndexQuey::get(ContestParticipant::query(), ['user', 'prize', 'contest']);

        $buttons = [
            [
                'label' => __('Export Report'),
                'icon' => 'ph ph-download-simple',
                'type' => 'link',
                'link' => route('admin.reports.contest-participants.export', ['excel', ...request()->all()]),
            ],
            [
                'label' => __('Pdf Report'),
                'icon' => 'ph ph-file-pdf',
                'type' => 'link',
                'link' => route('admin.reports.contest-participants.export', ['pdf', ...request()->all()]),
            ],
        ];

        $columns = [
            [
                'label' => __('User'),
                'key' => 'user.first_name',
                'header_class' => 'lg:w-[250px]',
            ],
            [
                'label' => __('Email'),
                'key' => 'user.email',
                'header_class' => 'lg:w-[250px]',
            ],
            [
                'label' => __('Contest Name'),
                'key' => 'contest.translation.title',
                'header_class' => 'lg:w-[250px]',
            ],
            [
                'label' => __('Prize'),
                'key' => 'prize.name',
                'header_class' => 'lg:w-[200px]',
            ],
            [
                'label' => __('Score'),
                'key' => 'score',
                'header_class' => 'lg:w-[150px]',
            ],
            [
                'label' => __('Status'),
                'key' => 'status',
                'header_class' => 'lg:w-[150px]',
                'render' => fn ($participant) => '<span class="status '.$participant->status->color().'">'.$participant->status->label().'</span>',
            ],
        ];

        return view('admin.pages.reports.contest-participant.index', compact('contestParticipants', 'columns', 'buttons'));
    }

    public function getExport(Request $request, $type)
    {
        adminUserHasPermission(permission: 'read');

        $storage = Storage::disk('report');

        if ($type === 'excel') {
            Excel::store(new ContestParticipantExport($request->all()), 'reports/contest_participants.xlsx', 'report');

            return response()->download($storage->path('reports/contest_participants.xlsx'));
        }

        $query = ContestParticipant::query()->with(['contest']);

        PdfGenerator::generate($query, 'reports.contest-participants', 'contest_participants.pdf');

        return response()->download($storage->path('reports/contest_participants.pdf'));
    }
}
