<?php

namespace App\Models;

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

/**
 * @property Supplier supplier
 */
// class PriceList extends Model implements AuditableContract
class PriceList extends BaseAuditable
{
    public function attachment(): MorphOne
    {
        return $this->morphOne(Attachment::class, 'attachable');
    }

    // protected $hidden=['created_at'];

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

    private array $productRelations = ['Producer', 'picture', 'specification'];

    public function getProductRelations(): array
    {
        return $this->productRelations;
    }

    public function setProductRelations(array $productRelations): void
    {
        $this->productRelations = $productRelations;
    }

    public function products(): BelongsToMany
    {
        return $this->belongsToMany(Product::class, 'price_list_prices', 'price_list_id', 'product_id')
            // ->where('price_list_id','=',$priceListId)
            // ->where('product_group_id','=',12)
            ->whereRaw('price_list_prices.price_list_id = ?', [$this->id])
            ->withTimestamps()
            ->withPivot(['price'])
            // ->with('Producer')
            ->with($this->productRelations)

            // ->using(PricePivot::class)
            ->as('priceListPrice');
        // ->wherePivot('price','<','500')
    }

    public function productsWithRelations(): BelongsToMany
    {
        return $this->belongsToMany(Product::class, 'price_list_prices', 'price_list_id', 'product_id')
            ->withTimestamps()
            ->withPivot(['price'])
            ->with(['Producer', 'ProductGroup'])
            // ->using(PricePivot::class)
            ->as('priceListPrice');

        // ->wherePivot('price','<','500')
    }
}
