<?php

namespace App\Models\Shop;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Venturecraft\Revisionable\RevisionableTrait;
use Wildside\Userstamps\Userstamps;

class Delivery extends Model
{
    use RevisionableTrait, SoftDeletes, Userstamps;

    protected $guarded = ['id'];

    protected $table = 'shop_deliveries';

    protected $revisionEnabled = true;

    protected $keepRevisionOf = [
        'sale_channel_id',
        'active',
        'is_public',
        'at_house',
        'name',
        'description',
        'address',
        'address2',
        'zipcode',
        'country',
        'event_date_begin',
        'event_date_end',
    ];

    public function customers()
    {
        return $this->hasMany(Customer::class);
    }

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

    public function scopeActive($query)
    {
        return $query->byActive(1);
    }

    public function scopeInactive($query)
    {
        return $query->byActive(0);
    }

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

    public function scopeByPublic($query, $isPublic)
    {
        return $query->where($this->table.'.is_public', $isPublic);
    }

    public function scopeAtHouse($query)
    {
        return $query->where($this->table.'.at_house', 1);
    }

    public function scopeManaged($query)
    {
        return $query->byPublic(0);
    }

    public function scopePublic($query)
    {
        return $query->byPublic(1);
    }
}
