<?php
namespace App\Services\Product;

use App\Models\Attribute;
use App\Models\Product;
use App\Models\ProductAttribute;
use App\Models\ProductAttributeChoice;
use App\Services\Auth\User\AccountService;
use App\Services\Product\Cost\CostCompany;
use App\Services\Product\Cost\CostService;
use App\Services\Product\Cost\CostUser;
use App\Services\Response\ResponseService;
use Illuminate\Support\Facades\Auth;

class ProductService
{
    private $product;

    public function __construct(Product $product = null)
    {
        $this->product = $product;
    }
    public function relatedProducts()
    {
        $category_id = $this->product->category_id;
        $products = Product::withDescription()->where('category_id',$category_id)->where('products.id','!=',$this->product->id)->limit(5)->cursor();
        return $products;
    }


    public function productCost()
    {
        $account = (new AccountService())->getAccount();
        if($account['user'])
        {
            switch ($account['user']->role)
            {
                case 'company':
                    $cost = new CostCompany();
                    break;
                default:
                    $cost = new CostUser();
                    break;
            }
        }
        else
        {
            $cost = new CostUser();
        }
        $cost = new CostService($cost);
        $cost = $cost->calculateCost($this->product);
        return $cost;
    }

    public function productServices()
    {

        $one_services = ProductAttribute::where('product_id',$this->product->id)->where('choice_id','!=',null)->cursor();


        $multi_services = ProductAttribute::where('product_id',$this->product->id)->where('choice_id',null)->where('product_attribute_id',null)->where('product_attribute_choice_id',null)->first();

        return[
            'one_services' => $one_services,
            'multi_services' => $multi_services,
        ];
    }

    public function productCount($choice_id = null)
    {
        $count = $choice_id ? ProductAttributeChoice::find($choice_id)->count : $this->product->count;

        if($count < 1)
        {
            $message = $this->product->descriptions()->title . ' ' . __('web.finished_product');
            $response = '';
            $status = 400;

            return
                [
                    'message'=>[$message],
                    'status'=>$status,
                    'response'=>$response,
                ];
        }
        $message = __('web.success');
        $response = $count;
        $status = 200;

        return
            [
                'message'=>$message,
                'status'=>$status,
                'response'=>$response,
            ];
    }
    public function checkAvailable($count,$choice_id = null)
    {
        $checkCount = $this->productCount($choice_id);
        
        if($checkCount['status'] != 200)
        {
            return $checkCount;
        }

        $product_count = $checkCount['response'];

        if(!$this->product)
        {
            return ResponseService::notFound();
        }

        if($product_count < $count)
        {
            return ResponseService::responseFail([__('web.over_count')]);
        }
        
        $account = (new AccountService())->getAccount();
        $role = $account['user']->role;
        if($role == 'company')
        {
            if($this->product->min_order > $count)
            {
                return ResponseService::responseFail([__('web.min_order_req') . $this->product->min_order]);
            }
        }

        $message = '';
        $status = 200;
        $response = '';
        return
            [
                'message'=>$message,
                'status'=>$status,
                'response'=>$response,
            ];
    }
}
