<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Scopes\OrganisationScope;
use Illuminate\Support\Facades\Auth;

class Competencies extends Model
{
    protected $fillable = [
        'name',
        'type',
        'created_by',
        'organisation_id',

    ];

    // public static $types = [
    //     'technical' => 'Technical',
    //     'organizational' => 'Organizational',
    //     'behavioural' => 'Behavioural',
    // ];

    public function getPerformance_type()
    {
        return $this->hasOne('App\Models\Performance_Type', 'id', 'type');
    }

    public function appraisalReviews()
    {
        return $this->belongsToMany(AppraisalReview::class, 'appraisal_review_competency')
            ->withPivot(['weight', 'rating', 'comment', 'organisation_id'])
            ->withTimestamps();
    }

    protected static function booted()
    {
        static::addGlobalScope(new OrganisationScope);

        // Automatically set the organisation_id before creating the model
        static::creating(function ($model) {
            if (Auth::check()) {
                $model->organisation_id = Auth::user()->organisation_id;
            }
        });
    }
}
