<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends BaseAuditable
{
    use SoftDeletes;

    protected $guarded = ['id'];
    // protected $fillable=['name'];

    public function ProductGroup(): BelongsTo
    {
        return $this->belongsTo(ProductGroup::class);
    }

    public function Supplier(): BelongsTo
    {
        return $this->belongsTo(Supplier::class);
    }

    public function Producer(): BelongsTo
    {
        return $this->belongsTo(Producer::class);
    }

    public function Price(?string $deliveryDate = null)
    {
        $price = $this->belongsToMany(PriceList::class, 'price_list_prices', 'product_id', 'price_list_id')->select('id')
//            ->withTimestamps()
            ->withPivot(['price']);

        if (! $deliveryDate) {
            $deliveryDate = Carbon::today()->format('Y-m-d');
        }
        $price->where('available', '<=', $deliveryDate);
        $price->orderBy('available', 'desc')->orderBy('id', 'asc');
        $price->first();
        if ($price = $price->get()->toArray()) {
            $price = $price[0]['pivot']['price'];
        }

        return $price;
    }

    public function setAttributePrice(?string $deliveryDate = null)
    {
        $this->setAttribute('price', $this->price($deliveryDate));
    }

    public function LastPrice(): BelongsToMany
    {

        return $this->belongsToMany(PriceList::class, 'price_list_prices', 'product_id', 'price_list_id')->select('id')
            ->withTimestamps()
            ->withPivot(['price'])
            ->where('available', '<=', Carbon::today()->format('Y-m-d'))
            ->orderBy('available', 'desc')->orderBy('id', 'desc');
        // ->limit(1)
        // ->as('priceListPrice')
    }

    public function PriceList(): BelongsToMany
    {
        return $this->belongsToMany(PriceList::class, 'price_list_prices', 'product_id', 'price_list_id')
            ->withTimestamps()
            ->withPivot(['price']);
        // ->as('priceListPrice')
    }

    public function ProfitCenter(): BelongsToMany
    {
        return $this->belongsToMany(ProfitCenter::class, 'profit_center_favorites')->withTimestamps();
    }

    public function picture(): MorphOne
    {
        return $this->morphOne(Attachment::class, 'attachable')->where('attachType', '=', 'picture');
    }

    public function specification(): MorphOne
    {
        return $this->morphOne(Attachment::class, 'attachable')->where('attachType', '=', 'specification');
    }

    public function getActualPriceList(?string $deliveryDate = null): ?PriceList
    {
        $price = $this->belongsToMany(PriceList::class, 'price_list_prices', 'product_id', 'price_list_id')->select('id')
//            ->withTimestamps()
            ->withPivot(['price']);

        if (! $deliveryDate) {
            $deliveryDate = Carbon::today()->format('Y-m-d');
        }
        $price->where('available', '<=', $deliveryDate);
        $price->orderBy('available', 'desc')->orderBy('id', 'asc');

        // $price->first();
        return $price->get()->first();
    }
}
