<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\RiwayatRekomendasi;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Throwable;

class RiwayatController 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 = RiwayatRekomendasi::with('user:id,name,email')
                ->orderBy('created_at', 'desc');

            if ($search !== '') {
                $query->where(function ($q) use ($search) {
                    $q->where('hasil_rekomendasi_jurusan', 'like', "%{$search}%")
                        ->orWhereHas('user', function ($user) use ($search) {
                            $user->where('name', 'like', "%{$search}%")
                                ->orWhere('email', 'like', "%{$search}%");
                        });
                });
            }

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

            $riwayat->getCollection()->transform(function ($item) {
                return [
                    'id' => $item->id,
                    'tanggal' => $item->created_at ? $item->created_at->format('d M Y') : '-',
                    'jam' => $item->created_at ? $item->created_at->format('H:i') : '-',
                    'nama_siswa' => $item->user ? $item->user->name : 'Siswa Tidak Diketahui',
                    'email_siswa' => $item->user ? $item->user->email : '-',
                    'hasil' => $item->hasil_rekomendasi_jurusan ?? 'Belum ada hasil',
                    'akurasi_prediksi' => $this->getAkurasiPrediksi($item),
                    'minat_utama' => $this->getMinatUtama($item),
                    'rata_nilai' => $this->getRataNilai($item),
                ];
            });

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

    public function show($id)
    {
        try {
            $detail = RiwayatRekomendasi::with('user:id,name,email')->find($id);

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

            return response()->json([
                'success' => true,
                'message' => 'Detail riwayat berhasil ditemukan.',
                'data' => [
                    'id' => $detail->id,
                    'tanggal' => $detail->created_at ? $detail->created_at->format('d M Y H:i:s') : '-',
                    'nama_siswa' => $detail->user ? $detail->user->name : 'Siswa Tidak Diketahui',
                    'email_siswa' => $detail->user ? $detail->user->email : '-',
                    'hasil_rekomendasi' => $detail->hasil_rekomendasi_jurusan ?? 'Belum ada hasil',
                    'akurasi_prediksi' => $this->getAkurasiPrediksi($detail),
                    'minat_utama' => $this->getMinatUtama($detail),
                    'rata_nilai' => $this->getRataNilai($detail),
                    'kondisi_ekonomi' => $this->getLabelEkonomi($detail->gaji_ortu),
                    'gaji_ortu' => $detail->gaji_ortu,
                    'nilai_ringkas' => [
                        'Agama' => $detail->agama,
                        'PKN' => $detail->pkn,
                        'B. Indonesia' => $detail->bahasa_indonesia,
                        'B. Inggris' => $detail->bahasa_inggris,
                        'Matematika' => $detail->matematika,
                        'Fisika' => $detail->fisika,
                        'Kimia' => $detail->kimia,
                        'Biologi' => $detail->biologi,
                        'Sejarah' => $detail->sejarah,
                        'Geografi' => $detail->geografi,
                        'Ekonomi' => $detail->ekonomi_mapel,
                        'Sosiologi' => $detail->sosiologi,
                        'Seni Budaya' => $detail->seni_budaya,
                        'PJOK' => $detail->pjok,
                        'Informatika' => $detail->informatika,
                    ],
                    'riasec' => [
                        'Realistic' => $detail->riasec_r,
                        'Investigative' => $detail->riasec_i,
                        'Artistic' => $detail->riasec_a,
                        'Social' => $detail->riasec_s,
                        'Enterprising' => $detail->riasec_e,
                        'Conventional' => $detail->riasec_c,
                    ],
                ],
            ], 200);
        } catch (Throwable $e) {
            return response()->json([
                'success' => false,
                'message' => 'Terjadi kesalahan saat mengambil detail riwayat.',
                'error' => $e->getMessage(),
            ], 500);
        }
    }

    public function destroy($id)
    {
        try {
            $riwayat = RiwayatRekomendasi::find($id);

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

            $riwayat->delete();

            return response()->json([
                'success' => true,
                'message' => 'Data riwayat berhasil dihapus.',
            ], 200);
        } catch (Throwable $e) {
            return response()->json([
                'success' => false,
                'message' => 'Data riwayat gagal dihapus.',
                'error' => $e->getMessage(),
            ], 500);
        }
    }

    private function getAkurasiPrediksi($riwayat): ?float
    {
        $tableName = (new RiwayatRekomendasi())->getTable();

        foreach (['akurasi_prediksi', 'probabilitas', 'akurasi'] as $column) {
            if (Schema::hasColumn($tableName, $column) && $riwayat->{$column} !== null) {
                return round((float) $riwayat->{$column}, 2);
            }
        }

        return null;
    }

    private function getRataNilai($riwayat): float
    {
        $nilai = [
            $riwayat->agama,
            $riwayat->pkn,
            $riwayat->bahasa_indonesia,
            $riwayat->bahasa_inggris,
            $riwayat->matematika,
            $riwayat->fisika,
            $riwayat->kimia,
            $riwayat->biologi,
            $riwayat->sejarah,
            $riwayat->geografi,
            $riwayat->ekonomi_mapel,
            $riwayat->sosiologi,
            $riwayat->seni_budaya,
            $riwayat->pjok,
            $riwayat->informatika,
        ];

        $nilai = array_filter($nilai, fn ($item) => $item !== null && $item !== '');

        if (count($nilai) === 0) {
            return 0;
        }

        return round(array_sum($nilai) / count($nilai), 2);
    }

    private function getMinatUtama($riwayat): string
    {
        $scores = [
            'Teknis & Lapangan' => (float) $riwayat->riasec_r,
            'Sains, Riset & Analitis' => (float) $riwayat->riasec_i,
            'Kreatif & Desain' => (float) $riwayat->riasec_a,
            'Sosial & Pelayanan' => (float) $riwayat->riasec_s,
            'Bisnis & Kepemimpinan' => (float) $riwayat->riasec_e,
            'Administratif & Terstruktur' => (float) $riwayat->riasec_c,
        ];

        arsort($scores);

        return array_key_first($scores) ?? 'Belum teridentifikasi';
    }

    private function getLabelEkonomi($gaji): string
    {
        if ($gaji === null || $gaji === '') {
            return '-';
        }

        $gaji = (float) $gaji;

        if ($gaji < 2000000) {
            return 'Kurang Mampu';
        }

        if ($gaji <= 5000000) {
            return 'Menengah';
        }

        return 'Mampu';
    }
}