<?php

namespace App\Models;

use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class VisitStageAdvance extends Model
{
    use BelongsToOwner;

    public const STATUS_ACTIVE = 'active';

    public const STATUS_BLOCKED = 'blocked';

    public const STATUS_COMPLETED = 'completed';

    public const STATUS_SKIPPED = 'skipped';

    protected $table = 'care_visit_stage_advances';

    protected $fillable = [
        'owner_ref', 'visit_id', 'workflow_id', 'workflow_stage_id', 'stage_code',
        'status', 'blocked_reason', 'entered_at', 'cleared_at', 'completed_at', 'meta',
    ];

    protected function casts(): array
    {
        return [
            'entered_at' => 'datetime',
            'cleared_at' => 'datetime',
            'completed_at' => 'datetime',
            'meta' => 'array',
        ];
    }

    public function visit(): BelongsTo
    {
        return $this->belongsTo(Visit::class, 'visit_id');
    }

    public function workflow(): BelongsTo
    {
        return $this->belongsTo(FacilityWorkflow::class, 'workflow_id');
    }

    public function stage(): BelongsTo
    {
        return $this->belongsTo(WorkflowStage::class, 'workflow_stage_id');
    }

    public function isBlocked(): bool
    {
        return $this->status === self::STATUS_BLOCKED;
    }
}
