<?php

namespace App\Domain\Training\Models;

use App\Domain\Participant\Models\Participant;
use App\Domain\Shared\Traits\Auditable;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use App\Domain\Training\Enums\EvaluationStatus;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Domain\Shared\Traits\BelongsToBranch;

class Evaluation extends Model
{
    use BelongsToAcademy, HasUuid, SoftDeletes, Auditable, BelongsToBranch;

    protected $fillable = [
        'academy_id', 'branch_id',
        'participant_id',
        'training_group_id',
        'evaluator_id',
        'evaluation_date',
        'period_from',
        'period_to',
        'overall_score',
        'overall_notes',
        'status',
        'approved_by',
        'approved_at',
        'shared_at',
        'metadata',
    ];

    protected $casts = [
        'status' => EvaluationStatus::class,
        'evaluation_date' => 'date',
        'period_from' => 'date',
        'period_to' => 'date',
        'overall_score' => 'decimal:2',
        'approved_at' => 'datetime',
        'shared_at' => 'datetime',
        'metadata' => 'array',
    ];

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

    public function group(): BelongsTo
    {
        return $this->belongsTo(TrainingGroup::class, 'training_group_id');
    }

    public function evaluator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'evaluator_id');
    }

    public function approver(): BelongsTo
    {
        return $this->belongsTo(User::class, 'approved_by');
    }

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