<?php

namespace App\Http\Controllers\Admin;

use App\Enums\PaymentStatusEnum;
use App\Http\Controllers\Controller;
use App\Models\Payment;
use App\Services\ModalIndexQuey;
use App\Services\Payment\Methods\ManualPaymentService;
use Carbon\Carbon;
use Illuminate\Support\Number;

class PaymentController extends Controller
{
    public function index()
    {
        adminUserHasPermission('read');

        $tab_buttons = [
            [
                'label' => __('All Payments'),
                'link' => route('admin.payments.index'),
            ],
            [
                'label' => __('Manual Pending Payments'),
                'link' => route('admin.payments.index', ['status' => 'manual_pending']),
            ],
            [
                'label' => __('Paid'),
                'link' => route('admin.payments.index', ['status' => PaymentStatusEnum::PAID->value]),
            ],
            [
                'label' => __('Cancelled'),
                'link' => route('admin.payments.index', ['status' => PaymentStatusEnum::CANCELLED->value]),
            ],
        ];

        $status = request('status') ?? '';

        $payments = ModalIndexQuey::get(Payment::query()->when($status, function ($query) use ($status) {
            if ($status === 'manual_pending') {
                $query->whereRelation('paymentMethod', 'type', 'manual')->where('status', PaymentStatusEnum::PENDING->value);
            } else {
                $query->where('status', $status);
            }
        }), ['user', 'paymentMethod']);

        $columns = [
            [
                'label' => __('Customer'),
                'key' => 'user.first_name',
                'render' => function ($payment) {
                    return '<div class="flex max-lg:justify-end items-center gap-3">
                                <img src="'.placeAvatar($payment->user?->avatar, $payment->user?->full_name).'" width="32" height="32"
                                    class="rounded-full max-md:hidden" alt="'.$payment->user?->full_name.'" />
                                <div>
                                    <p class="s-text mb-1 font-medium">'.$payment->user?->full_name.'</p>
                                    <span class="text-xs">'.$payment->user?->email.'</span>
                                    <span class="text-xs">'.$payment->user?->phone.'</span>
                                </div>
                            </div>';
                },
            ],
            [
                'label' => __('Identification'),
                'key' => 'payment_identifier',
                'header_class' => 'lg:w-[200px]',
            ],
            [
                'label' => __('Amount'),
                'key' => 'payable',
                'is_sortable' => true,
                'render' => function ($payment) {
                    return amountWithSymbol(Number::format($payment->payable));
                },
            ],
            [
                'label' => __('Charge'),
                'key' => 'charge',
                'is_sortable' => true,
                'render' => function ($payment) {
                    return amountWithSymbol(Number::format($payment->charge));
                },
            ],
            [
                'label' => __('Status'),
                'key' => 'status',
                'is_sortable' => true,
                'render' => function ($payment) {
                    return '<span class="status '.$payment->status->color().'">'.$payment->status->label().'</span>';
                },
            ],
            [
                'label' => __('Date'),
                'key' => 'created_at',
                'is_sortable' => true,
                'render' => function ($payment) {
                    return $payment->created_at ? '<p class="s-text font-medium">'.Carbon::parse($payment->created_at)->format('Y-m-d H:i A').'</p>
                            <span class="text-xs">'.Carbon::parse($payment->created_at)->diffForHumans().'</span>' : '-';
                },
            ],
            [
                'label' => __('Action'),
                'render' => function ($order) {
                    $action_buttons = [
                        [
                            'label' => __('Show'),
                            'icon' => 'ph ph-eye',
                            'type' => 'link',
                            'href' => route('admin.payments.show', $order->id),
                        ],
                    ];

                    return view('admin.components.table-action', compact('action_buttons'))->render();
                },
            ],
        ];

        return view('admin.pages.payments.page', compact('payments', 'columns', 'tab_buttons'));
    }

    public function show(Payment $payment)
    {
        adminUserHasPermission('read');

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

        $payment->load('user', 'paymentMethod');

        return view('admin.pages.payments.show', compact('payment', 'buttons'));
    }

    public function cancel(Payment $payment, ManualPaymentService $payment_service)
    {
        adminUserHasPermission('edit');

        $payment_service->handleFailed($payment->payment_identifier);

        return redirect()->back()->withSuccess(__('Payment canceled successfully'));
    }

    public function accept(Payment $payment, ManualPaymentService $payment_service)
    {
        adminUserHasPermission('edit');

        $payment_service->handleSuccess($payment->payment_identifier);

        return redirect()->back()->withSuccess(__('Payment accepted successfully'));
    }
}
