<?php

namespace Yajny\Models;

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

class Ad extends Model
{
    use HasTranslations, SelectTranslations;

    protected $table = 'ads';
    protected $guarded = [];
    public $translatable = ['link', 'app_link', 'image', 'mob_image', 'promo_text', 'title'];
    protected $appends = ['local_start_at', 'local_end_at'];

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('active', function (Builder $builder) {
            $now = Carbon::now()->toDateTimeString();

            $builder->where('status', 'active');
            $builder->where('start_at', '<=', $now);
            $builder->where(function ($query) use($now){
                $query->whereNull('end_at')->orWhere('end_at', '>=', $now);
            });
        });

        static::addGlobalScope('order', function (Builder $builder) {
            $builder->orderBy('ord');
        });
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id')->withoutGlobalScopes();
    }

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

    public function setLocalStartAtAttribute($value)
    {
        $this->attributes['start_at'] = Carbon::parse($value,'Africa/Cairo')->setTimezone('UTC');
    }

    public function getLocalStartAtAttribute()
    {
        return ($this->start_at == null) ? null : Carbon::parse($this->start_at,'UTC')->setTimezone('Africa/Cairo');
    }

    public function setLocalEndAtAttribute($value)
    {
        $this->attributes['end_at'] = (!empty($value)) ? Carbon::parse($value,'Africa/Cairo')->setTimezone('UTC') : null;
    }

    public function getLocalEndAtAttribute()
    {
        return ($this->end_at == null) ? null : Carbon::parse($this->end_at,'UTC')->setTimezone('Africa/Cairo');
    }

    public static function mapAdsItemsForApp($ad)
    {
        return [
            'id' => $ad->id,
            'image' => $ad->getTranslations('mob_image'),
            'store_id' => $ad->store_id,
            'app_link' => $ad->getTranslations('app_link'),
            'link_type' => $ad->link_type,
            'store' => Store::mapItems($ad->store),
            'category' => ($ad->category) ? Category::mapItems($ad->category):[]
        ];
    }

    public static function mapItemsForApp($ad)
    {
        $mobImage = $ad->getTranslations('mob_image');
        return [
            'id' => $ad->id,
            'label' => $ad->label,
            'title' => $ad->getTranslations('title'),
            'image' => $mobImage,
            'store_id' => $ad->store_id,
            'app_link' => $ad->getTranslations('app_link'),
            'link' => $ad->getTranslations('link'),
            'link_type' => $ad->link_type,
            'code' => $ad->coupon_code, // todo to be removed after force update for app
            'coupon_code' => $ad->coupon_code,
            'promo_text' => $ad->getTranslations('promo_text'),
            'mob_image' => $mobImage, // todo to be removed after force update for app
            'mob_image_en' => (isset($mobImage['en'])) ? $mobImage['en'] : null, // todo to be removed after force update for app
            'mob_image_ar' => (isset($mobImage['ar'])) ? $mobImage['ar'] : null, // todo to be removed after force update for app
            'store' => Store::mapItems($ad->store),
            'category' => ($ad->category) ? Category::mapItems($ad->category):[]
        ];
    }

    // todo to be removed after force update for app
    static function fixSliderAppLinKForOldVersion($ad)
    {
        $ad['app_link'] = $ad['app_link'][app()->getLocale()];
        return $ad;
    }
}
