<?php

namespace App\Http\Controllers;

use App\Models\Alumni;
use App\Models\Santri;
use Illuminate\Http\Request;

class AlumniController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $alumnis = Alumni::with('santri')->latest()->paginate(10);
        return view('alumnis.index', compact('alumnis'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('alumnis.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'nama_santri' => 'required|string|max:255',
            'tahun_lulus' => 'required|integer|min:2000|max:' . date('Y'),
            'aktivitas_setelah_lulus' => 'required|string',
            'kontak' => 'nullable|string',
            'keterangan' => 'nullable|string',
        ]);
        \App\Models\Alumni::create($validated);
        return redirect()->route('alumnis.index')->with('success', 'Data alumni berhasil ditambahkan.');
    }

    /**
     * Display the specified resource.
     */
    public function show(Alumni $alumni)
    {
        $alumni->load('santri');
        return view('alumnis.show', compact('alumni'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Alumni $alumni)
    {
        return view('alumnis.edit', compact('alumni'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Alumni $alumni)
    {
        $validated = $request->validate([
            'nama_santri' => 'required|string|max:255',
            'tahun_lulus' => 'required|integer|min:2000|max:' . date('Y'),
            'aktivitas_setelah_lulus' => 'required|string',
            'kontak' => 'nullable|string',
            'keterangan' => 'nullable|string',
        ]);
        $alumni->update($validated);
        return redirect()->route('alumnis.index')->with('success', 'Data alumni berhasil diupdate.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Alumni $alumni)
    {
        $alumni->delete();
        return redirect()->route('alumnis.index')->with('success', 'Data alumni berhasil dihapus.');
    }
}

namespace App\Http\Controllers\API;

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

class AlumniApiController extends Controller
{
    public function index(Request $request)
    {
        $alumni = Alumni::latest()->get();
        return response()->json($alumni);
    }
}
