<?php

namespace App\Http\Controllers;

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

class LandmarkController extends Controller
{
    public function index()
    {
        try {
            $landmarks = Landmark::all();
            return response()->json([
                'status' => 'success',
                'message' => 'Data landmarks berhasil dimuat',
                'data' => $landmarks
            ], 200);
        } catch (\Exception $e) {
            return response()->json(['error' => 'Gagal memuat landmarks', 'message' => $e->getMessage()], 500);
        }
    }

    public function store(Request $request)
    {
        try {
            $request->validate([
                'name' => 'required|string|max:255|unique:landmarks',
                'type' => 'required|string|max:255',
            ]);

            $landmark = Landmark::create([
                'name' => $request->name,
                'type' => $request->type
            ]);

            return response()->json(['message' => 'Landmark ditambahkan', 'data' => $landmark], 201);
        } catch (\Exception $e) {
            return response()->json(['error' => 'Gagal menambah landmark', 'message' => $e->getMessage()], 500);
        }
    }

    public function update(Request $request, $id)
    {
        try {
            $landmark = Landmark::findOrFail($id);

            $request->validate([
                'name' => 'sometimes|string|max:255|unique:landmarks,id',
                'type' => 'sometimes|string|max:255',
            ]);
            $landmark->update([
                'name' => $request->name,
                'type' => $request->type
            ]);

            return response()->json(['message' => 'Landmark diperbarui', 'data' => $landmark], 200);
        } catch (\Exception $e) {
            return response()->json(['error' => 'Gagal memperbarui landmark', 'message' => $e->getMessage()], 500);
        }
    }

    public function destroy($id)
    {
        try {
            $landmark = Landmark::findOrFail($id);
            $landmark->delete();

            return response()->json(['message' => 'Landmark dihapus'], 200);
        } catch (\Exception $e) {
            return response()->json(['error' => 'Gagal menghapus landmark', 'message' => $e->getMessage()], 500);
        }
    }
}
