<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helpers\CurrentUser;
use App\Models\BusinessType;
use App\Models\ServiceCharge;
use App\Models\Floor;
use App\Models\Shop;
use App\Models\User;
use Validator;

class ShopController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //To fet userId..
        $userId = CurrentUser::getUserId();
        //To fetch all the shop data...
        $shopData = Shop::orderBy('id', 'desc')->where('user_id', $userId)->paginate(10);
        $allShopCount = Shop::orderBy('id', 'desc')->where('user_id', $userId)->count();

        //To fetch all the floor data...
        $floorData = Floor::orderBy('floor_name', 'asc')->where('user_id', $userId)->get();
        $businessTypeData = BusinessType::orderBy('type_name', 'asc')->where('user_id', $userId)->get();
        $serviceChargeData = ServiceCharge::orderBy('service_charge', 'asc')->where('user_id', $userId)->get();
        $shopOwnerData = User::orderBy('name', 'asc')->where('status', true)->whereNotIn('role',['superadmin','admin'])->get();
        $shopKeeperData = User::orderBy('name', 'asc')->where('status', true)->whereNotIn('role',['superadmin','admin'])->get();

        if(!empty($shopData)){
            // Check user has permission...
            if (auth()->user()->hasPermissionTo('shop-list', 'web')){
                return response()->json([
                    'message'   =>  'Successfully loaded ata.',
                    'shopData'   =>  $shopData,
                    'allShopCount'   =>  $allShopCount,
                    'floorData'   =>  $floorData,
                    'businessTypeData'   =>  $businessTypeData,
                    'serviceChargeData'   =>  $serviceChargeData,
                    'shopOwnerData'   =>  $shopOwnerData,
                    'shopKeeperData'   =>  $shopKeeperData,
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Oops! Access Denied.',
                    'status_code'   => 403
                ], 403);
            }
        }else{
            return response()->json([
				'message'   =>  'Sorry you have no data.',
                'status_code'   => 500
			], 500);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'floor_id'=> 'required',
            'business_type_id'=> 'required',
            'service_charge_id'=> 'required',
            'shop_owner_id'=> 'required',
            'shop_keeper_id'=> 'required',
            'shop_name'=> 'required',
            'shop_no'=> 'required',
            'shop_size'=> 'required',
            'shop_description'=> 'nullable',
        ]);

        if ($validator->fails()) {
        	return response(['errors'=>$validator->errors()->all()], 422);
        }

        //To fet userId..
        $userId = CurrentUser::getUserId();
        $data = $request->all();
        $data['user_id'] = $userId;

        // Check user has permission...
        if (auth()->user()->hasPermissionTo('shop-create', 'web')){
            if(Shop::create($data)){
                return response()->json([
                    'message'   =>  'Shop created successfully.',
                    'shopData'   =>  $data,
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Sorry, Something is wrong.!',
                    'status_code'   => 500
                ], 500);
            }
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $singleShopData = Shop::where('id', $id)->with(['floorData','businessTypeData','serviceChargeData'])->first();
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('shop-list', 'web')){
            if(isset($singleShopData) && $singleShopData != null){
                return response()->json([
                    'message'   =>  'Shop loaded successfully.',
                    'singleShopData'   => $singleShopData,
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Sorry, Shop not found.!',
                    'status_code'   => 500
                ], 500);
            }
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'floor_id'=> 'required',
            'business_type_id'=> 'required',
            'service_charge_id'=> 'required',
            'shop_owner_id'=> 'required',
            'shop_keeper_id'=> 'required',
            'shop_name'=> 'required',
            'shop_no'=> 'required',
            'shop_size'=> 'required',
            'shop_description'=> 'nullable',
        ]);

        //To fet userId..
        $userId = CurrentUser::getUserId();
        $data = $request->all();
        $data['user_id'] = $userId;
        $singleShopData = Shop::where('id', $id)->first();

        // Check user has permission...
        if (auth()->user()->hasPermissionTo('shop-edit', 'web')){
            if($singleShopData->update($data)){
                return response()->json([
                    'message'   =>  'Shop updated successfully.',
                    'singleShopData'   =>  $singleShopData,
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Sorry, Something is wrong.!',
                    'status_code'   => 500
                ], 500);
            }
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $singleShopData = Shop::where('id', $id)->first();
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('shop-delete', 'web')) {
            if($singleShopData->delete()){
                return response()->json([
                    'message'   =>  'Shop deleted successfully.',
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Sorry, Something is wrong.!',
                    'status_code'   => 500
                ], 500);
            }
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }

    //To active status...
    public function shopActive($id)
    {
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('shop-edit', 'web')) {
            Shop::where('id', $id)->update(['status' => true]);
            return response()->json([
                'message'   =>  'Shop activated successfully.',
                'status_code'   => 201
            ], 201);
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }
    
    //To inactive status...
    public function shopInactive($id)
    {
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('shop-edit', 'web')) {
            Shop::where('id', $id)->update(['status' => false]);
            return response()->json([
                'message'   =>  'Shop in-activated successfully.',
                'status_code'   => 201
            ], 201);
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }
}
