<?php

namespace Yajny\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Position extends Model
{
    /**
     * @var string
     */
    protected $table = 'positions';
    /**
     * @var array
     */
    protected $guarded = [];

    /**
     *
     */
    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('active', function (Builder $builder) {
            $builder->where('status', 'active');
        });

        static::deleting(function ($position) {
            $position->ads()->delete();
        });
    }

    /**
     * @return mixed
     */
    public function ads()
    {
        return $this->hasMany(Ad::class, 'position_id', 'id');
    }

    /**
     * @param $label
     * @param $region
     * @return mixed
     */
    public static function getByLabel($label, $region)
    {
        return self::with([
            'ads' => function ($query) use ($region) {
                $query->whereJsonContains('region_code', $region ?? 'global');
            },
            'ads.store'
        ])
            ->where('status', 'active')
            ->where('label', $label)
            ->whereHas('ads', function ($query) use ($region) {
                $query->whereJsonContains('region_code', $region ?? 'global');
            })
            ->get()
            ->keyBy('slug')
            ->filter(function ($position) {
                return $position->type != 'ads' || $position->ads->count() == $position->size;
            });
    }
    public static function getByLabelwithoutRegion($label)
        {
            return self::with([

                'ads.store'
            ])
                ->where('status', 'active')
                ->where('label', $label)
                ->get()
                ->keyBy('slug')
                ->filter(function ($position) {
                    return $position->type != 'ads' || $position->ads->count() == $position->size;
                });
        }

    /**
     * @param $label
     * @param $region
     * @return mixed
     */
    public static function getFeaturedByLabel($label, $region,$position='mobile_discovery_main_featured')
    {
        $positions= self::with([
            'ads' => function ($query) use ($region) {
                $query->whereJsonContains('region_code', $region ?? 'global');

            },
            'ads.store'
        ])
            ->where('status', 'active')
            ->where('label', $label)
            ->whereHas('ads', function ($query) use ($region) {
                $query->whereJsonContains('region_code', $region ?? 'global');

            })
            ->get()
            ->keyBy('slug')
            ->filter(function ($position) {
                return $position->type != 'ads' || $position->ads->count() == $position->size;
            })->map(function ($position) {
                return Position::mapItemsForApp($position);
            });

        $sliders = null;
        $featuredads = null;
        if(!empty($positions)){
            if (isset($positions[$position])) {
                $sliders = $positions[$position]['ads']->toArray();

                foreach ($sliders as $index => $ad) {
                    $sliders[$index] = Ad::fixSliderAppLinKForOldVersion($ad);
                }
                $featuredads=array_slice($sliders,0,2) ?? null;
            }
        }
        return $featuredads;
    }
    /**
     * @param $label
     * @param $region
     * @return mixed
     */
    public static function getFeaturedByLabelwithoutRegion($label,$position='mobile_discovery_main_featured')
    {
        $positions= self::with([
            
            'ads.store'
        ])
            ->where('status', 'active')
            ->where('label', $label)
            ->get()
            ->keyBy('slug')
            ->filter(function ($position) {
                return $position->type != 'ads' || $position->ads->count() == $position->size;
            })->map(function ($position) {
                return Position::mapItemsForApp($position);
            });

        $sliders = null;
        $featuredads = null;
        if(!empty($positions)){
            if (isset($positions[$position])) {
                $sliders = $positions[$position]['ads']->toArray();

                foreach ($sliders as $index => $ad) {
                    $sliders[$index] = Ad::fixSliderAppLinKForOldVersion($ad);
                }
                $featuredads=array_slice($sliders,0,2) ?? null;
            }
        }
        return $featuredads;
    }

    /**
     * @param $position
     * @return array
     */
    public static function mapItemsForApp($position)
    {
        $mappedPosition = [
            'id' => $position->id,
            'size' => $position->size,
            'type' => $position->type,
            'ads' => $position->ads->map(function ($ad) {
                return Ad::mapItemsForApp($ad);
            })
        ];
        return $mappedPosition;
    }
}
