<?php

namespace App\Http\Controllers\Admin;

use App\Models\City;
use App\Lib\RequiredConfig;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CityController extends Controller
{
    public function cities() {
        $pageTitle       = 'All Cities';
        $cities       = City::searchable(['name'])->orderBy('id', 'desc')->paginate(getPaginate());
        return view('admin.city.index', compact('pageTitle', 'cities'));
    }

    public function cityStore(Request $request, $id = 0) {
        $request->validate([
            'name'               => 'required|unique:cities,name,' . $id,
        ]);

        if ($id) {
            $city         = City::findOrFail($id);
            $message          = 'City updated successfully';
        } else {
            $city = new City();
            $message  = 'City added successfully';
        }

        $city->name       = $request->name;
        $city->save();

        RequiredConfig::configured('city');
        $notify[] = ['success', $message];
        return back()->withNotify($notify);
    }

    public function status($id) {
        return City::changeStatus($id);
    }
}
