<?php

declare(strict_types=1);

namespace App\Domain\Replication\Models;

use App\Domain\Cluster\Models\ClusterNode;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ReplicationFailoverEvent extends Model
{
    protected $fillable = [
        'replication_setup_id',
        'event_type',
        'previous_master_id',
        'new_master_id',
        'details',
        'automatic',
        'occurred_at',
    ];

    protected function casts(): array
    {
        return [
            'details' => 'array',
            'automatic' => 'boolean',
            'occurred_at' => 'datetime',
        ];
    }

    public function setup(): BelongsTo
    {
        return $this->belongsTo(ReplicationSetup::class, 'replication_setup_id');
    }

    public function previousMaster(): BelongsTo
    {
        return $this->belongsTo(ClusterNode::class, 'previous_master_id');
    }

    public function newMaster(): BelongsTo
    {
        return $this->belongsTo(ClusterNode::class, 'new_master_id');
    }
}
