<?php

namespace App\Models;

use App\Services\Auth\User\AccountService;
use App\Services\Currency\CurrencyService;
use App\Services\Product\Favorite\FavoriteService;
use App\Services\Product\ProductService;
use App\Services\Product\Rate\RateService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

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

    protected $fillable = [
        'image','code','count','model_id','num_buy','category_id','most_sale','brand_id','gender','min_order','views','image_size','status','video','video_image'
    ];
    public function remain()
    {
        return $this->count;
    }
    
        
    public function attributeChoiceMulti($main_attr_id,$main_choice_id,$sub_attr =null,$sub_choice=null)
    {
        $response = [
            'count'=>null,
            'code'=>null,
            'image'=>null
        ];
        $main_attribute = ProductAttribute::where('product_id',$this->id)
            ->where('attribute_id',$main_attr_id)->where('choice_id',null)->where('product_attribute_id',null)->first();
        if($main_attribute)
        {
            $choice_main = ProductAttributeChoice::where('product_attribute_id',$main_attribute->id)
                ->where('choice_id',$main_choice_id)->first();
            $response = 
            [
                'count'=>$choice_main->count ?? null,
                'code'=>$choice_main->code ?? null,
                'image'=>$choice_main->image ?? null
            ];
            if($sub_choice && $sub_attr)
            {
                if ($choice_main)
                {
                    $sub_attribute = ProductAttribute::where('product_id',$this->id)
                    ->where('product_attribute_id',$choice_main->product_attribute_id)
                    ->where('attribute_id',$sub_attr)
                    ->where('product_attribute_choice_id',$choice_main->id)->first();
    
                    if ($sub_attribute)
                    {
                        $choice_sub = ProductAttributeChoice::where('product_attribute_id',$sub_attribute->id)
                            ->where('choice_id',$sub_choice)->first();
                        if($choice_sub)
                        {
                            $response = [
                                'count'=>$choice_sub->count,
                                'code'=>$choice_sub->code,
                                'image'=>$choice_sub->image,
                            ];
                        }
                    }
                }
            }
        }

        return $response;
    }
    public function attributeChoice($attribute_id,$choice_id)
    {
        $response = false;
        $check = ProductAttribute::where('product_id',$this->id)
            ->where('attribute_id',$attribute_id)
            ->where('choice_id',$choice_id)
            ->first();
        if($check)
        {
            $response = true;
        }
        return $response;
    }

    public function checkRate()
    {
        $user = (new AccountService())->getAccount();

        $status = $user['status'];
        $user = $user['user'];
        $rate = Rate::where('product_id',$this->id)
            ->where('user_id',$user->id)
            ->where('status',$status)->first();
        if($rate)
        {
            return false;
        }
        return true;
    }

    public function attribute($attribute_id)
    {
        $attribute = ProductAttribute::where('product_id',$this->id)
            ->where('attribute_id',$attribute_id)
            ->first();
        return $attribute;
    }

    public function brand()
    {
        return $this->belongsTo(Brand::class)->withTrashed();
    }

    public function model()
    {
        return $this->belongsTo(Category::class,'model_id')->withTrashed();
    }
    public function category()
    {
        return $this->belongsTo(Category::class,'category_id')->withTrashed();
    }
    public function city()
    {
        return $this->belongsTo(City::class)->withTrashed();
    }
    public function favorited()
    {
        $check = (new FavoriteService($this))->checkFavorite();
        return $check;
    }
    public function costs($status,$target)
    {
        $data = $this->hasOne(ProductCost::class)->where('status',$status)->first();

        return $data->{$target};
    }

    public function getSoldAttribute()
    {
        if(!$this->num_buy)
        {
            $count = OrderProduct::where('product_id',$this->id)->sum('count');
        }
        else
        {
            $count = $this->num_buy;
        }
        return $count;
    }
    public function getRateAttribute()
    {
        $rate = (new RateService($this))->getRate();
        return $rate;
    }
    public function getImageUrlAttribute()
    {
        return asset($this->image);
    }
    public function getPricesAttribute()
    {
        $cost = (new ProductService($this))->productCost();
        return $cost;
    }
    public function getCostAttribute()
    {
        $cost = (new ProductService($this))->productCost();
        return $cost['cost_discount'] ? $cost['cost_discount'] : $cost['cost'];
    }

    public function currentDescription()
    {
        return $this->hasOne(ProductDescription::class)->where('language_id',currentLanguage()['id']);
    }

    public function getUrlApplyAttribute()
    {
        $link = route('apply_course',$this->id);
        return $link;
    }
    public function getDateTimeAttribute()
    {
        $time = date('D',strtotime($this->date));
        $date = __('web.'.$time) .', ';
        $date .= date('Y/m/d',strtotime($this->date));
        return $date;
    }

    public function getUrlAttribute()
    {
        return route('courses.show',$this->id);
    }

    public function details($language_id= null)
    {
        return $this->hasOne(ProductDescription::class)->where('language_id', currentLanguage()['id']);
    }
    public function descriptions($language_id= null)
    {

        if ($language_id) {
            $data = $this->hasMany(ProductDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(ProductDescription::class)->first();
            }

            return $data;
        }
        $data = $this->hasMany(ProductDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }

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

    public static function withDescription($product_id = null)
    {
        $query = self::orderByDesc('products.id')->join('product_descriptions as ad', 'ad.product_id', 'products.id')
            ->where('ad.language_id', currentLanguage()->id)
            ->select(['products.created_at', 'ad.*']);

        if ($product_id)
        {
            if (is_array($product_id))
            {
                $query->whereIn('products.id', $product_id);
            }
            else
            {
                $query->where('products.id', $product_id);
            }
        }
        $query->select([
            'products.*',
            'ad.name',
            'ad.description',
        ]);
        return $query;
    }
}
