<?php

namespace App\Http\Controllers\Admin\Report;

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

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

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

        $query = Deposit::query();

        $buy_coins = ModalIndexQuey::get(
            (clone $query),
            ['user:id,first_name,last_name,username,email', 'payment'],
        );

        $columns = [
            [
                'label' => __('User|ID'),
                'key' => 'first_name',
                'header_class' => 'lg:w-[200px]',
                'is_sortable' => true,
                'render' => function ($deposit) {
                    return '<a href="'.route('admin.users.edit', $deposit->user?->id).'" class="flex flex-col items-start">
                                <p class="s-text font-medium">'.$deposit?->user?->full_name.'</p>
                                <span class="text-xs">'.$deposit?->user?->username.'</span>
                            </a>';
                },
            ],
            [
                'label' => __('Tracking Id'),
                'key' => 'track',
            ],
            [
                'label' => __('Amount'),
                'key' => 'amount',
                'is_sortable' => true,
                'render' => fn ($deposit) => amountWithSymbol(convertCurrency($deposit->amount + $deposit->charge, $deposit->currency), $deposit->currency),
            ],
            [
                'label' => __('Coins'),
                'key' => 'coins',
            ],
            [
                'label' => __('Status'),
                'key' => 'status',
                'is_sortable' => true,
                'render' => fn ($deposit) => '<span class="status '.$deposit->status->color().'">'.$deposit->status->label().'</span>',
            ],
            [
                'label' => __('Date'),
                'key' => 'created_at',
                'is_sortable' => true,
                'render' => function ($deposit) {
                    return '<p class="s-text font-medium">'.$deposit->created_at->format('Y-m-d H:i A').'</p>
                            <span class="text-xs">'.Carbon::parse($deposit->created_at)->diffForHumans().'</span>';
                },
            ],
        ];

        return view('admin.pages.reports.buy_coins.index', compact('buy_coins', 'columns', 'buttons'));
    }

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

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

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

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

        $query = Deposit::query()->with(['user:id,first_name,last_name,username,email']);

        PdfGenerator::generate($query, 'reports.buy_coins', 'buy_coins.pdf');

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