<?php

namespace App\Models\Advisories;

use App\Models\Companies\CompanyMember;
use App\Support\RelationValuesTrait;
use App\Support\UuidScopeTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class AdvisoryMember extends Model {
    use UuidScopeTrait, SoftDeletes, RelationValuesTrait;

    protected $dates = [
    ];

    protected $fillable = [
        'uuid',
        'advisory_id',
        'company_member_id',
        'rank'
    ];

    protected $hidden = [
        'id'
    ];



    public function advisory(): BelongsTo {
        return $this->belongsTo(Advisory::class);
    }

    public function companyMember(): BelongsTo {
        return $this->belongsTo(CompanyMember::class);
    }



    public function getTitleAttribute(): string {
        $rank = AdvisoryMemberRank::CATEGORIES[$this->rank] ?? 'Член';
        return "{$rank} {$this->advisory->number} {$this->companyMember->company->name}";
    }

    public function getParsedRankAttribute(): array {
        return ['name' => AdvisoryMemberRank::NAMES[$this->rank] ?? null, 'title' => AdvisoryMemberRank::TITLES[$this->rank] ?? null, 'level' => $this->rank];
    }

    public function getAdvisoryCompanyAttribute(): ?Model {
        return ($this->advisory && $this->companyMember) ? $this->advisory->advisoryCompanies()->where(['company_id' => $this->companyMember->company_id])->first() : null;
    }



    public function setRank($rank = null) {
        $this->update(['rank' => $rank ?? AdvisoryMemberRank::ORDINARY]);
    }


}
