<?php

namespace App\Http\Controllers\Api;

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

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $credentials = $request->only('email', 'password'); // or 'username' if you use that

        //if($request->client_id != env('ANALYZER_CLIENT_ID') || $request->client_secret != env('ANALYZER_CLIENT_SECRET')){
           // return response()->json(['error' => 'Invalid credentials'], 401);
        //}

        if (!Auth::attempt($credentials)) {
            return response()->json(['error' => 'Invalid credentials'], 401);
        }

        $user = Auth::user();
        $token = $user->createToken('api-token')->accessToken;
        return response()->json([
            'token_type' => 'Bearer',
            'expires_in' => 1800,
            'access_token' => $token,
        ]);
    }

    public function logout(Request $request)
    {
        $request->user()->token()->delete();
        return response()->json(['message' => 'Logged out successfully']);
    }
}
