<?php

namespace App\Http\Controllers;

use App\Models\City;
use App\Models\District;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class CityController extends Controller
{
    public function index(Request $request)
    {
        $query = City::query()
            ->with(['district:id,division_id,name_bn,name_en'])
            ->when($request->filled('search'), function ($q) use ($request) {
                $s = trim($request->string('search')->toString());
                $q->where(function ($qq) use ($s) {
                    $qq->where('name_bn', 'like', "%{$s}%")
                       ->orWhere('name_en', 'like', "%{$s}%")
                       ->orWhereHas('district', function ($dq) use ($s) {
                           $dq->where('name_bn', 'like', "%{$s}%")
                              ->orWhere('name_en', 'like', "%{$s}%");
                       });
                });
            })
            ->when($request->filled('district_id'), function ($q) use ($request) {
                $q->where('district_id', $request->integer('district_id'));
            });

        $cities = $query->latest('id')->paginate(20)->withQueryString();

        $districts = District::query()
            ->select('id', 'name_bn', 'name_en')
            ->orderBy('name_bn')
            ->get();

        return view('city.index', compact('cities', 'districts'));
    }

    public function create()
    {
        $districts = District::query()
            ->select('id', 'name_bn', 'name_en')
            ->orderBy('name_bn')
            ->get();

        return view('city.create', compact('districts'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'district_id' => ['required', 'exists:districts,id'],
            'name_bn' => ['required', 'string', 'max:255'],
            'name_en' => [
                'required', 'string', 'max:255',
                Rule::unique('cities')->where(fn ($q) => $q->where('district_id', $request->district_id)),
            ],
        ], [
            'district_id.required' => 'জেলা নির্বাচন করুন',
            'name_bn.required' => 'সিটি/পৌরসভার বাংলা নাম দিন',
            'name_en.required' => 'সিটি/পৌরসভার ইংরেজি নাম দিন',
            'name_en.unique' => 'এই জেলায় একই ইংরেজি নামের সিটি/পৌরসভা আগে থেকেই আছে',
        ]);

        City::create($data);

        return redirect()->route('cities.index')
            ->with('success', 'সিটি/পৌরসভা সফলভাবে যোগ করা হয়েছে।');
    }

    public function edit(City $city)
    {
        $districts = District::query()
            ->select('id', 'name_bn', 'name_en')
            ->orderBy('name_bn')
            ->get();

        return view('city.edit', compact('city', 'districts'));
    }

    public function update(Request $request, City $city)
    {
        $data = $request->validate([
            'district_id' => ['required', 'exists:districts,id'],
            'name_bn' => ['required', 'string', 'max:255'],
            'name_en' => [
                'required', 'string', 'max:255',
                Rule::unique('cities')
                    ->ignore($city->id)
                    ->where(fn ($q) => $q->where('district_id', $request->district_id)),
            ],
        ], [
            'district_id.required' => 'জেলা নির্বাচন করুন',
            'name_bn.required' => 'সিটি/পৌরসভার বাংলা নাম দিন',
            'name_en.required' => 'সিটি/পৌরসভার ইংরেজি নাম দিন',
            'name_en.unique' => 'এই জেলায় একই ইংরেজি নামের সিটি/পৌরসভা আগে থেকেই আছে',
        ]);

        $city->update($data);

        return redirect()->route('cities.index')
            ->with('success', 'সিটি/পৌরসভা সফলভাবে আপডেট হয়েছে।');
    }

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

        return redirect()->route('cities.index')
            ->with('success', 'সিটি/পৌরসভা সফলভাবে মুছে ফেলা হয়েছে।');
    }
}
