<?php

namespace App\Models\Communication;

use App\Models\Solicitor\Solicitor;
use App\Models\User\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Voicemail extends Model
{
    protected $fillable = [
        'communication_id', 'call_id', 'solicitor_id', 'assigned_agent_id',
        'provider', 'provider_recording_sid', 'from_number', 'to_number',
        'recording_url', 'duration', 'is_listened', 'callback_requested',
        'notes', 'received_at', 'meta',
    ];

    protected $casts = [
        'is_listened'        => 'boolean',
        'callback_requested' => 'boolean',
        'received_at'        => 'datetime',
        'meta'               => 'array',
    ];

    public function communication(): BelongsTo
    {
        return $this->belongsTo(Communication::class);
    }

    public function assignedAgent(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_agent_id');
    }

    public function solicitor(): BelongsTo
    {
        return $this->belongsTo(Solicitor::class);
    }

    /**
     * The audio recording matched by provider_recording_sid (both rows are
     * created in the same transaction by HandleRecordingStatusCallbackAction).
     */
    public function recording(): HasOne
    {
        return $this->hasOne(Recording::class, 'provider_recording_sid', 'provider_recording_sid');
    }
}
