<?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\HasManyThrough;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Znck\Eloquent\Relations\BelongsToThrough as RelationsBelongsToThrough;
use Znck\Eloquent\Traits\BelongsToThrough;

class Offer extends Model
{
    use BelongsToThrough, HasComments, HasRelationships;

    protected $guarded = ['id'];

    protected $table = 'shop_offers';

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

    public function article_nature(): RelationsBelongsToThrough
    {
        return $this->belongsToThrough(ArticleNature::class, Article::class, null, '', [
            'App\Models\Shop\Article' => 'article_id',
            'App\Models\Shop\ArticleNature' => 'article_nature_id',
        ]);
    }

    public function categories()
    {
        return $this->article->categories();
    }

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

    public function price_list_values()
    {
        return $this->hasManyDeep(
            PriceListValue::class,
            [Tariff::class, PriceList::class],
            ['id', 'tariff_id', 'price_list_id'],
            ['tariff_id', 'id', 'id'],
        );
    }

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

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

    public function tags()
    {
        return $this->article->tags();
    }

    public function get_price_lists()
    {
        return $this->tariff->price_lists();
    }

    public function get_price_list_values()
    {
        return $this->tariff->price_lists->price_list_values();
    }

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

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

    public function scopeByArticles($query, $ids)
    {
        return $query->whereIn($this->table.'.article_id', $ids);
    }

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

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

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

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

    public function scopeByStockAvailable($query)
    {
        return $query->where($this->table.'.stock_current', '>', 0);
    }

    public function scopeByIds($query, $ids)
    {
        return $query->whereIn('id', $ids);
    }

    public function scopeByArticleNature($query, $article_nature_id)
    {
        return $query->whereHas('article.article_nature', function ($query) use ($article_nature_id) {
            $query->byArticleNature($article_nature_id);
        });
    }

    public function scopeByCategory($query, $category_id)
    {
        return $query->whereHas('article.categories', function ($query) use ($category_id) {
            $query->where('category_id', $category_id);
        });
    }

    public function scopeByPackage($query, $package_id)
    {
        return $query->whereHas('variation', function ($query) use ($package_id) {
            $query->byPackage($package_id);
        });
    }

    public function scopeByTag($query, $tag_id)
    {
        return $query->whereHas('article.tags', function ($query) use ($tag_id) {
            $query->where('tag_id', $tag_id);
        });
    }

    public function scopeByTags($query, $tags)
    {
        return $query->whereHas('article.tags', function ($query) use ($tags) {
            $query->whereIn('tag_id', $tags);
        });
    }

    public function scopeHasTariffActive($query)
    {
        return $query->whereHas('tariff', function ($query) {
            $query->active();
        });
    }

    public function scopeBySaleChannel($query, $sale_channel_id)
    {
        return $query->whereHas('price_lists', function ($query) use ($sale_channel_id) {
            $query->active()->bySaleChannel($sale_channel_id);
        });
    }

    public function scopeWithPriceListsBySaleChannel($query, $sale_channel_id)
    {
        return $query->with([
            'price_lists' => function ($query) use ($sale_channel_id) {
                $query->bySaleChannel($sale_channel_id);
            },
        ]);
    }

    public function scopeWithPriceListValuesBySaleChannel($query, $sale_channel_id)
    {
        return $query->with([
            'price_list_values' => function ($query) use ($sale_channel_id) {
                $query->bySaleChannel($sale_channel_id);
            },
        ]);
    }

    public function scopeWithPriceBySaleChannelByQuantity($query, $sale_channel_id, $quantity = 1)
    {
        return $query->with([
            'price_lists' => function ($query) use ($sale_channel_id) {
                $query->bySaleChannel($sale_channel_id);
            },
            'price_lists.price_list_values' => function ($query) use ($quantity) {
                $query->byQuantity($quantity);
            },
        ]);
    }
}
