<?php

namespace App\Models\Companies;

use App\Models\Advisories\AdvisoryMember;
use App\Models\User;
use App\Support\RelationValuesTrait;
use App\Support\UuidScopeTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

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

    protected $dates = [
        'deleted_at',
    ];

    protected $fillable = [
        'uuid',
        'user_id',
        'company_id',
        'department_id',
        'rank',
        'role',
        'position',
        'room',
        'intercom',
        'selected'
    ];

    protected $hidden = [
        'id'
    ];



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

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

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

    public function advisoryMembership(): HasMany {
        return $this->hasMany(AdvisoryMember::class);
    }



    public function getIsMeAttribute(): ?bool {
        return ($user = Auth::user()) ? ($user->id === $this->user_id) : null;
    }

    public function getSubordinatesAttribute() {
        $query = $this->department ? $this->department->members() : $this->company->members()->whereNull('department_id');
        return $query->where('role', '<', $this->role)->get();
    }

    public function getSuperiorsAttribute() {
        $query = $this->department ? $this->department->members() : $this->company->members()->whereNull('department_id');
        return $query->where('role', '>', $this->role)->get();
    }

    public function getIsChiefAttribute(): bool {
        return $this->role === CompanyMemberRank::CHIEF;
    }

    public function getIsViceAttribute(): bool {
        return $this->role === CompanyMemberRank::VICE;
    }

    public function getIsManagementAttribute(): bool {
        return in_array($this->role, [CompanyMemberRank::VICE, CompanyMemberRank::CHIEF]);
    }

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

    public function getParsedRoleAttribute(): array {
        return ['name' => $this->role, 'title' => CompanyMemberRole::TITLES[$this->role] ?? null];
    }

    public function isSubordinateFor(User $user): bool {
        $members = $user->membership()->where(['company_id' => $this->company_id])->where('rank', '>', CompanyMemberRank::EMPLOYEE)->get();
        foreach ($members->all() as $member) {
            if ($this->department->isMember($member)) return ($member->rank > $this->rank);
            elseif ($member->department->level < $this->department->level) return $member->department->isParentFor($this->department);
        }
        return false;
    }



    public function setRank($rank): bool {
        if (!empty(CompanyMemberRank::TITLES[$rank])) {
            if (($rank === CompanyMemberRank::CHIEF) && ($chief = $this->department->chief)) $chief->setRank(CompanyMemberRank::EMPLOYEE);
            return $this->update(['rank' => $rank]);
        }
        return false;
    }
}
