<?php

namespace App\Http\Controllers\Api\Student;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Student;

class LogoutController extends Controller
{
    // Student Logout
    public function logout(Request $request)
    {
        try {
            $user = Auth::user();

            if (!$user) {
                return response()->json([
                    'validation_errors' => ['Invalid token or user not authenticated.'],
                    'response_status' => 400,
                ], 200);
            }

            // Invalidate the token
            $user->tokens()->where('id', $user->currentAccessToken()->id)->delete();

            return response()->json([
                'response_message' => ['success' => ['Logged Out Successfully!']],
                'response_status' => 200,
            ], 200);

        } catch (Exception $exception) {
            return response()->json([
                'validation_errors' => $exception->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }
}
