<?php

namespace App\Http\Controllers\API\V1\Auth;

use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Models\User;
use App\Traits\HttpResponses;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class APIAuthController extends Controller
{
    Use HttpResponses;

    public function login(Request $request)
    {
        $validator = $request->validate([
            'email'     => 'required',
            'password'  => 'required',
        ]);

        if (!Auth::attempt($request->only(['email', 'password']))) {
            return $this->error(
                data:   null,
                message: 'Email & Password does not match with our record.',
                code: 401,
            );
        }

        $user = User::find(Auth::id());

        if(!$user){ return $this->abort(); }

        if ($user->status == 0) {
            return $this->error(null, message: 'Your account is disabled', code: 403);
        }

        $user['token'] = $user->createToken('auth_token' . $user->id)->plainTextToken;

        return $this->success(
            data: new UserResource($user),
            message: 'You have logged in successfully',
        );
    }

    public function register(Request $request)
    {
        $validator = $request->validate([
            'name'      => 'required',
            'uid'       => 'nullable',
            'email'     => 'required|email|unique:users,email',
            'phone'     => 'required|unique:users,phone',
            'password'  => 'required|min:6',
            'bio'       => 'nullable',
        ]);

        $user = User::create([
            'name'  => $request->name,
            'uid'   => $request->uid,
            'password' => Hash::make($request->password),
            'email' => $request->email,
            'phone' => $request->phone,
            'bio' => $request->bio,
        ]);

        try{
            $user = User::find($user->id);
        }catch(\Exception $e){
            return $this->abort();
        }

        // $email = $request->email;
        // Mail::to($email)->send(new WelcomeMail($user));
        $user['token'] = $user->createToken('auth_token' . $user->id)->plainTextToken;

        return $this->success(
            data: new UserResource($user),
            message: 'You have registered successfully',
        );
    }

   /*  public function register(Request $request)
    {
        $request->validate([
            'name'      => 'required|string',
            'email'     => 'required|string|email|unique:users',
            'uid'       => 'required|string|unique:users',
            'image_url' => 'nullable|string',
            'password'  => 'required|min:6',
        ]);

        // $attrs['password'] = bcrypt($attrs['password']);
        $user = User::create([
            'uid'   => $request->uid,
            'email' => $request->email,
            'password' => Hash::make($request->password),
            'name'  => $request->name,
            'bio'  => $request->bio,
            'phone' => $request->phone,
            'image_url' => $request->image_url,
            'province_id' => $request->province_id,
            'latitude' => $request->province_id,
            'longitude' => $request->province_id,
            'cm_firebase_token' => $request->province_id,
            'type'  => $request->type,
            'following_count' => 0,
            'follower_count' => 0,
        ]);

        return $this->success([
            'user' => $user,
            'token' => $user->createToken('Newgenventure API Token' . $user->id)->plainTextToken
        ]);
    } */


}



  /*  public function hasUid($uid)
    {
        $count = User::where('uid', $uid)->count();
        return response()->json($count != 0 ? true : false, 200);
    }

    public function hasUser($id)
    {
        $count = User::where('id', $id)->count();
        return response()->json($count != 0 ? true : false, 200);
    } */
