<?php

namespace App\Repositories;

use App\Models\BusinessProductsOrdersDetails;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Mail\Mailer;

class BusinessProductsOrdersDetailsRepository
{
    public function __construct(
        BusinessProductsCategoryValuesRepository $businessProductsCategoryValuesRepository,
        BusinessProductsOrdersDetails $businessProductsOrdersDetails,
        Filesystem $filesystem,
        Mailer $mailer
    ) {
        $this->businessProductsCategoryValuesRepository = $businessProductsCategoryValuesRepository;
        $this->model = $businessProductsOrdersDetails;
        $this->file = $filesystem;
        $this->mailer = $mailer;
    }

    public function getById($id)
    {
        return $this->model->where('id', $id)->first();
    }

    public function addProduct($order_id,
        $productId,
        $pageId,
        $userid,
        $cartQuantity,
        $subTotal,
        $productPrice,
        $productDiscount,
        $shipping_cost,
        $community_id,
        $business_id,
        $order_date_time,
        $productsizeid,
        $product_color_id)
    {

        $sizeData = $this->businessProductsCategoryValuesRepository->getById($productsizeid);
        // $colorData=$this->businessProductsCategoryValuesRepository->getById($product_color_id);

        $order = $this->model->newInstance();
        $order->order_id = $order_id;
        $order->product_id = $productId;
        $order->business_id = $pageId;
        $order->user_id = $userid;
        $order->quantity = $cartQuantity;
        $order->amount = $subTotal;
        $order->price = $productPrice;
        $order->discount = $productDiscount;
        $order->shipping_cost = $shipping_cost;
        $order->community_id = $community_id;
        $order->community_business_id = $business_id;
        $order->order_date_time = $order_date_time;
        $order->productsizeid = $productsizeid;
        $order->product_color_id = $product_color_id;
        if ($sizeData) {
            $order->product_size = $sizeData->value;
        }

        if ($sizeData) {
            $order->product_color = $sizeData->color;
        }

        $order->save();
    }

    public function getByBusinessId($id, $community_id)
    {
        return $this->model->where('order_id', $id)->where('community_id', $community_id)->groupBy('community_business_id')->get();
    }

    public function getByBusinessIdAll($id, $community_business_id)
    {
        return $this->model->where('order_id', $id)->where('community_business_id', $community_business_id)->get();
    }

    public function getByCommunityBusinessId($community_business_id)
    {
        return $this->model->where('community_business_id', $community_business_id)->get();
    }

    public function getByOrderId($order_id)
    {
        return $this->model->where('order_id', $order_id)->get();
    }
}
