<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Vendor;
use Auth, Validator;

class LoginController extends Controller
{   
       
    /** login */
       /** login */
    public function vendorsignin(Request $request){
        $rules = ['email' => 'required', 'password' => 'required'];
        $validator = Validator::make($request->all(), $rules);
        
        if($validator->fails())
            return response()->json(['status' => 422, 'message' => $validator->errors()]);
        
        $auth = auth()->attempt(['email' => $request->email, 'password' => $request->password]);
        
        if(!$auth){
            return response()->json(['status' => 401, 'message' => 'Invalid login details']);
        }else{
            $vendor = Vendor::where('email', $request->email)->firstOrFail();
            
            if($vendor->status == 'active'){
               
                
                return response()->json(['status' => 200, 'message' => 'Login Successfully']);
            }else{
                return response()->json(['status' => 201, 'message' => 'This account has been inactive or deleted, please contact admin']);
            }
        }
    }
    /** login */
        
    /** logout */
        public function logout(Request $request){
            $request->vendor()->currentAccessToken()->delete();
            return response()->json(['status' => 200, 'message' => 'Logout Successfully']);
        }
    /** logout */
}
