<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\InternshipBookingParticipant;
use App\Models\MasterData;

class ParticipantController extends Controller
{
    /**
     * Dashboard interaktif Peserta Magang
     */
    public function dashboard(Request $request)
    {
        $user = auth()->user();

        // Cek status booking user saat ini
        $participant = InternshipBookingParticipant::with('booking.instansi', 'booking.status')
            ->where('user_id', $user->id)
            ->latest()
            ->first();

        // Logika untuk menampilkan menu absensi hanya jika status = diterima
        $statusAcceptedId = MasterData::idFor(MasterData::BOOKING_STATUS, 'accepted');
        $isAccepted = $participant && $participant->booking->status_id === $statusAcceptedId;

        return view('participant.dashboard', compact('user', 'participant', 'isAccepted'));
    }

    /**
     * Lengkapi profil peserta
     */
    public function updateProfile(Request $request)
    {
        $request->validate([
            'nama' => 'required|string',
            'no_hp' => 'required|string',
            'asal_instansi' => 'required|string'
        ]);
        
        $user = auth()->user();
        $user->update($request->only('nama', 'no_hp', 'asal_instansi'));
        return back()->with('success', 'Profil berhasil diperbarui');
    }
}
