<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class WorkflowTransition extends Model
{
    protected $fillable = ['workflow_id', 'created_by', 'from_step_id', 'to_step_id', 'name', 'role_ids', 'user_ids', 'require_comments', 'require_file', 'is_end'];

    protected $casts = [
        'role_ids' => 'array',
        'user_ids' => 'array',
    ];

    public function createdBy()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function workflow()
    {
        return $this->belongsTo(Workflow::class);
    }

    public function fromStep()
    {
        return $this->belongsTo(WorkflowStep::class, 'from_step_id');
    }

    public function toStep()
    {
        return $this->belongsTo(WorkflowStep::class, 'to_step_id');
    }

        // Accessor to get assigned user models
    public function getUsersAttribute()
    {
        if (empty($this->user_ids) || !is_array($this->user_ids)) {
            return collect();
        }
        return User::whereIn('id', $this->user_ids)->get();
    }
}
