<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helpers\CurrentUser;
use App\Models\BusinessType;
use Validator;

class BusinessTypeController 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 business type data...
        $businessTypeData = BusinessType::orderBy('id', 'desc')->paginate(10);
        $allBusinessTypeCount = BusinessType::orderBy('id', 'desc')->count();

        if(!empty($businessTypeData)){
            // Check user has permission...
            if (auth()->user()->hasPermissionTo('business-type-list', 'web')){
                return response()->json([
                    'message'   =>  'Successfully loaded ata.',
                    'businessTypeData'   =>  $businessTypeData,
                    'allBusinessTypeCount'   =>  $allBusinessTypeCount,
                    '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(), [
            'type_name'=> 'required',
        ]);

        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('business-type-create', 'web')){
            if(BusinessType::create($data)){
                return response()->json([
                    'message'   =>  'BusinessType created successfully.',
                    'businessTypeData'   =>  $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)
    {
        $singleBusinessTypeData = BusinessType::where('id', $id)->first();
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('business-type-list', 'web')){
            if(isset($singleBusinessTypeData) && $singleBusinessTypeData != null){
                return response()->json([
                    'message'   =>  'Business type loaded successfully.',
                    'singleBusinessTypeData'   => $singleBusinessTypeData,
                    'status_code'   => 201
                ], 201);
            }else{
                return response()->json([
                    'message'   =>  'Sorry, Business type 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(), [
            'type_name'=> 'required',
        ]);

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

        // Check user has permission...
        if (auth()->user()->hasPermissionTo('business-type-edit', 'web')){
            if($singleBusinessTypeData->update($data)){
                return response()->json([
                    'message'   =>  'BusinessType updated successfully.',
                    'singleBusinessTypeData'   =>  $singleBusinessTypeData,
                    '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)
    {
        $singleBusinessTypeData = BusinessType::where('id', $id)->first();
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('business-type-delete', 'web')) {
            if($singleBusinessTypeData->delete()){
                return response()->json([
                    'message'   =>  'BusinessType 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 businessTypeActive($id)
    {
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('business-type-edit', 'web')) {
            BusinessType::where('id', $id)->update(['status' => true]);
            return response()->json([
                'message'   =>  'Business type activated successfully.',
                'status_code'   => 201
            ], 201);
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }
    
    //To inactive status...
    public function businessTypeInactive($id)
    {
        // Check user has permission...
        if (auth()->user()->hasPermissionTo('business-type-edit', 'web')) {
            BusinessType::where('id', $id)->update(['status' => false]);
            return response()->json([
                'message'   =>  'Business type in-activated successfully.',
                'status_code'   => 201
            ], 201);
        }else{
            return response()->json([
                'message'   =>  'Oops! Access Denied.',
                'status_code'   => 403
            ], 403);
        }
    }
}
