<?php

namespace App\Http\Controllers\Admin\Settings;

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

class StateController extends Controller
{
    public function index()
    {
        $countries = countries();

        $states = State::query();

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

        if (request()->filled('country')) {
            $states->where('country_id', request('country'));
        }

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

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

    public function create()
    {
        $countries = countries();

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

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

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

        $state = new State();
        $state->name = $request->name;
        $state->country_id = $request->country;
        $state->save();

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

    public function edit(State $state)
    {
        $countries = countries();

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

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

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

        $state->name = $request->name;
        $state->country_id = $request->country;
        $state->update();

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

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