<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\ShippinCompanyRequest;
use App\Models\LocalizationManagement\Country;
use App\Models\OrderManagement\ShippingCompany;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class ShippingCompanyController extends Controller{


    public function __construct(){

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

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

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

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

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

    }

    public function index(): View{
        $shippingCompanies = ShippingCompany::withCount('countries')
            ->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.shipping_companies.index', compact('shippingCompanies'));
    }

    public function create(): View{
        $countries = Country::active()->orderBy('id', 'asc')->get(['id', 'name']);
        return view('backend.shipping_companies.create', compact('countries'));
    }

    public function store(ShippinCompanyRequest $request): RedirectResponse{

        if ($request->validated()) {
            $shippingCompany = ShippingCompany::create($request->except('countries', '_token',"multiselect"));
            $shippingCompany->countries()->sync($request->countries);

            return redirect()->route('admin.shipping_companies.index')->with([
                'message' => action_message("create"),
                'alert-type' => 'success'
            ]);
        }

        return redirect()->back()->with([
            'message' => general_error(),
            'alert-type' => 'danger'
        ]);

    }

    public function show(ShippingCompany $shippingCompany): View{
        return view('backend.shipping_companies.show', compact('shippingCompany'));
    }

    public function edit(ShippingCompany $shippingCompany): View{
        $shippingCompany->with('countries');
        $countries = Country::active()->orderBy('id', 'asc')->get(['id', 'name']);

        return view('backend.shipping_companies.edit', compact('shippingCompany', 'countries'));
    }

    public function update(ShippinCompanyRequest $request, ShippingCompany $shippingCompany): RedirectResponse{

        if ($request->validated()) {
            $shippingCompany->update($request->except('countries', '_token', '_method',"multiselect"));
            $shippingCompany->countries()->sync($request->countries);

            return redirect()->route('admin.shipping_companies.index')->with([
                'message' => action_message("update"),
                'alert-type' => 'success'
            ]);
        }

        return redirect()->back()->with([
            'message' => general_error(),
            'alert-type' => 'danger'
        ]);
    }

    public function destroy(ShippingCompany $shippingCompany): RedirectResponse{
        $shippingCompany->delete();

        return redirect()->route('admin.shipping_companies.index')->with([
            'message' => action_message("delete"),
            'alert-type' => 'success'
        ]);
    }
}
