<?php

namespace App\Models\Shop;

use App\Traits\Model\HasComments;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Znck\Eloquent\Traits\BelongsToThrough;

class PriceList extends Model
{
    use BelongsToThrough, HasComments;

    protected $guarded = ['id'];

    protected $table = 'shop_price_lists';

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

    public function offers(): HasManyThrough
    {
        return $this->hasManyThrough(Offer::class, Tariff::class, 'id', 'tariff_id', 'id', 'id');
    }

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

    public function price_list_values(): HasMany
    {
        return $this->hasMany(PriceListValue::class);
    }

    public function scopeByTariff($query, $id)
    {
        return $query->where($this->table.'.tariff_id', $id);
    }

    public function scopeBySaleChannel($query, $id)
    {
        return $query->where($this->table.'.sale_channel_id', $id);
    }

    public function scopeByStatus($query, $id)
    {
        return $query->where($this->table.'.status_id', $id);
    }

    public function scopeActive($query)
    {
        return $query->byStatus(0)->has('price_list_values');
    }

    public function scopeByOffer($query, $id)
    {
        return $query->whereHas('offers', function ($query) use ($id) {
            $query->byID($id);
        });
    }
}
