<?php

namespace Modules\Product\Models;

use Modules\Category\Models\Category;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Order\Models\Order;
use Modules\Order\Models\OrderProduct;
use Modules\Rate\Models\Rate;
use Modules\Store\Models\Store;
use Modules\User\Services\AccountService;
use Modules\Order\Enums\OrderStatusEnum;

class Product extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'code', 'count', 'status', 'cost', 'cost_discount', 'category_id', 'sub_category_id','image'
    ];

    public function photos()
    {
        return $this->hasMany(ProductPhoto::class);
    }

    public function sizes()
    {
        return $this->hasMany(ProductAttribute::class)->where('main_attribute_id',null);
    }
    public function colors()
    {
        return $this->hasMany(ProductAttribute::class)->where('main_attribute_id','!=',null);
    }

    public function items()
    {
        return $this->hasMany(ProductItem::class);
    }
    public function rates()
    {
        return $this->hasMany(Rate::class,'product_id');
    }

    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id')->withTrashed();
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id')->withTrashed();
    }

    public function subCategory()
    {
        return $this->belongsTo(Category::class, 'sub_category_id')->withTrashed();
    }

    public function favorites()
    {
        return $this->hasMany(Favorite::class,'product_id');
    }

    public function orders()
    {
        return $this->hasMany(OrderProduct::class,'product_id');
    }

    /**
     * Get current description of the the Room
     * @return HasOne
     */
    public function currentDescription(): HasOne
    {
        return $this->hasOne(ProductDescription::class)->where('language_id', currentLanguage()->id);
    }
//
//    public function getImageAttribute()
//    {
//        $image = $this->photos()->first() ? asset($this->photos()->first()->image) : asset('logo.png');
//        return $image;
//    }

    public function getPriceAttribute()
    {
        $price = $this->cost_discount ? $this->cost_discount : $this->cost;
        return number_format($price,2);
    }

    public function getPricingAttribute()
    {
        $pricing =
            [
                'main_cost' => $this->cost,
                'offer' => '',
            ];
        if ($this->cost_discount) {
            $offer = number_format((($this->cost - $this->cost_discount) / $this->cost) * 100, 2);
            $pricing['offer'] = __('web.off') . ' ' . $offer . '%';
        }

        return $pricing;
    }

    public function getRateAttribute()
    {
        $account = (new AccountService())->getAccount();
        $count = Rate::where('product_id',$this->id)->count();
        if ($count)
        {
            $rate = Rate::where('product_id',$this->id)->sum('rate') / $count;
        }
        else
        {
            $rate = 0;
        }
        return number_format($rate,1);
    }

    public function getRateCountAttribute()
    {
        $account = (new AccountService())->getAccount();
        $rate = Rate::where('product_id',$this->id)->count();
        return $rate;
    }

    public function getCanRateAttribute()
    {
        $account = (new AccountService())->getAccount();
        $show = true;
        $rate = Rate::where('user_id',$account['user']->id)->where('status',$account['status'])->where('product_id',$this->id)->first();

        if ($rate)
        {
            $show = false;
        }
        else
        {
            $order = Order::join('order_products AS op', 'orders.id', 'op.order_id')
                ->where('orders.status',OrderStatusEnum::RECEIVED->value)
                ->where('orders.user_id',$account['user']->id)
                ->where('orders.user_status',$account['status'])
                ->where('op.product_id',$this->id)
                ->count();
            if (!$order)
            {
                $show = false;
            }

        }
        return $show;
    }
}
