<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\Alumni;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Throwable;

class AlumniController extends Controller
{
    public function index(Request $request)
    {
        try {
            $perPage = (int) $request->input('per_page', 7);
            $perPage = in_array($perPage, [5, 7, 10, 25, 50, 100]) ? $perPage : 7;

            $search = trim((string) $request->input('search', ''));

            $query = Alumni::query()->orderBy('id', 'desc');

            if ($search !== '') {
                $query->where(function ($q) use ($search) {
                    $q->where('id_alumni', 'like', "%{$search}%")
                        ->orWhere('nama_alumni', 'like', "%{$search}%")
                        ->orWhere('perguruan_tinggi', 'like', "%{$search}%")
                        ->orWhere('jurusan_diambil', 'like', "%{$search}%");
                });
            }

            $alumni = $query->paginate($perPage);

            return response()->json([
                'success' => true,
                'message' => 'Data alumni berhasil dimuat.',
                'data' => $alumni->items(),
                'current_page' => $alumni->currentPage(),
                'last_page' => $alumni->lastPage(),
                'per_page' => $alumni->perPage(),
                'total' => $alumni->total(),
                'from' => $alumni->firstItem(),
                'to' => $alumni->lastItem(),
            ], 200);
        } catch (Throwable $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal memuat data alumni.',
                'error' => $e->getMessage(),
            ], 500);
        }
    }

    public function store(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'nama_alumni' => 'required|string|max:255',
                'perguruan_tinggi' => 'required|string|max:255',
                'jurusan_diambil' => 'required|string|max:255',
            ], [
                'nama_alumni.required' => 'Nama alumni wajib diisi.',
                'perguruan_tinggi.required' => 'Nama perguruan tinggi wajib diisi.',
                'jurusan_diambil.required' => 'Jurusan yang diambil wajib diisi.',
            ]);

            if ($validator->fails()) {
                return response()->json([
                    'success' => false,
                    'message' => 'Validasi gagal.',
                    'errors' => $validator->errors(),
                ], 422);
            }

            $alumni = new Alumni();
            $alumni->nama_alumni = $request->input('nama_alumni');
            $alumni->perguruan_tinggi = $request->input('perguruan_tinggi');
            $alumni->jurusan_diambil = $request->input('jurusan_diambil');
            $alumni->profil_nilai = $request->input('profil_nilai');
            $alumni->profil_minat = $request->input('profil_minat');
            $alumni->save();

            return response()->json([
                'success' => true,
                'message' => 'Data alumni berhasil ditambahkan.',
                'data' => $alumni,
            ], 201);
        } catch (Throwable $e) {
            return response()->json([
                'success' => false,
                'message' => 'Terjadi kesalahan saat menyimpan data alumni.',
                'error' => $e->getMessage(),
            ], 500);
        }
    }

    public function show($id)
    {
        try {
            $alumni = Alumni::find($id);

            if (!$alumni) {
                return response()->json([
                    'success' => false,
                    'message' => 'Data alumni tidak ditemukan.',
                ], 404);
            }

            return response()->json([
                'success' => true,
                'message' => 'Data alumni berhasil ditemukan.',
                'data' => $alumni,
            ], 200);
        } catch (Throwable $e) {
            return response()->json([
                'success' => false,
                'message' => 'Terjadi kesalahan saat mengambil data alumni.',
                'error' => $e->getMessage(),
            ], 500);
        }
    }

    public function update(Request $request, $id)
    {
        try {
            $alumni = Alumni::find($id);

            if (!$alumni) {
                return response()->json([
                    'success' => false,
                    'message' => 'Data alumni tidak ditemukan.',
                ], 404);
            }

            $validator = Validator::make($request->all(), [
                'nama_alumni' => 'required|string|max:255',
                'perguruan_tinggi' => 'required|string|max:255',
                'jurusan_diambil' => 'required|string|max:255',
            ], [
                'nama_alumni.required' => 'Nama alumni wajib diisi.',
                'perguruan_tinggi.required' => 'Nama perguruan tinggi wajib diisi.',
                'jurusan_diambil.required' => 'Jurusan yang diambil wajib diisi.',
            ]);

            if ($validator->fails()) {
                return response()->json([
                    'success' => false,
                    'message' => 'Validasi gagal.',
                    'errors' => $validator->errors(),
                ], 422);
            }

            $alumni->nama_alumni = $request->input('nama_alumni');
            $alumni->perguruan_tinggi = $request->input('perguruan_tinggi');
            $alumni->jurusan_diambil = $request->input('jurusan_diambil');
            $alumni->profil_nilai = $request->input('profil_nilai');
            $alumni->profil_minat = $request->input('profil_minat');
            $alumni->save();

            return response()->json([
                'success' => true,
                'message' => 'Data alumni berhasil diperbarui.',
                'data' => $alumni,
            ], 200);
        } catch (Throwable $e) {
            return response()->json([
                'success' => false,
                'message' => 'Terjadi kesalahan saat memperbarui data alumni.',
                'error' => $e->getMessage(),
            ], 500);
        }
    }

    public function destroy($id)
    {
        try {
            $alumni = Alumni::find($id);

            if (!$alumni) {
                return response()->json([
                    'success' => false,
                    'message' => 'Data alumni tidak ditemukan.',
                ], 404);
            }

            $alumni->delete();

            return response()->json([
                'success' => true,
                'message' => 'Data alumni berhasil dihapus.',
            ], 200);
        } catch (Throwable $e) {
            return response()->json([
                'success' => false,
                'message' => 'Data alumni tidak dapat dihapus karena masih digunakan atau terjadi kesalahan server.',
                'error' => $e->getMessage(),
            ], 500);
        }
    }
}