<?php

namespace App\Http\Controllers\Api\Front;

use App\Http\Controllers\Controller;
use App\Http\Requests\Front\ForgetPasswordRequest;
use App\Http\Requests\Front\LoginRequest;
use App\Http\Requests\Front\RegisterRequest;
use App\Http\Requests\Front\ResendRequest;
use App\Http\Requests\Front\VerifyRequest;
use App\Http\Transformers\Front\UserTransformer;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{

    public function Register(RegisterRequest $request)
    {
        $userData = $request->validated();
        $userData['code'] = Hash::make(123456);
        $user = User::create($userData);
        $user->assignRole($request->type);
        $token = $user->createToken('Real-estate')->plainTextToken;
        $fractal = fractal()
            ->item($user)
            ->transformWith(new UserTransformer())
            ->toArray();
        return $this->fullDataResponse($token, $fractal, trans('messages.register'), 200);
    }

    public function verification(VerifyRequest $request)
    {
        $user = User::where('phone', $request->phone)->first();
        if (!$user) {
            return $this->errorResponse('User Not Found', null, 422);
        }
        if (!Hash::check($request->code, $user->code)) {
            return $this->errorResponse(trans('messages.wrong_code'), null, 422);
        }
        $user->phone_verification = Carbon::now();
        $user->code = null;
        $user->save();
        $token = $user->createToken('Real-estate')->plainTextToken;
        $fractal = fractal()
            ->item($user)
            ->transformWith(new UserTransformer())
            ->toArray();
        return $this->fullDataResponse($token, $fractal, trans('messages.activated_account'), 200);
    }

    public function resend(ResendRequest $request)
    {
        $user = User::where('phone', $request->phone)->first();
        if (!$user) {
            return $this->errorResponse('User Not Found', null, 422);
        }
        $user->code = Hash::make(123456);
        $user->save();
        return $this->dataResponse(null, trans('messages.send_code'), 200);
    }

    public function login(LoginRequest $request)
    {
        $userData = $request->validated();
        $user = User::where('phone', $request->phone)->first();
        if (auth()->attempt($userData)) {
            if (!is_null($user->phone_verification)) {
                $user = Auth::user();
                $token = $user->createToken('Real-estate')->plainTextToken;
                $fractal = fractal()
                    ->item($user)
                    ->transformWith(new UserTransformer())
                    ->toArray();
                return $this->fullDataResponse($token, $fractal, null, 200);
            } else {
                return $this->dataResponse(null, 'Please , Verify Your Account Before ', 403);
            }
        } else {
            return $this->errorResponse(trans('messages.password_error'), null, 401);
        }
    }

    public function forgetPassword(ForgetPasswordRequest $request)
    {
        $request->validated();
        $user = User::where('phone',$request->phone)->first();
        if (is_null($user->phone_verification)) {
            return $this->dataResponse(null, trans('messages.verify_type'), 403);
        }
        $user->password = $request->password;
        $user->save();
        return $this->dataResponse(null, trans('messages.password_changed'), 200);
    }

}
