<?php


namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\BusinessInformation;
use App\Models\Services;
use App\Models\ProductCoupon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BusinessInformationController extends Controller
{

    public function business_info_radius(Request $request)
    {
         $business = BusinessInformation::where('bname',$request->bname)->exists();
        if($business)
        {
            return redirect('/add-business-details')->with('error','Business Already Exists.');
        }
        else{
            $validator = Validator::make($request->all(),[
                'bname'=>'required',
            ]);
    
            if ($validator->fails()){
                return redirect('/add-business-details')->with('error','please fill all fields.');
            }
    
            $name = [$request->bname];
            $phone = [$request->bphone];
            $email = [$request->bemail];
            $address = [$request->baddress];
            $website = [$request->website_url];
            $working_hours = [$request->working_hours];
            $description = [$request->description];
            $timezone = [$request->timezone];
            $category = [$request->category];
            // $services = Services::all();

            // fetching data from crm
            // $dummydata = 'https://get.myaio.com/api/client-business-information';
            // $dum = json_decode(file_get_contents($dummydata), true);
            return view('addradius')->with([
            'name'=>$name,
            'phone'=>$phone,
            'email'=>$email,
            'address'=>$address,
            'website_url'=>$website,
            'description'=>$description,
            'timezone' => $timezone,
            'category' => $category,

        ]);
        
        }   
    }

    public function business_info_insert(Request $request)
    {
        $business = BusinessInformation::where('client_id', Auth()->User()->id)->exists();

        if($business)
        {
            return redirect('/home')->with('error',"you have already added your business details");
        }
        else{    

            $category_name = $request->category;
            $removedUnderscores = str_replace("_", " ", $category_name);
            $capitalizedcategory = ucwords($removedUnderscores);

            $data = [  
                'client_id' => $request->client_id,    
                'bname'=> $request->bname,     
                'bemail'=> $request->bemail,
                'bphone' => $request->bphone,
                'website_url'=> $request->website_url,
                'baddress' => $request->baddress,
                'description'=> $request->description,
                'radius_address'=> $request->radius_address,
                'radius' => $request->radius,
                'radius_image' => $request->radius_image,
                'closerid' => $request->closure_id,
                // 'closer_name' => $request->self_closure,
                'services' => $request->services,
                'timezone' => $request->timezone,
                'category' => $capitalizedcategory,
            ];
            BusinessInformation::create($data);
            return redirect('select-package')->with('message',"your business information is addedd successfully");
        }
        
        
    }

    public function updatebusiness()
    {
        $data = BusinessInformation::where('client_id', Auth()->User()->id)->get();
        return view('editbusiness')->with('data',$data);
    }

    public function business_info_update(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'bname'=>'required',
        ]);

        if ($validator->fails()){
            return redirect('/edit-business-details')->with('error','please fill all fields.');
        }
        $name = [$request->bname];
        $phone = [$request->bphone];
        $email = [$request->bemail];
        $address = [$request->baddress];
        $website = [$request->website_url];
        $working_hours = [$request->working_hours];
        $description = [$request->description];
        $radiusaddress = [$request->radiusaddress];
        $radius = [$request->radius];
        $radius_image = $request->radius_image;
        $timezone = [$request->timezone];
        $category = [$request->category];
        $clsrid = [$request->closerid];

        return view('editradius')->with([
        'name'=>$name,
        'phone'=>$phone,
        'email'=>$email,
        'address'=>$address,
        'website_url'=>$website,
        'description'=>$description,
        'radiusaddress' => $radiusaddress,
        'radius' => $radius,
        'radius_image' => $radius_image,
        'timezone' => $timezone,
        'category' => $category,
        'closerid' => $clsrid,
    ]);
    }

    public function business_update(Request $request)
    {
        $category_name = $request->category;
        $removedUnderscores = str_replace("_", " ", $category_name);
        $capitalizedcategory = ucwords($removedUnderscores);

        $self = $request->closure_id;
        $emp_closer = $request->emp_closure_id;

        $business = BusinessInformation::where('client_id', $request->client_id)->first();

        if ($business) {
            // Check if $self is not null, update closerid with $self
            if (!is_null($self)) {
                $business->closerid = $self;
            }
            // If $self is null, check if $emp_closer is not null, update closerid with $emp_closer
            elseif (!is_null($emp_closer)) {
                $business->closerid = $emp_closer;
            }

            // Update other fields
            $business->update([
                'bname' => $request->bname,
                'bemail' => $request->bemail,
                'bphone' => $request->bphone,
                'website_url' => $request->website_url,
                'baddress' => $request->baddress,
                'description' => $request->description,
                'radius_address' => $request->radius_address,
                'radius' => $request->radius,
                'radius_image' => $request->radius_image,
                'timezone' => $request->timezone,
                'category' => $capitalizedcategory,
            ]);

            return redirect('select-package')->with('message',"your business information is addedd successfully");
        } else {
            // Handle the case where no business information is found
            return 'error';
        }

        return redirect('select-package')->with('message',"your business information is addedd successfully");
    }
}
