<?php

namespace Yajny\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use Yajny\Traits\SelectTranslations;

class StoreDeal extends Model
{
    use HasTranslations, SelectTranslations;

    protected $table = 'store_deals';
    protected $primaryKey = 'id';
    protected $guarded = ['id'];

    public $translatable = ['title', 'desc', 'image', 'button_text', 'coupon_title', 'promo_text'];

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('enabled', function (Builder $builder) {
            $builder->where('enabled', 1);
        });
    }

    public function store()
    {
        return $this->belongsTo('Yajny\Models\Store', 'store_id', 'id')->withoutGlobalScopes();
    }

    public static function mapItemsForApp($storeDeal)
    {
        return [
            'id' => $storeDeal->id,
            'slug' => $storeDeal->slug,
            'title' => $storeDeal->getTranslations('title'),
            'description' => $storeDeal->getTranslations('desc'),
            'button_text' => $storeDeal->getTranslations('button_text'),
            'image' => $storeDeal->getTranslations('image'),
            'coupon_title' => $storeDeal->getTranslations('coupon_title'),
            'promo_text' => $storeDeal->getTranslations('promo_text'),
            'coupon_code' => $storeDeal->coupon_code,
            'store' => Store::mapItems($storeDeal->store)
        ];
    }
}
