<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $data = null;
        $status = '';
        $req_status = 0;
        $isActive = '';
        $message = '';

        try {
            if ($request->email != null && $request->password != null) {
                $data = User::where('email', $request->email)->firstOrFail();
                if (Hash::check($request->password, $data->password)) {
                    $req_status = HttpFoundationResponse::HTTP_OK;
                    $status = 'success';
                    $message = 'Berhasil';
                    $isActive = true;
                    $data->token = $data->createToken('token')->plainTextToken;
                } else {
                    $req_status = HttpFoundationResponse::HTTP_INTERNAL_SERVER_ERROR;
                    $status = 'failed';
                    $message = 'Akun tidak ditemukan';
                }
            } else {
                $req_status = HttpFoundationResponse::HTTP_BAD_REQUEST;
                $status = 'failed';
                $message = 'Bad request';
            }
        } catch (Exception $e) {
            $req_status = HttpFoundationResponse::HTTP_INTERNAL_SERVER_ERROR;
            $status = 'failed';
            $message = 'Terjadi kesalahan : ' . $e->getMessage();
        } catch (QueryException $e) {
            $req_status = HttpFoundationResponse::HTTP_INTERNAL_SERVER_ERROR;
            $status = 'failed';
            $message = 'Terjadi kesalahan pada database: ' . $e->getMessage();
        } finally {
            return response()->json([
                'data' => $data,
                'status' => $status,
                'message' => $message,
                'isActive' => $isActive,
            ], $req_status);
        }
    }

    public function logout(Request $request)
    {
        $status = '';
        $req_status = 0;
        $message = '';

        try {
            $request->user()->currentAccessToken()->delete();
            $req_status = HttpFoundationResponse::HTTP_OK;
            $status = 'success';
            $message = 'Berhasil Logout.';
        } catch (Exception $e) {
            $req_status = HttpFoundationResponse::HTTP_INTERNAL_SERVER_ERROR;
            $status = 'failed';
            $message = 'Terjadi kesalahan : ' . $e->getMessage();
        } catch (QueryException $e) {
            $req_status = HttpFoundationResponse::HTTP_INTERNAL_SERVER_ERROR;
            $status = 'failed';
            $message = 'Terjadi kesalahan pada database: ' . $e->getMessage();
        } finally {
            return response()->json([
                'status' => $status,
                'message' => $message
            ], $req_status);
        }
    }
}
