<?php

namespace App\Http\Controllers;

use App\Models\ORM\Place;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;

class PlacesController extends Controller
{
    public function index(): View
    {
        $places = Place::paginate(10);
        return view('places.index', compact('places'));
    }

    public function store(Request $request): Redirector|RedirectResponse {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'lat' => 'required|numeric',
            'lng' => 'required|numeric',
        ]);

        Place::create($data);

        return redirect()
            ->route('places.index')
            ->with('status', 'Локация создана!');
    }

    public function edit(Place $place): VIEW
    {
        return view('places.edit', compact('place'));
    }

    public function update(Request $request, Place $place): Redirector|RedirectResponse
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'lat' => 'required|numeric',
            'lng' => 'required|numeric',
        ]);
        $place->update($data);
        return redirect()
            ->route('places.index')
            ->with('status', 'Обновлено!');
    }

    public function destroy(Place $place): Redirector|RedirectResponse {
        $place->delete();
        return redirect()
            ->route('places.index')
            ->with('status', 'Удалено!');
    }
}
