<?php

namespace App\Http\Controllers;

use App\Http\Services\LoginService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    protected $loginService;

    public function __construct(LoginService $loginService)
    {
        $this->loginService = $loginService;
    }

    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'account' => 'required',
            'password' => 'required',
        ], [
            'account.required' => '請輸入帳號',
            'password.required' => '請輸入密碼',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'errors' => $validator->errors(),
            ], 422);
        }

        return $this->loginService->attemptLogin($request->account, $request->password);
    }

    public function logout(Request $request)
    {
        $user = $request->user();
        // $user->tokens()->delete();

        return $user;
    }
}
