<?php

namespace App\Domain\Experiment\Models;

use App\Domain\Agent\Models\Agent;
use App\Domain\Crew\Models\Crew;
use App\Domain\Experiment\Enums\ExecutionMode;
use App\Domain\Skill\Models\Skill;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;

/**
 * @property string $id
 * @property string $experiment_id
 * @property string|null $agent_id
 * @property string|null $skill_id
 * @property string|null $crew_id
 * @property string|null $workflow_node_id
 * @property int $order
 * @property ExecutionMode|null $execution_mode
 * @property string|null $group_id
 * @property array<string, mixed>|null $conditions
 * @property array<string, mixed>|null $input_mapping
 * @property array<string, mixed>|null $output
 * @property array<string, mixed>|null $checkpoint_data
 * @property string $status
 * @property int|null $duration_ms
 * @property int|null $cost_credits
 * @property int|null $loop_count
 * @property string|null $error_message
 * @property array<string, mixed>|null $error_metadata
 * @property Carbon|null $started_at
 * @property Carbon|null $completed_at
 * @property Carbon|null $last_heartbeat_at
 * @property string|null $worker_id
 * @property string|null $idempotency_key
 * @property int $checkpoint_version
 * @property array<string, mixed>|null $guardrail_result
 * @property Carbon|null $resume_at
 * @property string|null $root_event_id
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 */
class PlaybookStep extends Model
{
    use HasUuids;

    protected $fillable = [
        'experiment_id',
        'agent_id',
        'skill_id',
        'crew_id',
        'workflow_node_id',
        'order',
        'execution_mode',
        'group_id',
        'conditions',
        'input_mapping',
        'output',
        'checkpoint_data',
        'status',
        'duration_ms',
        'cost_credits',
        'loop_count',
        'error_message',
        'error_metadata',
        'started_at',
        'completed_at',
        'last_heartbeat_at',
        'worker_id',
        'idempotency_key',
        'checkpoint_version',
        'guardrail_result',
        'resume_at',
        'root_event_id',
    ];

    protected function casts(): array
    {
        return [
            'execution_mode' => ExecutionMode::class,
            'conditions' => 'array',
            'input_mapping' => 'array',
            'output' => 'array',
            'checkpoint_data' => 'array',
            'duration_ms' => 'integer',
            'cost_credits' => 'integer',
            'checkpoint_version' => 'integer',
            'guardrail_result' => 'array',
            'started_at' => 'datetime',
            'completed_at' => 'datetime',
            'last_heartbeat_at' => 'datetime',
            'resume_at' => 'datetime',
            'error_metadata' => 'array',
        ];
    }

    /** @return BelongsTo<Experiment, $this> */
    public function experiment(): BelongsTo
    {
        return $this->belongsTo(Experiment::class);
    }

    /** @return BelongsTo<Agent, $this> */
    public function agent(): BelongsTo
    {
        return $this->belongsTo(Agent::class);
    }

    /** @return BelongsTo<Skill, $this> */
    public function skill(): BelongsTo
    {
        return $this->belongsTo(Skill::class);
    }

    /** @return BelongsTo<Crew, $this> */
    public function crew(): BelongsTo
    {
        return $this->belongsTo(Crew::class);
    }

    public function isPending(): bool
    {
        return $this->status === 'pending';
    }

    public function isCompleted(): bool
    {
        return $this->status === 'completed';
    }

    public function isFailed(): bool
    {
        return $this->status === 'failed';
    }

    public function isRunning(): bool
    {
        return $this->status === 'running';
    }

    public function isSkipped(): bool
    {
        return $this->status === 'skipped';
    }

    public function isWaitingHuman(): bool
    {
        return $this->status === 'waiting_human';
    }

    public function isWaitingTime(): bool
    {
        return $this->status === 'waiting_time';
    }

    public function isTerminal(): bool
    {
        return in_array($this->status, ['completed', 'failed', 'skipped']);
    }

    public function hasCheckpoint(): bool
    {
        return ! empty($this->checkpoint_data);
    }
}
