<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\PaymentMethodRequest;
use App\Models\PaymentManagement\PaymentMethod;
use App\Services\ImageService;
use App\Traits\ImageUploadTrait;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;


class PaymentMethodController extends Controller{

    use ImageUploadTrait;

    public function __construct(){

        $this->middleware('permission:payment-method-list')
            ->only(["index"]);

        $this->middleware('permission:payment-method-show')
            ->only(["show"]);

        $this->middleware('permission:payment-method-create')
            ->only(["store"]);

        $this->middleware('permission:payment-method-update')
            ->only(['update',"edit"]);

        $this->middleware('permission:payment-method-delete')
            ->only(['destroy']);

    }

    public function index(): View{
        $paymentMethods = PaymentMethod::query()
                            ->when(request()->keyword != null, function ($query) {
                                $query->search(request()->keyword);
                            })
                            ->when(request()->active != null, function ($query) {
                                $query->whereStatus(request()->active);
                            })
                            ->orderBy(request()->sortBy ?? 'id', request()->orderBy ?? 'desc')
                            ->paginate(request()->limitBy ?? 10);

        return view('backend.payment_methods.index', compact('paymentMethods'));
    }

    public function create(): View{
        return view('backend.payment_methods.create');
    }

    public function store(PaymentMethodRequest $request){
       return  DB::transaction(function () use ($request){
           try {
               $paymentMethod =  PaymentMethod::create($request->except('image',"submit","_token"));

               if ($request->filled("image")) {
                   $paymentMethod->createMedia([$request->file("image")],1,400,400);
               }

               clear_cache();

               return redirect()->route('admin.payment_methods.index')->with([
                   'message' => action_message("create"),
                   'alert-type' => 'success'
               ]);
           } catch (\Exception $exception){
               DB::rollBack();
               throw $exception;
           }
       });
    }

    public function show(PaymentMethod $paymentMethod): View{
        return view('backend.payment_methods.show', compact('paymentMethod'));
    }

    public function edit(PaymentMethod $paymentMethod): View{
        return view('backend.payment_methods.edit', compact('paymentMethod'));
    }

    public function update(PaymentMethodRequest $request, PaymentMethod $paymentMethod): RedirectResponse{
        return DB::transaction(function () use ($request , $paymentMethod) {
            try {
                $paymentMethod->update($request->validated());

                return redirect()->route('admin.payment_methods.index')->with([
                    'message' => action_message("update"),
                    'alert-type' => 'success'
                ]);
            } catch (\Exception $exception){
                DB::rollBack();
                throw $exception;
            }
        });
    }

    public function destroy(PaymentMethod $paymentMethod): RedirectResponse{
        $paymentMethod->delete();
        return redirect()->route('admin.payment_methods.index')->with([
            'message' => action_message("delete"),
            'alert-type' => 'success'
        ]);
    }
}
