<?php

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

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

    public function contact()
    {
        return $this->belongsTo("Contact");
    }

    public function solutions()
    {
        $this->belongsToMany("Solution", "solution_sector");
    }

    public function relatedSolutions()
    {
        return $this->belongsToMany("Solution", "sector_solution", "sector_id", "solution_id");
    }

    public function caseStudies()
    {
        return $this->belongsToMany("CaseStudy", "case_study_sector");
    }

    public function relatedCaseStudies()
    {
        return $this->belongsToMany("CaseStudy", "sector_case_study");
    }

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

    public function sidebarWidgets()
    {
        return $this->belongsToMany('SidebarWidget', 'sector_sb_widgets');
    }

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

    public function highlightImage()
    {
        return $this->belongsTo("MediaItem");
    }

    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->highlightImage;
        $fieldsObject->image_alt = $this->title;
        $fieldsObject->cta = generateObjectLinkJson($this);
        $fieldsObject->cta2 = null;

        return $fieldsObject;
    }
}
