<?php

namespace App\Http\Controllers;

use App\Models\District;
use App\Models\Division;
use App\Models\Thana;
use App\Models\Upazila;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;

class LocationController extends Controller
{

    public function index()
    {
        $districts = District::with('division')->get();
        $divisions = Division::orderBy('name', 'asc')->get();


        return view('locations.index', compact('districts', 'divisions'));
    }

    public function upazilaList()
    {
        $upazilas = Upazila::with('district')->get();
        $districts = District::orderBy('name', 'asc')->get();

        return view('locations.upazila', compact('upazilas', 'districts'));
    }

    public function upazilaStore(Request $request)
    {
        $request->validate([
            'district_id' => 'required|exists:districts,id',
            'name' => 'required|string|max:255',
            'bn_name' => 'nullable|string|max:255',
            'coverage_status' => 'nullable|string|max:255',
        ]);

        Upazila::create([
            'district_id' => $request->district_id,
            'name' => $request->name,
            'bn_name' => $request->bn_name ?? '',
            'coverage_status' => $request->coverage_status ?? '',
        ]);

        Toastr::success('Upazila created successfully');
        return redirect()->route('upazilaList');
    }
    public function thanaList()
    {
        $thanas = Thana::with('upazila.district')->get();
        $upazilas = Upazila::orderBy('name', 'asc')->get();

        return view('locations.thana', compact('thanas', 'upazilas'));
    }

    public function thanaStore(Request $request)
    {
        $request->validate([
            'upazila_id' => 'required|exists:upazilas,id',
            'name' => 'required|string|max:255',
            'bn_name' => 'nullable|string|max:255',
            'coverage_status' => 'nullable|string|max:255',
        ]);

        Thana::create([
            'upazila_id' => $request->upazila_id,
            'name' => $request->name,
            'bn_name' => $request->bn_name ?? '',
            'coverage_status' => $request->coverage_status ?? '',
        ]);

        Toastr::success('Thana created successfully');
        return redirect()->route('thanaList');
    }


    public function create()
    {
        //
    }

    public function divisionStore(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'bn_name' => 'nullable|string|max:255',
        ]);

        Division::create([
            'name' => $request->name,
            'bn_name' => $request->bn_name ?? '',
        ]);

        Toastr::success('Division created successfully');
        return redirect()->route('location.index');
    }

    public function store(Request $request)
    {
        $request->validate([
            'division_id' => 'required|exists:divisions,id',
            'name' => 'required|string|max:255',
            'bn_name' => 'nullable|string|max:255',
        ]);

        District::create([
            'division_id' => $request->division_id,
            'name' => $request->name,
            'bn_name' => $request->bn_name ?? '',
        ]);

        Toastr::success('District created successfully');
        return redirect()->route('location.index');
    }


    public function show($id)
    {
        //
    }


    public function edit($id)
    {
        $divisions = Division::all();
        $district = District::find($id);

        return view('locations.districtEdit', compact('divisions', 'district'));
    }

    public function update(Request $request, $id)
    {
        $district = District::find($id);
        $district->division_id = $request->division_id;
        $district->name = $request->name;
        $district->bn_name = $request->bn_name;
        $district->save();

        Toastr::success("successfully Updated");
        return redirect()->route('location.index');
    }

    public function upazilaEdit($id)
    {
        $districts = District::all();
        $upazila = Upazila::find($id);

        return view('locations.upazilaEdit', compact('districts', 'upazila'));
    }

    public function upazilaUpdate(Request $request)
    {
        $upazila = Upazila::find($request->id);
        $upazila->district_id = $request->district_id;
        $upazila->name = $request->name;
        $upazila->bn_name = $request->bn_name;
        $upazila->coverage_status = $request->coverage_status;
        $upazila->save();

        Toastr::success("successfully Updated");
        return redirect()->route('upazilaList');
    }

    public function thanaEdit($id)
    {
        $upazilas = Upazila::all();
        $thana = Thana::find($id);

        return view('locations.thanaEdit', compact('upazilas', 'thana'));
    }

    public function thanaUpdate(Request $request)
    {
        $upazila = Thana::find($request->id);
        $upazila->upazila_id = $request->upazila_id;
        $upazila->name = $request->name;
        $upazila->bn_name = $request->bn_name;
        $upazila->coverage_status = $request->coverage_status;
        $upazila->save();

        Toastr::success("successfully Updated");
        return redirect()->route('thanaList');
    }


    public function destroy($id)
    {
        $district = District::find($id);
        $district->deleted_by = auth()->user()->id;
        $district->save();
        $district->delete();

        Toastr::success("successfully Deleted");
        return redirect()->route('location.index');
    }
}
