<?php

namespace App\Http\Controllers;

use App\Models\Mahasiswa;
use App\Models\FeeTracking;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ValidasiMahasiswaController extends Controller
{
    public function index(Request $request)
    {
        $search = $request->get('search');

        $query = Mahasiswa::with(['kampus', 'prodi'])
            ->where(function ($query) {
                $query->whereNull('validasi_status')
                    ->orWhere('validasi_status', 'pending');
            })
            ->when($search, function ($query) use ($search) {
                $query->where(function ($q) use ($search) {
                    $q->where('nim', 'like', "%{$search}%")
                        ->orWhere('nama', 'like', "%{$search}%")
                        ->orWhereHas('prodi', function ($prodiQuery) use ($search) {
                            $prodiQuery->where('nama', 'like', "%{$search}%");
                        })
                        ->orWhereHas('kampus', function ($kampusQuery) use ($search) {
                            $kampusQuery->where('nama', 'like', "%{$search}%");
                        });
                });
            });

        $data = $query->orderBy('created_at', 'desc')->get();

        return view('validasi.mahasiswa', compact('data', 'search'));
    }

    public function detail($id)
    {
        $mhs = Mahasiswa::with(['kampus', 'prodi'])->findOrFail($id);
        return view('validasi.mahasiswa.detail', compact('mhs'));
    }

    public function setujui(Request $request, $id)
    {


        $mhs = Mahasiswa::findOrFail($id);

        $updateData = [
            'validasi_status' => 'valid',
            'validasi_at' => now(),
            'validasi_by' => Auth::id(),
        ];

        $mhs->update($updateData);

        FeeTracking::where('mahasiswa_id', $id)->update([
            'status' => 'disetujui'
        ]);

        return back()->with('success', 'Mahasiswa berhasil divalidasi.');
    }

    public function tolak(Request $request, $id)
    {


        $mhs = Mahasiswa::findOrFail($id);

        $mhs->update([
            'validasi_status' => 'ditolak',
            'validasi_at' => now(),
            'validasi_by' => Auth::id(),
        ]);

        return back()->with('success', 'Validasi mahasiswa ditolak.');
    }

    public function bulkSetujui(Request $request)
    {
        $request->validate([
            'ids' => 'required|array',
            'ids.*' => 'exists:mahasiswa,id'
        ]);

        Mahasiswa::whereIn('id', $request->ids)
            ->where(function ($query) {
                $query->whereNull('validasi_status')
                    ->orWhere('validasi_status', 'pending');
            })
            ->update([
                'validasi_status' => 'valid',
                'validasi_at' => now(),
                'validasi_by' => Auth::id(),
            ]);

        FeeTracking::whereIn('mahasiswa_id', $request->ids)->update([
            'status' => 'disetujui'
        ]);

        return back()->with('success', count($request->ids) . ' mahasiswa berhasil divalidasi.');
    }

    public function bulkTolak(Request $request)
    {
        $request->validate([
            'ids' => 'required|array',
            'ids.*' => 'exists:mahasiswa,id',
        ], [
        ]);

        Mahasiswa::whereIn('id', $request->ids)
            ->where(function ($query) {
                $query->whereNull('validasi_status')
                    ->orWhere('validasi_status', 'pending');
            })
            ->update([
                'validasi_status' => 'ditolak',
                'validasi_at' => now(),
                'validasi_by' => Auth::id(),
            ]);

        return back()->with('success', count($request->ids) . ' mahasiswa ditolak.');
    }
}
