<?php

namespace App\Http\Controllers\Admin\Deposit;

use App\Enums\DepositStatusEnum;
use App\Http\Controllers\Controller;
use App\Models\Currency;
use App\Models\Deposit;
use App\Services\ModalIndexQuey;
use Carbon\Carbon;
use Illuminate\Http\Request;

class DepositController extends Controller
{
    public function index(Request $request)
    {
        adminUserHasPermission(permission: 'read');

        $tab_buttons = [
            [
                'label' => __('All Deposits'),
                'link' => route('admin.deposits.index'),
            ],
            [
                'label' => __('Pending'),
                'link' => route('admin.deposits.index', ['status' => DepositStatusEnum::PENDING]),
            ],
            [
                'label' => __('Confirmed'),
                'link' => route('admin.deposits.index', ['status' => DepositStatusEnum::CONFIRMED]),
            ],
            [
                'label' => __('Paid'),
                'link' => route('admin.deposits.index', ['status' => DepositStatusEnum::PAID]),
            ],
            [
                'label' => __('Failed'),
                'link' => route('admin.deposits.index', ['status' => DepositStatusEnum::FAILED]),
            ],
            [
                'label' => __('Cancelled'),
                'link' => route('admin.deposits.index', ['status' => DepositStatusEnum::CANCELLED]),
            ],
        ];

        $deposits = ModalIndexQuey::get(
            Deposit::query()->when(
                $request->status,
                fn ($query, $status) => $query->where('status', $status)
            ),
            [
                'user:id,first_name,last_name,email,phone',
                'payment:id,payment_identifier,currency,status,payment_method_id',
                'payment.paymentMethod:id,name',
            ]
        );

        $defaultCurrency = Currency::getDefaultCurrency();

        $columns = [
            [
                'label' => __('Name'),
                'key' => 'first_name',
                'header_class' => 'lg:w-[350px]',
                'render' => function ($deposit) {
                    return '<a href="'.route('admin.users.edit', $deposit->user->id).'" class="flex max-lg:justify-end items-center gap-3">
                                <img src="'.placeAvatar($deposit->user->avatar, $deposit->user->full_name).'" width="32" height="32"
                                    class="rounded-full max-md:hidden" alt="'.$deposit->user->full_name.'" />
                                <div>
                                    <p class="s-text mb-1 font-medium">'.$deposit->user->full_name.'</p>
                                    <span class="text-xs">'.$deposit->user->email.'</span>
                                </div>
                            </a>';
                },
            ],
            [
                'label' => __('Gateway|Transaction'),
                'key' => 'payment_identifier',
                'header_class' => 'lg:w-[200px]',
                'render' => function ($deposit) {
                    return '<div>
                                <p class="s-text mb-1 font-medium">'.$deposit->payment->paymentMethod->name.'</p>
                                <span class="text-xs">@'.$deposit->payment->payment_identifier.'</span>
                            </div>';
                },
            ],
            [
                'label' => __('Amount'),
                'key' => 'amount',
                'is_sortable' => true,
                'render' => function ($deposit) {
                    return '<p class="s-text font-medium">'.amountWithSymbol($deposit->amount).'</p>';
                },
            ],
            [
                'label' => __('Currency'),
                'key' => 'currency',
                'render' => function ($deposit) {
                    return '<p class="s-text font-medium">'.$deposit->payment->currency.'</p>';
                },
            ],
            [
                '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>';
                },
            ],
            [
                'label' => __('Status'),
                'key' => 'status',
                'is_sortable' => true,
                'render' => function ($deposit) {
                    return '<span class="status '.$deposit->payment->status->color().' capitalize">'.$deposit->payment->status->label().'</span>';
                },
            ],
            [
                'label' => __('Action'),
                'render' => function ($deposit) {
                    return '<a href="'.route('admin.deposits.show', $deposit->id).'" class="btn-primary outlined !py-1.5 !px-4" >
                            <i class="ph ph-eye"></i> Details</a>';
                },
            ],
        ];

        return view('admin.pages.deposits.index', compact('deposits', 'columns', 'tab_buttons'));
    }

    public function show(Request $request, Deposit $deposit)
    {
        adminUserHasPermission(permission: 'read');

        $buttons = [
            [
                'label' => 'Back',
                'link' => route('admin.deposits.index'),
                'type' => 'link',
                'icon' => 'ph ph-arrow-left',
            ],
        ];

        $deposit->load([
            'user:id,first_name,last_name,email,phone',
            'payment:id,payment_identifier,currency,status,payment_method_id',
            'payment.paymentMethod:id,name',
        ]);

        return view('admin.pages.deposits.details', compact('deposit', 'buttons'));
    }
}
