<?php

namespace App\Http\Controllers;

use App\Models\Continent;
use Illuminate\Http\Request;

class ContinentController extends Controller
{
    public function index()
    {
        $continents = Continent::orderBy('name')->get();

        return view('geography.continents.index', compact('continents'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100',
            'code' => 'required|string|max:10|unique:continents,code',
            'status' => 'required|in:0,1',
        ]);

        Continent::create($validated);

        return redirect()->route('continents.index')->with('success', 'Continent added.');
    }

    public function edit(Continent $continent)
    {
        return view('geography.continents.edit', compact('continent'));
    }

    public function update(Request $request, Continent $continent)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100',
            'code' => 'required|string|max:10|unique:continents,code,' . $continent->id,
            'status' => 'required|in:0,1',
        ]);

        $continent->update($validated);

        return redirect()->route('continents.index')->with('success', 'Continent updated.');
    }

    public function destroy(Continent $continent)
    {
        $continent->delete();

        return redirect()->route('continents.index')->with('success', 'Continent deleted.');
    }
}
