<?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',
    ];


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

    public function getDateTimeAttribute()
    {
        $time = date('D',strtotime($this->date));
        $date = __('web.'.$time) .', ';
        $date .= date('Y/m/d',strtotime($this->date));
        return $date;
    }
    public function features()
    {
        return $this->hasMany(ProductFeature::class);
    }
    public function photos() :hasMany
    {
        return $this->hasMany(ProductPhoto::class);
    }

    public static function withDescription($product_id = null)
    {
        $query = self::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;
    }
}
