<?php

namespace App\Models\Shop;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class SaleChannel extends Model
{
    protected $guarded = ['id'];

    protected $table = 'shop_sale_channels';

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

    public function customers(): BelongsToMany
    {
        return $this->belongsToMany(Customer::class, CustomerSaleChannel::class)->wherePivotNull('deleted_at');
    }

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

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

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

    public function scopeByCode($query, $code)
    {
        return $query->where($this->table.'.code', $code);
    }

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