<?php

namespace App\Http\Controllers\Admin\Report;

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

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

        $query = Transaction::query();

        $transactions = ModalIndexQuey::get(
            (clone $query),
            ['holder', 'ref']
        );

        $columns = [
            [
                'label' => __('User|ID'),
                'key' => 'first_name',
                'header_class' => 'lg:w-[200px]',
                'is_sortable' => true,
                'render' => function ($transaction) {

                    if (! $transaction?->holder?->full_name) {
                        return 'Admin User';
                    }

                    return '<a href="'.route('admin.users.edit', $transaction->holder_id).'" class="flex flex-col items-start">
                                <p class="s-text font-medium">'.$transaction?->holder?->full_name.'</p>
                                <span class="text-xs">'.$transaction?->holder?->username.'</span>
                            </a>';
                },
            ],
            [
                'label' => __('Amount|Charge'),
                'key' => 'amount',
                'is_sortable' => true,
                'render' => function ($transaction) {
                    $amount = '';
                    if ($transaction->type == 'debit') {
                        $amount = '<p class="s-text font-medium text-red-500">-'.amountWithSymbol($transaction->amount).'</p>';
                    } else {
                        $amount = '<p class="s-text font-medium text-green-500">+'.amountWithSymbol($transaction->amount).'</p>';
                    }

                    return $amount;
                },
            ],
            [
                'label' => __('Transaction Id'),
                'key' => 'track',
            ],
            [
                'label' => __('Date'),
                'key' => 'created_at',
                'is_sortable' => true,
                'render' => function ($transaction) {
                    return '<p class="s-text font-medium">'.$transaction->created_at->format('Y-m-d H:i A').'</p>
                            <span class="text-xs">'.Carbon::parse($transaction->created_at)->diffForHumans().'</span>';
                },
            ],
            [
                'label' => __('Details'),
                'render' => function ($transaction) {
                    return '<div class="flex flex-col justify-end">
                        <p>'.$transaction->title.'</p>
                        <span class="text-xs">'.$transaction->note.'</span>
                    </div>';
                },
            ],
        ];

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

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

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

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

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

        $query = Transaction::query();

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

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