<?php

namespace App\Models\Backoffice;

use App\Models\Advertisement;
use App\Models\Backoffice\Support\ContentTypeModel;
use App\Models\Backoffice\Traits\LanguageSupportTrait;
use App\Models\CardCompetitionItem;
use App\Models\Competition;
use App\Models\Result;
use App\Models\SubPlan;
use App\Models\TextItem;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Traits\HasSpatial;

class Block extends ContentTypeModel
{
    use LanguageSupportTrait;
    use HasSpatial;

    protected $hidden = ['languages', "websites"];
    protected $with = ['languages', "websites"];

    protected $dates = []; // dates

    protected $casts = [
        'is_active' => 'boolean',
        'link' => 'json',
        'second_link' => 'json',
        'location' => Point::class
    ];

    public function slider()
    {
        return $this->belongsToMany(SliderItem::class)->withPivot('position')->orderBy('position');
    }

    public function cardsCompetition()
    {
        return $this->belongsToMany(CardCompetitionItem::class)->withPivot('position')->orderBy('position');
    }

    public function advertisements()
    {
        return $this->belongsToMany(Advertisement::class)->withPivot('position')->orderBy('position');
    }

    public function results()
    {
        return $this->belongsToMany(Result::class)->withPivot('position')->with('winners')->orderBy('position');
    }

    public function competitions()
    {
        return $this->belongsToMany(Competition::class)->withPivot('position')->orderBy('position');
    }

    public function subscriptionPlans()
    {
        return $this->belongsToMany(SubPlan::class)
            ->withPivot('position')->orderBy('position');
    }

    public function textItems()
    {
        return $this->hasMany(TextItem::class);
    }

    public function page()
    {
        return $this->belongsToMany(Page::class, 'page_block')->withPivot('position');
    }

    public function mediaItemsReferences()
    {
        return $this->morphToMany(MediaItem::class, 'referenceable', 'media_item_references')->withTimestamps();
    }

    public function image() {
        return $this->belongsTo(MediaItem::class);
    }

    public function secondImage() {
        return $this->belongsTo(MediaItem::class);
    }
}
