HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: /var/www/javago-portal-updates/app/Services/CafeManagementService.php
<?php

namespace App\Services;

use App\Helpers\Aws;
use App\Mail\SendCredentialsMail;
use App\Models\Addon;
use App\Models\AddonSize;
use App\Models\Admin;
use App\Models\CafeFilter;
use App\Models\CafeManageLoyaltyStamp;
use App\Models\CafeMenu;
use App\Models\CafeMenuItem;
use App\Models\CafeRequest;
use App\Models\CafeTiming;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Yajra\DataTables\Facades\DataTables;
use Illuminate\Support\Str;

class CafeManagementService
{
    private $model;

    public function __construct($model)
    {
        $this->model = new $model();
    }

    public function store($request)
    {
        DB::beginTransaction();
        try {
            $create = [];
            $userExist = CafeRequest::where('email', $request->email)->first();
            if ($userExist) {
                DB::rollback();
                session()->flash('error_message', __('validation.validation.email.unique'));
                return redirect()->back();
            }
            // $password = Str::random(6) . '@' . mt_rand(10,99);
            // $password = Str::random(3, 'a') . Str::random(3, 'A') . '@' .mt_rand(10,99);
            // Define arrays of valid characters for each password component
            $lowercase = range('a', 'z');
            $uppercase = range('A', 'Z');
            $special = array('@');
            $numbers = range(0, 9);

            // Combine the arrays and shuffle to get a random order
            $all_chars = array_merge($lowercase, $uppercase, $special, $numbers);
            shuffle($all_chars);

            // Choose a random character from each array and concatenate into a password string
            $password = '';
            $password .= $lowercase[array_rand($lowercase)];
            $password .= $uppercase[array_rand($uppercase)];
            $password .= $special[array_rand($special)];
            $password .= $all_chars[array_rand($all_chars)];
            $password .= $all_chars[array_rand($all_chars)];
            $password .= $all_chars[array_rand($all_chars)];
            $password .= $all_chars[array_rand($all_chars)];
            $password .= $numbers[array_rand($numbers)];

            // Print the password
            // echo $password;
            $create['password'] = bcrypt($password);
            $create['cafe_name'] = $request->cafe_name;
            $create['email'] = $request->email;
            $create['phone'] = $request->phone;
            $create['cafe_type'] = $request->cafe_type;
            $create['bio'] = $request->bio;
            $create['address'] = $request->address;
            $create['postcode'] = $request->postcode;
            $create['website'] = $request->website;
            $create['latitude'] = isset($request->latitude) ? $request->latitude : 0;
            $create['longitude'] = isset($request->longitude) ? $request->longitude : 0;
            $create['approved'] = 1;
            $create['cafe_filter'] = implode(', ', $request->cafe_filter);
            $create['signup_completed'] = 1;
            $create['profile_completed'] = 1;
            $create['is_active'] = 1;
            $create['created_at'] = Carbon::now()->timestamp;
            $create['updated_at'] = Carbon::now()->timestamp;
            if (isset($request->picture__input) && is_file($request->picture__input)) {
                $create['banner_image'] = Aws::uploadImageS3Bucket('images/cafe', $request->picture__input, '');
            } else {
                DB::rollback();
                session()->flash('error_message', __('validation.validation.image.required'));
                return redirect()->back();
            }
            $cafeId = $this->model->insertGetId($create);
            $cafe = $this->model->find($cafeId);
            $cafe->assignRole('cafe_admin');
            $cafeTime = [
                'cafe_id' => $cafeId,
                'open_time' => config('constants.cafe_timing.open_time'),
                'close_time' => config('constants.cafe_timing.close_time'),
                'is_active' => config('constants.cafe_timing.is_active'),
                'created_at' => Carbon::now()->timestamp,
                'updated_at' => Carbon::now()->timestamp,
            ];
            $cafeTiming = [];
            for ($i = 0; $i < 7; $i++) {
                $cafeTime['day'] = $i;
                array_push($cafeTiming, $cafeTime);
            }
            CafeTiming::insert($cafeTiming);
            // add cafe menu
            $cafeMenus = [
                // [
                //     'menu_name' => 'Drinks',
                //     'cafe_id' => $cafeId,
                //     'created_at' => Carbon::now()->timestamp,
                //     'updated_at' => Carbon::now()->timestamp,
                // ],
                // [
                //     'menu_name' => 'Pastries',
                //     'cafe_id' => $cafeId,
                //     'created_at' => Carbon::now()->timestamp,
                //     'updated_at' => Carbon::now()->timestamp,
                // ],
                [
                    'menu_name' => 'Hot Drinks',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Tea',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Bakery',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Sandwiches',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Iced drinks',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Smoothies',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Bagles',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Salads',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Wraps',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Snacks',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Bottled Drinks',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Breakfast Options',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Bubble Tea',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Cakes',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Coffee',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Hot Chocolate',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Soft Drinks',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Brunch',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Breakfast',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Lunch',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Main Meals',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Desserts',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Soup',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Milkshakes',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Kids Menu',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Seasonal',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Vegan Options',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Matcha',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Juice',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Coffee Beans and Other Retail',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Other',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Unique',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'menu_name' => 'Bundles',
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
            ];
            CafeMenu::insert($cafeMenus);

            // add addons
            $addons = [
                [
                    'addon_name' => "Milk",
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'addon_name' => "Sugar",
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'addon_name' => "Syrup",
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'addon_name' => "Other",
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'addon_name' => "Added Taste",
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
                [
                    'addon_name' => "Add on",
                    'cafe_id' => $cafeId,
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ],
            ];
            Addon::insert($addons);

            // add addon sizes
            $addonSizes = [];
                    $addon = Addon::where('cafe_id', $cafeId)->get();
                    foreach ($addon as $key => $value) {
                        switch ($value->addon_name) {
                            case 'Milk':
                                $addonSize = [
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'No milk',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Whole milk',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Semi skimmed milk',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Oat milk',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Soya milk',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Almond milk',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Skimmed milk',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Coconut milk',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                ];                                
                                array_push($addonSizes, $addonSize);
                                break;

                            case 'Sugar':
                                $addonSize = [
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'White sugar',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Brown sugar',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Sweetener',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                ];
                                array_push($addonSizes, $addonSize);
                                break;

                            case 'Syrup':
                                $addonSize = [
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Vanilla',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Hazelnut',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Caramel',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Salted caramel',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Mint',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Almond',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Mocha/Chocolate',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Coconut',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Cinnamon',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Toffee Nut',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Macadamia Nut',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Raspberry',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Strawberry',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Blueberry',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Cherry',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Peach',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Mango',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Passionfruit',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Pomegranate',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Orange',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Pumpkin Spice',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Peppermint',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Gingerbread',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Maple',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Irish Cream',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Amaretto',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Butter Pecan',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Sugar-Free Vanilla',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Sugar-Free Caramel',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Sugar-Free Hazelnut',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Pistachio',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Walnut',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Peanut Butter',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                    [
                                        'addon_id' => $value->id,
                                        'cafe_id' => $cafeId,
                                        'addon_size_name' => 'Matcha',
                                        'addon_size_price' => 0,
                                        'created_at' => Carbon::now()->timestamp,
                                        'updated_at' => Carbon::now()->timestamp,
                                    ],
                                ];                                
                                array_push($addonSizes, $addonSize);
                                break;

                                case 'Other':
                                    $addonSize = [
                                        [
                                            'addon_id' => $value->id,
                                            'cafe_id' => $cafeId,
                                            'addon_size_name' => 'Decaf',
                                            'addon_size_price' => 0,
                                            'created_at' => Carbon::now()->timestamp,
                                            'updated_at' => Carbon::now()->timestamp,
                                        ],
                                        [
                                            'addon_id' => $value->id,
                                            'cafe_id' => $cafeId,
                                            'addon_size_name' => 'Extra shot',
                                            'addon_size_price' => 0,
                                            'created_at' => Carbon::now()->timestamp,
                                            'updated_at' => Carbon::now()->timestamp,
                                        ],
                                        [
                                            'addon_id' => $value->id,
                                            'cafe_id' => $cafeId,
                                            'addon_size_name' => 'Espresso',
                                            'addon_size_price' => 0,
                                            'created_at' => Carbon::now()->timestamp,
                                            'updated_at' => Carbon::now()->timestamp,
                                        ],
                                    ];
                                    array_push($addonSizes, $addonSize);
                                    break;
                                    case 'Added Taste':
                                        $addonSize = [
                                            [
                                                'addon_id' => $value->id,
                                                'cafe_id' => $cafeId,
                                                'addon_size_name' => 'Ginger',
                                                'addon_size_price' => 0,
                                                'created_at' => Carbon::now()->timestamp,
                                                'updated_at' => Carbon::now()->timestamp,
                                            ],
                                            [
                                                'addon_id' => $value->id,
                                                'cafe_id' => $cafeId,
                                                'addon_size_name' => 'Mint',
                                                'addon_size_price' => 0,
                                                'created_at' => Carbon::now()->timestamp,
                                                'updated_at' => Carbon::now()->timestamp,
                                            ],
                                            [
                                                'addon_id' => $value->id,
                                                'cafe_id' => $cafeId,
                                                'addon_size_name' => 'Honey',
                                                'addon_size_price' => 0,
                                                'created_at' => Carbon::now()->timestamp,
                                                'updated_at' => Carbon::now()->timestamp,
                                            ],
                                        ];
                                        array_push($addonSizes, $addonSize);
                                        break;
                                        case 'Add on':
                                            $addonSize = [
                                                [
                                                    'addon_id' => $value->id,
                                                    'cafe_id' => $cafeId,
                                                    'addon_size_name' => 'Marshmallow',
                                                    'addon_size_price' => 0,
                                                    'created_at' => Carbon::now()->timestamp,
                                                    'updated_at' => Carbon::now()->timestamp,
                                                ],
                                                [
                                                    'addon_id' => $value->id,
                                                    'cafe_id' => $cafeId,
                                                    'addon_size_name' => 'Whipped cream',
                                                    'addon_size_price' => 0,
                                                    'created_at' => Carbon::now()->timestamp,
                                                    'updated_at' => Carbon::now()->timestamp,
                                                ],
                                            ];
                                            array_push($addonSizes, $addonSize);
                                            break;

                            default:
                                break;
                        }
                    }
                    for ($i = 0; $i < sizeof($addonSizes); $i++) {
                        AddonSize::insert($addonSizes[$i]);
                    }

            $reply_to = $create['email'];
            $data = [
                'subject' => __('common.mailSubjectCafeCredentials'),
                'username' => $create['cafe_name'],
                'email' => $create['email'],
                'password' => $password,
                'link' => route('cafe.login'),
                'view' => 'emails.cafe-credentials',
            ];
            $admin = Admin::first();
            $bcc = $admin->email;
            Mail::to($reply_to)->bcc($bcc)->send(new SendCredentialsMail($data));
            DB::commit();
            session()->flash('success_message', __('common.profileUpdatedSuccessfully'));
            return redirect()->route('admin.cafe-management.index');
        } catch (\Throwable $th) {
            DB::rollback();
            session()->flash('error_message', __('common.somethingWentWrong'));
            return redirect()->back();
        }
    }

    public function show($id)
    {
        abort_if(!$id, 404);
        $cafe = $this->model->find(decrypt($id));
        abort_if(!$cafe, 404);
        $data = [];
        $data['name'] = $cafe->cafe_name;
        $data['email'] = $cafe->email;
        $data['banner_image'] = $cafe->banner_image ? Aws::presignedUri($cafe->banner_image) : '';
        $data['bio'] = $cafe->bio;
        $data['phone'] = $cafe->phone;
        $data['cafe_type'] = $cafe->cafe_type;
        $data['address'] = $cafe->address;
        $data['postcode'] = $cafe->postcode;
        $cafeFilter = CafeFilter::whereIn('id', explode(', ' ,$cafe->cafe_filter))->pluck('name');
        $data['cafe_filter'] = $cafeFilter;
        $data['website'] = asset($cafe->website) ? $cafe->website : '-';
        $data['cafeManagementUrl'] = route('admin.cafe-management.index');
        $data['timing'] = $cafe->timing;
        $data['days'] = [];
        foreach (__('common.days') as $value) {
            array_push($data['days'], $value);
        }
        // Fetch and process menu items
        $menuItems = CafeMenuItem::join('pre_defined_item_images', 'pre_defined_item_images.id', '=', 'cafe_menu_items.item_image_id')
        ->join('cafe_menus', 'cafe_menus.id', '=', 'cafe_menu_items.cafe_menu_id')
        ->where('cafe_menu_items.cafe_id', $cafe->id)
        ->where('cafe_menu_items.item_deleted_at', 0)
        ->select(
            'cafe_menu_items.id', 
            'cafe_menu_items.item_name', 
            'cafe_menu_items.item_type', 
            'pre_defined_item_images.item_image as item_image', 
            'cafe_menus.menu_name as item_category', 
            'cafe_menu_items.item_description', 
            'cafe_menu_items.status', 
            'cafe_menu_items.item_price'
        )
        ->get();

        // Map item types to their values using the configuration
        $itemTypes = config('constants.item_type');
         // Map item types to their names
        $menuItems->transform(function($item) use ($itemTypes) {
            // Update item_type to its name
            $item->item_type = $itemTypes[$item->item_type] ?? 'Unknown'; // Provide a default value if type not found
            return $item;
        });

        // Add processed menu items to data array
        $data['menu_items'] = $menuItems;


        $data['stamps'] = CafeManageLoyaltyStamp::where('cafe_id', $cafe->id)->get();

        return view('cafe.review-profile', $data);
    }

    public function edit($id)
    {
        abort_if(!$id, 404);
        $cafe = $this->model->find(decrypt($id));
        abort_if(!$cafe, 404);
        $data = [];
        $data['name'] = $cafe->cafe_name;
        $data['email'] = $cafe->email;
        $data['banner_image'] = $cafe->banner_image ? Aws::presignedUri($cafe->banner_image) : '';
        $data['bio'] = $cafe->bio;
        $data['phone'] = $cafe->phone;
        $data['cafe_type'] = $cafe->cafe_type;
        $data['address'] = $cafe->address;
        $data['postcode'] = $cafe->postcode;
        $data['website'] = $cafe->website;
        $data['latitude'] = $cafe->latitude;
        $data['longitude'] = $cafe->longitude;
        $data['timing'] = $cafe->timing;
        $data['url'] = route('admin.cafe-management.update', encrypt($cafe->id));
        $data['cancelUrl'] = route('admin.cafe-management.index');
        $data['setHoursUrl'] = route('admin.cafe-management.updateCafeHours', encrypt($cafe->id));
        $data['CafeFilter'] = CafeFilter::where([['name', '!=', 'All'], ['name', '!=', 'Favorites']])->pluck('name', 'id');
        $data['cafe_filter'] = explode(', ', $cafe->cafe_filter);
        $data['urlGoogleAPI'] = config('geocode.API_URI');

        $data['types'] = [];
        foreach (__('common.cafe_types') as $value) {
            array_push($data['types'], $value);
        }
        $data['days'] = [];
        foreach (__('common.days') as $value) {
            array_push($data['days'], $value);
        }
        return view('cafe.edit-profile', $data);
    }

    public function update($request, $id)
    {
        abort_if(!$id, 404);
        DB::beginTransaction();
        try {
            $cafe = $this->model->find(decrypt($id));
            if (!$cafe) {
                DB::rollback();
                // session()->flash('error_message', __('common.somethingWentWrong'));
                // return redirect()->back();
                abort(404);
            }
            $update = [];
            $userExist = CafeRequest::where('email', $request->email)->first();
            if ($userExist) {
                DB::rollback();
                session()->flash('error_message', __('validation.validation.email.unique'));
                return redirect()->back();
            }
            $update['cafe_name'] = $request->cafe_name;
            $update['email'] = $request->email;
            $update['phone'] = $request->phone;
            $update['cafe_type'] = $request->cafe_type;
            $update['bio'] = $request->bio;
            $update['address'] = $request->address;
            $update['postcode'] = $request->postcode;
            $update['website'] = isset($request->website) ? $request->website : '';
            $update['latitude'] = isset($request->latitude) ? $request->latitude : '';
            $update['longitude'] = isset($request->longitude) ? $request->longitude : '';
            $update['profile_completed'] = 1;
            $update['cafe_filter'] = implode(', ', $request->cafe_filter);
            $update['updated_at'] = Carbon::now()->timestamp;
            if (isset($request->picture__input) && is_file($request->picture__input)) {
                $update['banner_image'] = Aws::uploadImageS3Bucket('images/cafe', $request->picture__input, $cafe->banner_image);
            } else {
                if ($cafe->banner_image == '') {
                    DB::rollback();
                    session()->flash('error_message', __('validation.validation.image.required'));
                    return redirect()->back();
                }
            }
            $cafe->update($update);
            DB::commit();
            session()->flash('success_message', __('common.profileUpdatedSuccessfully'));
            return redirect()->route('admin.cafe-management.index');
        } catch (\Throwable $th) {
            DB::rollback();
            session()->flash('error_message', __('common.somethingWentWrong'));
            return redirect()->back();
        }
    }

    public function updateCafeHours($request, $id)
    {
        DB::beginTransaction();
        try {
            $cafeExist = $this->model->find(decrypt($id));
            if (!$cafeExist) {
                DB::rollback();
                return response()->json([
                    'status' => 500,
                    'message' => __('common.somethingWentWrong'),
                ]);
            }
            $cafe = $request->cafe;
            $cafeId = $cafeExist->id;
            CafeTiming::chunk(7, function ($cafeTiming) use ($cafe, $cafeId) {
                foreach ($cafe as $key => $value) {
                    $cafe_time = [
                        'open_time' => $value['open_time'],
                        'close_time' => $value['close_time'],
                        'is_active' => isset($value['is_active']) ? $value['is_active'] : 0,
                        'updated_at' => Carbon::now()->timestamp,
                    ];
                    CafeTiming::where('cafe_id', $cafeId)->where('day', $key)->update($cafe_time);
                }
            });
            DB::commit();
            session()->flash('success_message', __('common.setCafeHoursSuccessfully'));
            return response()->json([
                'status' => 200,
                'message' => __('common.setCafeHoursSuccessfully'),
                'goto' => route('admin.cafe-management.edit', $id),
            ]);
            // return redirect()->route('admin.cafe-management.edit', $id);
        } catch (\Throwable $th) {
            DB::rollback();
            return response()->json([
                'status' => 500,
                'message' => __('common.somethingWentWrong'),
            ]);
            // session()->flash('error_message', __('common.somethingWentWrong'));
            // return redirect()->back();
        }
    }

    public function status($request, $id, $userType)
    {
        DB::beginTransaction();
        try {
            $cafe = $this->model->find(decrypt($id));
            if (!$cafe) {
                DB::rollback();
                return response()->json([
                    'status' => 500,
                    'message' => __('common.somethingWentWrong'),
                ]);
            }
            $status = [];
            $status['is_active'] = $request->status == 1 ? 0 : 1;
            $status['updated_at'] = Carbon::now()->timestamp;
            $cafe->update($status);
            DB::commit();
            return response()->json([
                'status' => 200,
                'message' => __('common.statusChangedSuccessfully', ['userType' => $userType]),
            ]);
        } catch (\Throwable $th) {
            DB::rollback();
            return response()->json([
                'status' => 500,
                'message' => __('common.somethingWentWrong'),
            ]);
        }
    }

    public function change_admin_approval($id, $userType)
    {
        DB::beginTransaction();
        try {
            // Decrypt the ID and find the record
            $cafe = $this->model->find(decrypt($id));
    
            if (!$cafe) {
                DB::rollback();
                return response()->json([
                    'status' => 404,
                    'message' => __('common.recordNotFound'),
                ]);
            }
    
            // Toggle the approval status
            $status = [
                'is_admin_approved' => $cafe->is_admin_approved == 1 ? 0 : 1,
                'updated_at' => Carbon::now(), // Use Carbon directly
            ];
    
            // Update the record
            $cafe->update($status);
    
            // Commit the transaction
            DB::commit();
    
            // Return success response
            return response()->json([
                'status' => 200,
                'message' => __('Cafe Admin Approval status Changed Successfully!', ['userType' => $userType]),
            ]);
    
        } catch (\Throwable $th) {
    
            // Rollback the transaction on error
            DB::rollback();
    
            // Return error response
            return response()->json([
                'status' => 500,
                'message' => __('common.somethingWentWrong'),
                'error' => $th->getMessage(),
            ]);
        }
    }

    public function destroy($id, $modelName)
    {
        DB::beginTransaction();
        try {
            $cafe = $this->model->find(decrypt($id));
            if (!$cafe) {
                DB::rollback();
                return response()->json([
                    'status' => 500,
                    'message' => __('common.somethingWentWrong'),
                ]);
            }
            $delete = [];
            $delete['deleted_at'] = Carbon::now()->timestamp;
            $cafe->update($delete);
            DB::commit();
            return response()->json([
                'status' => 200,
                'message' => __('common.DeletedSuccessfully', ['model' => $modelName]),
            ]);
        } catch (\Throwable $th) {
            DB::rollback();
            return response()->json([
                'status' => 500,
                'message' => __('common.somethingWentWrong'),
            ]);
        }
    }

    public function data()
    {
        //$data = $this->model->query()->where([['signup_completed', '=', 1], ['deleted_at', '=', 0]])->select('id', 'cafe_name', 'phone', 'email', 'address', 'banner_image', 'is_active', 'is_admin_approved');
        $adminApproved = request()->get('admin_approved');

        // Build the query with additional filtering
        $query = $this->model->query()
            ->where([['signup_completed', '=', 1], ['deleted_at', '=', 0], ['profile_completed', '=', 1], ['menu_completed', '=', 1], ['loyalty_completed', '=', 1]])
            ->select('id', 'cafe_name', 'phone', 'email', 'address', 'banner_image', 'is_active', 'is_admin_approved');
    
        // Apply the admin_approved filter if it's set
        if ($adminApproved !== null && $adminApproved !== '') {
            $query->where('is_admin_approved', $adminApproved);
        }
        return DataTables::eloquent($query)
            ->addIndexColumn()
            ->editColumn('cafe_name', function ($row) {
                if (empty($row->cafe_name)) {
                    return '-';
                } else {
                    if (strlen($row->cafe_name) > 30) {
                        return substr($row->cafe_name, 0, 50) . '...';
                    } else {
                        return $row->cafe_name;
                    }
                }
            })
            ->editColumn('phone', function ($row) {
                if (empty($row->phone)) {
                    return '-';
                } else {
                    return $row->phone;
                }
            })
            ->editColumn('email', function ($row) {
                if (empty($row->email)) {
                    return '-';
                } else {
                    if (strlen($row->email) > 50) {
                        return substr($row->email, 0, 50) . '...';
                    } else {
                        return $row->email;
                    }
                }
            })
            ->editColumn('address', function ($row) {
                if (empty($row->address)) {
                    return '-';
                } else {
                    if (strlen($row->address) > 50) {
                        return substr($row->address, 0, 50) . '...';
                    } else {
                        return $row->address;
                    }
                }
            })
            ->editColumn('banner_image', function ($row) {
                // if (empty($row->banner_image)) {
                //     return '-';
                // } else {
                return view(
                    "partials.common",
                    [
                        'image' => $row->banner_image,
                        'isProfile' => 1,
                    ]
                )->render();
                // }
            })
            ->addColumn('status', function ($row) {
                return view(
                    "partials.action",
                    [
                        'currentRoute' => 'admin.cafe-management',
                        'row' => $row,
                        'isAvailable' => 1,
                    ]
                )->render();
            })
	    ->addColumn('is_admin_approved', function ($row) {
                return view(
                    "partials.action",
                    [
                        'currentRoute' => 'admin.cafe-management',
                        'row' => $row,
                        'is_admin_approved' => 1,
                    ]
                )->render();
            })
            ->addColumn('action', function ($row) {
                return view(
                    "partials.action",
                    [
                        'currentRoute' => 'admin.cafe-management',
                        'row' => $row,
                        'isEdit' => 1,
                        'isDelete' => 1,
                        'isView' => 1,
                    ]
                )->render();
            })
            ->rawColumns(['banner_image', 'action', 'status', 'is_admin_approved'])
            ->toJson();
    }
}