<?php
namespace App\Services\Product\Shipping;

use App\Models\Address;
use App\Models\Shipping;
use App\Models\ShippingCity;
use App\Services\Currency\CurrencyService;
use App\Services\Auth\User\AccountService;

class ShippingService
{
    public function calculate($requestContent)
    {
        $shipping_status = false;
        $shipping_cost = 0;
        $shipping_offer_percentage = 0;
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        $discount = 0;
        if($user->role != 'company')
        {
            $total = $requestContent['total'];
            $address_id = $requestContent['address_id'];
            $shipping_id = $requestContent['shipping_id'];
            $address = Address::find($address_id);
            
            if($address)
            {
                $city_id = $address->city_id;
                
                $shipping = Shipping::find($shipping_id);
                if($shipping)
                {
                    $shipping_status = true;
                    $shipping_cost = ShippingCity::where('shipping_id',$shipping_id)->where('city_id',$city_id)->first()->tax;
                    if($shipping->min_order)
                    {
                        $currency =(new CurrencyService())->getCurrency();
                        $min_order = $shipping->min_order * $currency->currency;
                        if($min_order <= $total)
                        {
                            $shipping_offer_percentage = $shipping->discount;
                            $discount = $shipping_cost * ($shipping->discount / 100);
                            $shipping_cost = $shipping_cost - $discount;
                        }
                    }
                }
            }
        }
        return 
        [
            'total'=>$requestContent['total'],
            'status' => $shipping_status,
            'offer_percentage' => $shipping_offer_percentage,
            'discount' => round($discount,2),
            'shipping_cost' => round($shipping_cost,2),
        ];
    }


}
