<?php

namespace App\Http\Controllers\Admin\Settings;

use App\Http\Controllers\Controller;
use App\Models\City;
use App\Models\State;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CityController extends Controller
{
    public function index()
    {
        $states = State::orderbyDesc('id')->get();

        $cities = City::query();

        if (request()->filled('search')) {
            $searchTerm = '%' . request('search') . '%';
            $cities->where(function ($query) use ($searchTerm) {
                $query->where('name', 'like', $searchTerm);
            });
        }

        if (request()->filled('state')) {
            $cities->where('state_id', request('state'));
        }

        $cities = $cities->orderbyDesc('id')->paginate(50);
        $cities->appends(request()->only(['search']));

        return view('admin.settings.countries.cities.index', [
            'states' => $states,
            'cities' => $cities,
        ]);
    }

    public function create()
    {
        $states = State::orderbyDesc('id')->get();

        return view('admin.settings.countries.cities.create', [
            'states' => $states,
        ]);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => ['required', 'string', 'block_patterns', 'max:255'],
            'state' => ['required', 'integer', 'exists:states,id'],
        ]);

        if ($validator->fails()) {
            foreach ($validator->errors()->all() as $error) {
                toastr()->error($error);
            }
            return back()->withInput();
        }

        $city = new City();
        $city->name = $request->name;
        $city->state_id = $request->state;
        $city->save();

        toastr()->success(d_trans('Created Successfully'));
        return redirect()->route('admin.settings.countries.cities.edit', $city->id);
    }

    public function edit(City $city)
    {
        $states = State::orderbyDesc('id')->get();

        return view('admin.settings.countries.cities.edit', [
            'city' => $city,
            'states' => $states,
        ]);
    }

    public function update(Request $request, City $city)
    {
        $validator = Validator::make($request->all(), [
            'name' => ['required', 'string', 'block_patterns', 'max:255'],
            'state' => ['required', 'integer', 'exists:states,id'],
        ]);

        if ($validator->fails()) {
            foreach ($validator->errors()->all() as $error) {
                toastr()->error($error);
            }
            return back();
        }

        $city->name = $request->name;
        $city->state_id = $request->state;
        $city->update();

        toastr()->success(d_trans('Updated Successfully'));
        return back();
    }

    public function destroy(City $city)
    {
        $city->delete();
        toastr()->success(d_trans('Deleted Successfully'));
        return back();
    }
}
