<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MapPoint;
use Illuminate\Http\Request;

class MapPointController extends Controller
{
    public function index()
    {
        $points = MapPoint::orderBy('jenis')->get();
        return view('admin.map-points.index', compact('points'));
    }

    public function create()
    {
        return view('admin.map-points.form', ['point' => new MapPoint()]);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'nama'       => 'required|string|max:255',
            'jenis'      => 'required|in:batas_desa,dusun,kantor_desa,jalan_utama,fasilitas_umum,objek_wisata',
            'deskripsi'  => 'nullable|string',
            'latitude'   => 'required|numeric|between:-90,90',
            'longitude'  => 'required|numeric|between:-180,180',
            'icon'       => 'nullable|string|max:100',
        ]);

        MapPoint::create($validated);
        return redirect()->route('admin.peta.index')->with('success', 'Titik peta berhasil ditambahkan.');
    }

    public function edit(MapPoint $peta)
    {
        return view('admin.map-points.form', ['point' => $peta]);
    }

    public function update(Request $request, MapPoint $peta)
    {
        $validated = $request->validate([
            'nama'       => 'required|string|max:255',
            'jenis'      => 'required|in:batas_desa,dusun,kantor_desa,jalan_utama,fasilitas_umum,objek_wisata',
            'deskripsi'  => 'nullable|string',
            'latitude'   => 'required|numeric|between:-90,90',
            'longitude'  => 'required|numeric|between:-180,180',
            'icon'       => 'nullable|string|max:100',
        ]);

        $peta->update($validated);
        return redirect()->route('admin.peta.index')->with('success', 'Titik peta berhasil diperbarui.');
    }

    public function destroy(MapPoint $peta)
    {
        $peta->delete();
        return redirect()->route('admin.peta.index')->with('success', 'Titik peta berhasil dihapus.');
    }
}
