<?php

namespace App\Models;

use App\Models\Traits\LanguageSupportTrait;
use App\Models\Traits\BlocksTrait;
use App\Models\Interfaces\CardableModel;
use stdClass;

class Entry extends ContentTypeModel implements CardableModel
{
    use LanguageSupportTrait;
    use BlocksTrait;

    protected $hidden = array('languages', "websites");
    protected $with = array("websites", "languages");

    public function blocks()
    {
        return $this->belongsToMany(Block::class);
    }

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

    public function metaImage()
    {
        return $this->belongsTo(MediaItem::class, "meta_image_id");
    }

    public function codeEmbeds()
    {
        return $this->belongsToMany(CodeEmbed::class, "entry_code_embeds");
    }

    /*
    public static function withExtra($fullData = false){
        if($fullData){
            return self::with("websites", "languages");
        }else{
            $filters = array("websites" => function($query){
                $query->where("website_id" , Localization::getWebsite()->id);
            });

            $filters["languages"] = function($query){
                $defaultLocale = Localization::getDefaultLocale();
                $activeLocale = Localization::getActiveLocale();
                $languages = array($defaultLocale);
                if(!empty($activeLocale) && $activeLocale != $defaultLocale){
                    $languages[] = $activeLocale;
                }

                $query->whereIn("language_id" , $languages);
            };


            return self::with($filters);

        }
    }
    */

    public function getCardFields()
    {
        $wo = $this->websites()->first();
        if ($wo->status != 'published' || $wo->lang_status != 'published') {
            return null;
        }

        $fieldsObject = new \stdClass();
        $fieldsObject->subject = '';
        $fieldsObject->title = $this->title;
        $fieldsObject->excerpt = '<p>' . $this->reference_summary . '</p>';
        $fieldsObject->image = $this->image;
        $fieldsObject->image_alt = $this->title;
        $fieldsObject->cta = generateObjectLinkJson($this);
        $fieldsObject->cta2 = null;

        return $fieldsObject;
    }
}
