<?php

declare(strict_types=1);

namespace ImovelZapAi\Conversations\Infrastructure\Persistence\Eloquent;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use ImovelZapAi\Conversations\Domain\Enums\HandoffStatus;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\BelongsToTenant;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\HasUuidPrimaryKey;
use ImovelZapAi\Users\Infrastructure\Persistence\Eloquent\UserModel;

class HumanHandoffModel extends Model
{
    use BelongsToTenant;
    use HasUuidPrimaryKey;

    protected $table = 'human_handoffs';

    protected $fillable = [
        'tenant_id',
        'contact_id',
        'conversation_id',
        'conversation_summary_id',
        'status',
        'priority_score',
        'assigned_user_id',
        'queued_at',
        'claimed_at',
        'completed_at',
    ];

    protected function casts(): array
    {
        return [
            'status' => HandoffStatus::class,
            'queued_at' => 'datetime',
            'claimed_at' => 'datetime',
            'completed_at' => 'datetime',
        ];
    }

    public function contact(): BelongsTo
    {
        return $this->belongsTo(ContactModel::class, 'contact_id');
    }

    public function conversation(): BelongsTo
    {
        return $this->belongsTo(ConversationModel::class, 'conversation_id');
    }

    public function summary(): BelongsTo
    {
        return $this->belongsTo(ConversationSummaryModel::class, 'conversation_summary_id');
    }

    public function assignedUser(): BelongsTo
    {
        return $this->belongsTo(UserModel::class, 'assigned_user_id');
    }

    public function leadScore(): HasOne
    {
        return $this->hasOne(LeadScoreModel::class, 'human_handoff_id');
    }
}
