<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\CountryRequest;
use App\Models\LocalizationManagement\Country;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;

class CountryController extends Controller{

    public function __construct(){

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

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

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

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

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

    }

    public function index(): View{

        $countries = Country::with('states')
            ->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.countries.index', compact('countries'));
    }

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

    public function store(CountryRequest $request): RedirectResponse{
        Country::create($request->validated());
        return redirect()->route('admin.countries.index')->with([
            'message' => action_message("create"),
            'alert-type' => 'success'
        ]);
    }

    public function show(Country $country): View{
        return view('backend.countries.show', compact('country'));
    }

    public function edit(Country $country): View{
        return view('backend.countries.edit', compact('country'));
    }

    public function update(CountryRequest $request, Country $country): RedirectResponse{
        $country->update($request->validated());
        return redirect()->route('admin.countries.index')->with([
            'message' => action_message("update"),
            'alert-type' => 'success'
        ]);
    }

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