<?php

namespace App\Http\Controllers\Admin\Report;

use App\Exports\WithdrawExport;
use App\Http\Controllers\Controller;
use App\Models\Withdraw;
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 WithdrawController extends Controller
{
    public function index()
    {
        adminUserHasPermission(permission: 'read');

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

        $query = Withdraw::query();

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

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

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

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

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

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

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

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

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

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