<?php

namespace Modules\MembershipApplication\Entities;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Modules\Auth\Entities\User;
use Modules\MembershipApplication\Enums\BoardDecision;
use Modules\MembershipApplication\Enums\ManagementDecision;
use Modules\MembershipApplication\Enums\ReviewStage;

class MembershipApplicationReview extends Model
{
    use HasUuids;

    protected $table = 'membership_application_reviews';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'membership_application_id',
        'stage',
        'decision',
        'remarks',
        'reviewer_id',
        'reviewed_at',
    ];

    /**
     * @var list<string>
     */
    protected $hidden = [
    ];

    protected function casts(): array
    {
        return [
            'stage' => ReviewStage::class,
            'reviewed_at' => 'datetime',
        ];
    }

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

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

    public function managementDecision(): ?ManagementDecision
    {
        if ($this->stage !== ReviewStage::Management) {
            return null;
        }

        return ManagementDecision::tryFrom($this->decision);
    }

    public function boardDecision(): ?BoardDecision
    {
        if ($this->stage !== ReviewStage::Board) {
            return null;
        }

        return BoardDecision::tryFrom($this->decision);
    }
}
