<?php

namespace Yajny\Models;

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

class StripBanner extends Model
{
    use HasTranslations, SelectTranslations;

    protected $table = 'strip_banners';
    protected $guarded = [];
    protected $appends = ['local_from', 'local_to'];

    public $timestamps = true;
    public $translatable = ['text', 'link', 'app_link'];
    public $selColmns = ['id', 'store_id', 'text', 'link_type', 'link', 'app_link', 'app_deep_link', 'platform', '`from`', '`to`', 'background_color', 'text_color'];

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

            $builder->where('is_active', true);
            $builder->where('from', '<=', $today);
            $builder->where('to', '>=', $today);
        });
    }

    private function convertDateTimeTimezone($date, $fromTz, $toTz)
    {
        return Carbon::createFromFormat('Y-m-d H:i:s', $date, $fromTz)->timezone($toTz)->toDateTimeString();
    }

    public function getLocalFromAttribute()
    {
        return ($this->from) ? $this->convertDateTimeTimezone($this->from, 'UTC', 'Africa/Cairo') : null;
    }

    public function getLocalToAttribute()
    {
        return ($this->to) ? $this->convertDateTimeTimezone($this->to, 'UTC', 'Africa/Cairo') : null;
    }

    public function setLocalFromAttribute($value)
    {
        $this->attributes['from'] = $this->convertDateTimeTimezone($value, 'Africa/Cairo', 'UTC');
    }

    public function setLocalToAttribute($value)
    {
        $this->attributes['to'] = $this->convertDateTimeTimezone($value, 'Africa/Cairo', 'UTC');
    }

    public static function getForWeb($region_code)
    {
        return self::with(['store' => function ($q) {
            $q->selectRaw(Store::colSelect());
        }])
            ->where('region', $region_code)
            ->where('platform', '!=', 'app')
            ->selectRaw(StripBanner::colSelect())
            ->first();
    }

    public static function getForApp($region_code)
    {
        return self::with(['store' => function ($q) {
            $q->selectRaw(Store::colSelect());
        }])
            ->where('region', $region_code)
            ->where('platform', '!=', 'web')
            ->selectRaw(StripBanner::colSelect())
            ->first();
    }

    public static function mapItemsForApp($strip_banner)
    {
        return [
            'id' => $strip_banner->id,
            'store_id' => $strip_banner->store_id,
            'text' => [
                'en' => $strip_banner->text_en,
                'ar' => $strip_banner->text_ar,
            ],
            'link_type' => $strip_banner->link_type,
            'link' => [
                'en' => $strip_banner->link_en,
                'ar' => $strip_banner->link_ar,
            ],
            'app_link' => [
                'en' => $strip_banner->app_link_en,
                'ar' => $strip_banner->app_link_ar,
            ],
            'app_deep_link' => $strip_banner->app_deep_link,
            'date_from' => $strip_banner->from,
            'date_to' => $strip_banner->to,
            'background_color' => $strip_banner->background_color,
            'text_color' => $strip_banner->text_color,
            'store' => Store::mapItems($strip_banner->store)
        ];
    }

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