<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use App\Helpers\ApiHelper;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    public function login(Request $request)
    {
        try {

            $validator = Validator::make($request->all(), [
                'email' => 'required|email',
                'password' => 'required',
            ]);

            if ($validator->fails()) {

                return ApiHelper::validationError(
                    $validator->errors()->first(),
                    $validator->errors()
                );
            }

            if (!Auth::attempt([
                'email' => $request->email,
                'password' => $request->password
            ])) {

                return ApiHelper::unauthorized(
                    'Invalid credentials'
                );
            }

            $user = Auth::user();

            // Passport Token
            $token = $user->createToken('API Token')->accessToken;

            return ApiHelper::success(
                'Login successful',
                [
                    'user' => $user,
                    'token' => $token
                ]
            );
        } catch (\Exception $e) {

            return ApiHelper::error(
                'LOGIN_01',
                'Something went wrong during login',
                $e
            );
        }
    }

    public function changePassword(Request $request)
    {
        try {

            $validator = Validator::make($request->all(), [

                'current_password' => 'required',

                'new_password' => 'required|min:6|confirmed',
            ]);

            if ($validator->fails()) {

                return ApiHelper::validationError(
                    $validator->errors()->first(),
                    $validator->errors()
                );
            }

            $user = Auth::user();

            if (!$user) {

                return ApiHelper::unauthorized(
                    'User not authenticated'
                );
            }

            /*
        |--------------------------------------------------------------------------
        | Check Current Password
        |--------------------------------------------------------------------------
        */

            if (!Hash::check(
                $request->current_password,
                $user->password
            )) {

                return ApiHelper::error(
                    'CHANGE_PASSWORD_01',
                    'Current password is incorrect',
                    null,
                    400
                );
            }

            /*
        |--------------------------------------------------------------------------
        | Update Password
        |--------------------------------------------------------------------------
        */

            $user->update([

                'password' => Hash::make(
                    $request->new_password
                )
            ]);

            return ApiHelper::success(
                'Password changed successfully'
            );
        } catch (\Exception $e) {

            return ApiHelper::error(
                'CHANGE_PASSWORD_02',
                'Something went wrong while changing password',
                $e
            );
        }
    }

    public function logout(Request $request)
    {
        try {

            $user = $request->user();

            if (!$user) {

                return ApiHelper::unauthorized(
                    'User not authenticated'
                );
            }

            // Passport Logout
            $user->token()->revoke();

            return ApiHelper::success(
                'Logged out successfully'
            );
        } catch (\Exception $e) {

            return ApiHelper::error(
                'LOGOUT_01',
                'Something went wrong during logout',
                $e
            );
        }
    }
}
