<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class StudentProfileController extends Controller
{
    /**
     * Show the authenticated student's personal profile.
     */
    public function show()
    {
        $student = Auth::user();
        $registrationNumber = strtoupper($student->registration_number ?? $student->username);

        // Check registration details
        $registration = DB::table('registrations')
            ->where(DB::raw('UPPER(registration_number)'), $registrationNumber)
            ->first();

        $programme      = 'N/A';
        $attendanceMode = 'N/A';
        $credits        = 'N/A';
        $duration       = 'N/A';
        $status         = 'N/A';

        if ($registration && $registration->programme_code) {
            $programmeRecord = DB::table('programmes')
                ->where('programme_code', $registration->programme_code)
                ->first();

            if ($programmeRecord) {
                $programme      = $programmeRecord->description ?? 'N/A';
                $attendanceMode = $programmeRecord->attendance_mode ?? 'N/A';
                $credits        = $programmeRecord->total_credits ?? 'N/A';
                $duration       = $programmeRecord->duration ?? 'N/A';
                $status         = $programmeRecord->status ?? 'N/A';
            }
        }

        return view('student.profile', compact(
            'student',
            'registrationNumber',
            'programme',
            'attendanceMode',
            'credits',
            'duration',
            'status'
        ));
    }

    /**
     * Update student profile.
     */
    public function update(Request $request)
    {
        $request->validate([
            'first_name'   => 'required|string|max:255',
            'last_name'    => 'required|string|max:255',
            'national_id'  => 'required|string|max:20',
            'dob'          => 'required|date',
        ]);

        $user = Auth::user();
        $user->first_name   = $request->first_name;
        $user->last_name    = $request->last_name;
        $user->national_id  = $request->national_id;
        $user->dob          = $request->dob;
        $user->save();

        return redirect()->back()->with('success', 'Profile updated successfully.');
    }

    /**
     * Show registered programme from `programmes` table.
     */
    public function showProgramme()
    {
        $student = Auth::user();
        $registrationNumber = strtoupper($student->registration_number ?? $student->username);

        $registration = DB::table('registrations')
            ->where(DB::raw('UPPER(registration_number)'), $registrationNumber)
            ->first();

        if (!$registration) {
            return view('student.programmes', ['message' => 'You are not registered for any programme.']);
        }

        $programme = DB::table('programmes')
            ->where('programme_code', $registration->programme_code)
            ->first();

        return view('student.programmes', [
            'registrationNumber' => $registrationNumber,
            'programmeCode' => $registration->programme_code ?? 'N/A',
            'programme' => $programme
        ]);
    }
}
