<?php

namespace Modules\Admin\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Admin\Repositories\MunicipalityRepository;
use Modules\Admin\Services\AdminService;

class MunicipalityController extends Controller
{
    private $municipalityRepository;
    private $adminService;

    public function __construct(MunicipalityRepository $municipalityRepository, AdminService $adminService)
    {
        $this->municipalityRepository = $municipalityRepository;
        $this->adminService = $adminService;
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = 'Municipality List';
        $data['municipalityLists'] = $this->municipalityRepository->findAll();
        return view('admin::municipalities.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = 'Create Municipality';
        $data['editable'] = false;
        $data['districtLists'] = $this->adminService->pluckDistricts();
        return view('admin::municipalities.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        try {
            $this->municipalityRepository->create($request->all());
            toastr()->success('Municipality created successfully');
        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());

        }
        return redirect()->route('municipality.index');
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        return view('admin::municipalities.show');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $data['title'] = 'Edit Municipality';
        $data['editable'] = true;
        $data['municipality'] = $this->municipalityRepository->getMunicipalityById($id);
        $data['districtLists'] = $this->adminService->pluckDistricts();
        return view('admin::municipalities.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id): RedirectResponse
    {
        try {
            $this->municipalityRepository->update($id, $request->all());
            toastr()->success('Municipality updated successfully');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('municipality.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {

            $this->municipalityRepository->delete($id);
            toastr()->success('Municipality deleted successfully');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('municipality.index');
    }
}