<?php

namespace App\Http\Controllers\Api\Admin;

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

class ProspekKerjaController 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 = ProspekKerja::with('jurusan')->orderBy('id', 'desc');

            if ($search !== '') {
                $query->where(function ($q) use ($search) {
                    $q->where('nama_profesi', 'like', "%{$search}%")
                        ->orWhere('range_gaji', 'like', "%{$search}%")
                        ->orWhere('deskripsi_pekerjaan', 'like', "%{$search}%")
                        ->orWhereHas('jurusan', function ($jurusan) use ($search) {
                            $jurusan->where('nama_jurusan', 'like', "%{$search}%");
                        });
                });
            }

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

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

    public function store(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'jurusan_id' => 'required|integer|exists:jurusans,id',
                'nama_profesi' => 'required|string|max:255',
                'range_gaji' => 'nullable|string|max:255',
                'deskripsi_pekerjaan' => 'nullable|string',
            ], [
                'jurusan_id.required' => 'Jurusan wajib dipilih.',
                'jurusan_id.exists' => 'Jurusan yang dipilih tidak ditemukan.',
                'nama_profesi.required' => 'Nama profesi wajib diisi.',
            ]);

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

            $prospek = new ProspekKerja();
            $prospek->jurusan_id = $request->input('jurusan_id');
            $prospek->nama_profesi = $request->input('nama_profesi');
            $prospek->range_gaji = $request->input('range_gaji');
            $prospek->deskripsi_pekerjaan = $request->input('deskripsi_pekerjaan');
            $prospek->save();
            $prospek->load('jurusan');

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

    public function show($id)
    {
        try {
            $prospek = ProspekKerja::with('jurusan')->find($id);

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

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

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

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

            $validator = Validator::make($request->all(), [
                'jurusan_id' => 'required|integer|exists:jurusans,id',
                'nama_profesi' => 'required|string|max:255',
                'range_gaji' => 'nullable|string|max:255',
                'deskripsi_pekerjaan' => 'nullable|string',
            ], [
                'jurusan_id.required' => 'Jurusan wajib dipilih.',
                'jurusan_id.exists' => 'Jurusan yang dipilih tidak ditemukan.',
                'nama_profesi.required' => 'Nama profesi wajib diisi.',
            ]);

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

            $prospek->jurusan_id = $request->input('jurusan_id');
            $prospek->nama_profesi = $request->input('nama_profesi');
            $prospek->range_gaji = $request->input('range_gaji');
            $prospek->deskripsi_pekerjaan = $request->input('deskripsi_pekerjaan');
            $prospek->save();
            $prospek->load('jurusan');

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

    public function destroy($id)
    {
        try {
            $prospek = ProspekKerja::find($id);

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

            $prospek->delete();

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