<?php

namespace App\Traits;

use App\Models\Order;
use App\Models\BusinessInformation;
use App\Models\Product;
use App\Models\ProductCoupon;
use App\Models\Coupon;
use App\Models\Closer;
use App\Models\User;

trait BusinessInformationTrait{

    public function get_business_info()
    {
        $businessinfo = BusinessInformation::where('client_id', Auth()->user()->id)
        ->get();

    $business_info = null;

    foreach ($businessinfo as $business) {
        $business_info = BusinessInformation::select()
            ->join('product_coupons', 'product_coupons.business_id', '=', 'business_information.id')
            ->join('products', 'products.id', '=', 'product_coupons.product_id')
            ->join('coupons', 'coupons.id', '=', 'product_coupons.coupon_id')
            ->join('users', 'users.id', '=', 'coupons.closer_id')
            ->join('closers', 'closers.user_id', '=', 'users.id')
            ->where('business_information.id', '=', $business->id)
            ->select(
                'product_coupons.new_discount_price AS newprice',
                'products.productname',
                'products.productdescription'
            )
            ->get();

        if ($business_info->isNotEmpty()) {
            break;
        }
    }

    if ($business_info->isNotEmpty()) {
        return $business_info;
    } else {
        $business_info2 = BusinessInformation::where('client_id', Auth()->user()->id)
            ->join('users', 'users.id', '=', 'business_information.client_id')
            ->join('products', 'products.id', '=', 'business_information.pid')
            ->join('closers', 'closers.id', '=', 'business_information.closerid')
            ->select(
                'products.price AS newprice',
                'products.productname',
                'products.productdescription'
            )
            ->get();

        return $business_info2;
    }
    }
}