<?php

namespace Hilco\Models;

/**
 * Hilco\Models\WebLandingPage
 *
 * @property integer $id
 * @property string $name
 * @property string $date_created
 * @property string $date_modified
 * @property string $deleted_at
 * @property-read mixed $context
 * @property-read mixed $default_context
 * @property-read mixed $web_layout
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\WebLandingPage whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\WebLandingPage whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\WebLandingPage whereDateCreated($value)
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\WebLandingPage whereDateModified($value)
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\WebLandingPage whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\WebLandingPage named($name)
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\WebModel joinContext($contextId = false)
 * @mixin \Eloquent
 * @property string $slug
 * @property string $language
 * @property string $detail
 * @property string $css
 * @property integer $createdby_user_id
 * @property boolean $requires_auth
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\WebLandingPage whereSlug($value)
 */
class WebLandingPage extends WebModel implements HasLandingPageInterface {

	protected $table = "WebLandingPages";
    protected $fillable = ['name', 'detail', 'slug', 'language', 'css', 'metadata_title', 'metadata_description', 'requires_auth'];
    protected $casts = ['requires_auth' => 'boolean'];

    public function slugUrl() {
        return route('landingpage.slug', [$this->slug]);
    }

    public function scopeVisible($query) {
        return $query;
    }

	use HasSlug, HasLandingPage, HasMetadata;

    public static function IsExists($name = null, $slug = null){
        if(!isset($name) && !isset($slug)) return true;
        $query = null;

        if(isset($name)){
            if(!isset($query)) $query = self::named($name);
            else $query = $query->named($name);
        }

        if(isset($slug)){
            if(!isset($query)) $query = self::where('slug',$slug);
            else $query = $query->orWhere('slug',$slug);
        }
        $result = $query->count();
        return (isset($result) && $result > 0);
    }

    public function webGroups() {
        return $this->belongsToMany(WebGroup::class, 'WebGroup_WebLandingPage', 'weblandingpage_id', 'webgroup_id');
    }

    public function getParentRelationName()
    {
        return false;
    }

    /**
     * @param $webSilo
     * @return bool
     */
    public function isBlockedForWebSilo ($webSilo) {
        $blockedPage = WebSiloBlockedPage::bySiloPage($webSilo->id, $this->id)->first();
        return isset($blockedPage);
    }

    public function getMetadataTitle() {
        $metadataTitle = !is_null($this->metadata) ? $this->metadata->where('meta_name', 'title')->first() : null;
        return isset($metadataTitle) ? $metadataTitle->meta_value : '';
    }

    public function getMetadataDescription() {
        $metadataDesc = !is_null($this->metadata) ? $this->metadata->where('meta_name', 'description')->first() : null;
        return isset($metadataDesc) ? $metadataDesc->meta_value : '';
    }

    /**
     * @return bool
     */
    public function requiresAuth(): bool {
        return $this->requires_auth;
    }
}