<?php

namespace App\Http\Controllers\Api\Admin;

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

class JurusanController extends Controller
{
    public function index()
    {
        $jurusan = Jurusan::all();
        return response()->json(['success' => true, 'data' => $jurusan], 200);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'nama_jurusan' => 'required|string|max:255',
            'rumpun' => 'required|string',
        ]);

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

        $jurusan = Jurusan::create($request->all());
        return response()->json(['success' => true, 'message' => 'Jurusan berhasil ditambahkan', 'data' => $jurusan], 201);
    }

    public function show($id)
    {
        $jurusan = Jurusan::find($id);
        if (!$jurusan) return response()->json(['success' => false, 'message' => 'Data tidak ditemukan'], 404);
        return response()->json(['success' => true, 'data' => $jurusan], 200);
    }

    public function update(Request $request, $id)
    {
        $jurusan = Jurusan::find($id);
        if (!$jurusan) return response()->json(['success' => false, 'message' => 'Data tidak ditemukan'], 404);

        $jurusan->update($request->all());
        return response()->json(['success' => true, 'message' => 'Jurusan berhasil diperbarui', 'data' => $jurusan], 200);
    }

    public function destroy($id)
    {
        $jurusan = Jurusan::find($id);
        if (!$jurusan) return response()->json(['success' => false, 'message' => 'Data tidak ditemukan'], 404);

        $jurusan->delete();
        return response()->json(['success' => true, 'message' => 'Jurusan berhasil dihapus'], 200);
    }
}