<?php

    namespace App\Models;

    use App\Libs\Database\Eloquent\ContentTypeModel;
    use App\Models\GenericBackoffice\Traits\LanguageSupportTrait;

    class Epigraph extends ContentTypeModel
    {
        const NOT_MUSEUM_IDS = [35, 43, 44];

        use LanguageSupportTrait;

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

        protected $fillable = [
            'external_id',
            'external_reference',
            'local_id',
            'address',
            'geoY',
            'inscription_type_id',
            'technique_id',
            'idiom_id'
        ];

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

        public function themes()
        {
            return $this->belongsToMany(Theme::class)->withPivot('position')->withTimestamps();
        }

        public function classifications()
        {
            return $this->belongsToMany(Classification::class)->withPivot('position')->withTimestamps();
        }

        public function technique()
        {
            return $this->belongsTo(Technique::class);
        }

        public function inscriptionType()
        {
            return $this->belongsTo(InscriptionType::class);
        }

        public function idiom()
        {
            return $this->belongsTo(Idiom::class);
        }

        public function location()
        {
            return $this->belongsTo(Location::class, 'local_id');
        }

        public function images()
        {
            return $this->belongsToMany(PropertyImage::class, 'epigraph_epigraph_image', 'epigraph_id', 'epigraph_image_id')->withPivot('position')->withTimestamps();
        }

        public function properties()
        {
            return $this->belongsToMany(Property::class)->withPivot(['position', 'found_local_id'])->withTimestamps();
        }

        public function chronologies()
        {
            return $this->belongsToMany(Chronology::class)->withPivot('position')->withTimestamps();
        }

        public function audioVisuals()
        {
            return $this->belongsToMany(AudioVisual::class)->withPivot('position')->withTimestamps();
        }

        public static function foundLocalId($typeId)
        {
            if (in_array($typeId, self::NOT_MUSEUM_IDS)) {
                return 1;
            } else {
                return 2;
            }
        }
    }
