<?php

namespace App\Http\Controllers\Admin\Settings;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Settings\WithdrawMethodRequest;
use App\Models\WithdrawMethod;
use App\Services\ModalIndexQuey;
use Illuminate\Support\Facades\Cache;

class WithdrawMethodController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        adminUserHasPermission(permission: 'read');

        $buttons = [
            [
                'label' => __('Create Withdraw Method'),
                'icon' => 'ph ph-plus-bold',
                'type' => 'link',
                'link' => route('admin.settings.withdraw-methods.create'),
            ],
        ];

        $withdraw_methods = ModalIndexQuey::get(model: WithdrawMethod::query());

        $columns = [
            [
                'label' => __('Name'),
                'key' => 'name',
                'header_class' => 'lg:w-[350px]',
                'is_sortable' => true,
            ],
            [
                'label' => __('Minimum Amount'),
                'key' => 'min',
            ],
            [
                'label' => __('Maximum Amount'),
                'key' => 'max',
            ],
            [
                'label' => __('Percentage Fee'),
                'key' => 'percent_charge',
            ],
            [
                'label' => __('Fixed Fee'),
                'key' => 'fixed_charge',
            ],
            [
                'label' => __('Currency'),
                'key' => 'currency',
            ],
            [
                'label' => __('Status'),
                'key' => 'status',
                'is_sortable' => true,
                'render' => function ($withdraw_method) {
                    $color = $withdraw_method->status == 'active' ? 'success' : 'danger';

                    return '<span class="status '.$color.' capitalize">'.__($withdraw_method->status).'</span>';
                },
            ],
            [
                'label' => __('Action'),
                'render' => function ($withdraw_method) {
                    $action_buttons = [
                        [
                            'label' => __('Edit'),
                            'icon' => 'ph ph-pencil',
                            'type' => 'link',
                            'href' => route('admin.settings.withdraw-methods.edit', $withdraw_method->id),
                        ],
                        [
                            'label' => __('Delete'),
                            'icon' => 'ph ph-trash',
                            'type' => 'delete',
                            'class' => 'form-submit-delete',
                            'href' => route('admin.settings.withdraw-methods.destroy', $withdraw_method->id),
                        ],
                    ];

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

        return view('admin.pages.settings.withdraw-method.index', compact('columns', 'withdraw_methods', 'buttons'));
    }

    public function create()
    {
        adminUserHasPermission(permission: 'create');

        $buttons = [
            [
                'label' => __('Back'),
                'icon' => 'lets-icons:back',
                'type' => 'link',
                'link' => route('admin.settings.withdraw-methods.index'),
            ],
        ];

        return view('admin.pages.settings.withdraw-method.create', compact('buttons'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(WithdrawMethodRequest $request)
    {
        adminUserHasPermission(permission: 'create');
        WithdrawMethod::create($request->validated());

        Cache::forget('withdraw_method_list');

        return response()->json(['message' => __('Withdraw Method created successfully'), 'redirect' => route('admin.settings.withdraw-methods.index')]);
    }

    public function edit(WithdrawMethod $withdrawMethod)
    {
        adminUserHasPermission(permission: 'edit');

        $buttons = [
            [
                'label' => __('Back'),
                'icon' => 'lets-icons:back',
                'type' => 'link',
                'link' => route('admin.settings.withdraw-methods.index'),
            ],
        ];

        return view('admin.pages.settings.withdraw-method.edit', compact('withdrawMethod', 'buttons'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(WithdrawMethodRequest $request, WithdrawMethod $withdrawMethod)
    {
        adminUserHasPermission(permission: 'edit');

        $withdrawMethod->update($request->validated());

        Cache::forget('withdraw_method_list');

        return response()->json(['message' => __('Withdraw Method updated successfully'), 'redirect' => route('admin.settings.withdraw-methods.index')]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(WithdrawMethod $withdrawMethod)
    {
        adminUserHasPermission(permission: 'delete');
        $withdrawMethod->delete();

        Cache::forget('withdraw_method_list');

        return back()->withSuccess(__('Withdraw Method deleted successfully'));
    }
}
