<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\CityRequest;
use App\Models\LocalizationManagement\City;
use App\Models\LocalizationManagement\State;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;

class CityController extends Controller{

    public function __construct(){

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

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

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

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

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

    }


    public function index(): View{

        $cities = City::with('state')
                        ->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.cities.index', compact('cities'));
    }

    public function create(): View{
        $states = State::get(['id', 'name']);
        return view('backend.cities.create', compact('states'));
    }

    public function store(CityRequest $request): RedirectResponse{
        City::create($request->validated());

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

    public function show(City $city): View{
        return view('backend.cities.show', compact('city'));
    }

    public function edit(City $city): View{
        $states = State::get(['id', 'name']);
        return view('backend.cities.edit', compact('city', 'states'));
    }

    public function update(CityRequest $request, City $city): RedirectResponse{
        $city->update($request->validated());

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

    public function destroy(City $city): RedirectResponse{
        $city->delete();

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

    public function get_cities(Request $request): JsonResponse{
        $cities = City::whereStateId($request->state_id)
            ->active()
            ->get(['id', 'name'])
            ->toArray();

        return response()->json($cities);
    }

    public function dtList(){
        $cities = City::active()->get();
       // $cities = collect(null);
        return datatables($cities)
            ->addIndexColumn()
            ->addColumn('type', function ($row){
                return  "Alone";
            })
            //->only()
            ->make(true);
    }
}
