<?php


namespace App\Http\Controllers\AdminAPI\Admin;

#Model
use App\User;
use App\Models\City;
use App\Models\State;
use App\Models\Payment;
use App\Models\UserBanks;
use App\Models\ReferalCode;
use App\Models\ReferalCodeHistory;

#Other Class
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\API\BaseController as BaseController;


class UserController extends BaseController
{

    #Bind City model
    protected $city;

    #Bind UserBanks model
    protected $userBank;

    #Bind User model
    protected $user;

    #Bind State model
    protected $state;

    #Bind Payment model
    protected $payment;

    #Bind ReferalCode model
    protected $referalCode;

    #Bind ReferalCodeHistory model
    protected $referalCodeHistory;

    /**
     * Defining the default constructor for controller
     *
     */
    public function __construct(
        City $city,
        State $state,
        User $user,
        UserBanks $userBank,
        Payment  $payment,
        ReferalCode  $referalCode,
        ReferalCodeHistory  $referalCodeHistory
    ) {
        $this->city                = $city;
        $this->state               = $state;
        $this->user                = $user;
        $this->userBank            = $userBank;
        $this->payment             = $payment;
        $this->referalCode         = $referalCode;
        $this->referalCodeHistory  = $referalCodeHistory;
    }

   
    /**
     * login api
     *
     * @return \Illuminate\Http\Response
     */
    public function login(Request $request)
    {

        if (Auth::attempt(['mobile_number' => $request->mobile_number, 'password' => $request->password])) {
            $user = Auth::user();
        }else{
            return $this->sendError('Invalid Credentials.', 'Invalid Credentials.');
        }
        


        if ($user) {

            if ($user->type == 'admin') {
                Auth::login($user);
                $user = Auth::user();
                $data = ['device_key' => request('device_key'),];
                $this->user->whereId($user->id)->update($data);
                $success['token'] =  $user->createToken('MyApp')->accessToken;
                $success['name'] =  $user->name;
                $success['amount'] =  totalAmount($user->id);
                $success['id'] =  $user->id;
                return $this->sendResponse($success, 'Admin Login successfully.');
            } else {
                return $this->sendError('Unauthorised.', '401');
            }
        } else {
            return $this->sendError('Unauthorised.', '401');
        }
    }
    /**
     * logout api
     *
     * @return \Illuminate\Http\Response
     */
    public function logout()
    {
        if (Auth::check()) {
            Auth::user()->token()->revoke();
            $success['data'] = '';
            return $this->sendResponse($success, 'logout_success.');
        } else {
            return $this->sendError('Unauthorised.', '401');
        }
    }

    /**
     * details api
     *
     * @return \Illuminate\Http\Response
     */
    
}
