<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class SmsMessage extends Model
{
    protected $fillable = [
        'user_id',
        'device_id',
        'recipient',
        'message',
        'sms_type',
        'sms_parts',
        'batch_id',
        'schedule_at',
        'status',
        'attempts',
        'claimed_at',
        'sent_at',
        'failed_at',
        'last_error',
    ];

    public const STATUS_PENDING = 'pending';
    public const STATUS_PROCESSING = 'processing';
    public const STATUS_SENT = 'sent';
    public const STATUS_FAILED = 'failed';

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

    /**
     * @return BelongsTo<UserDevice, $this>
     */
    public function device(): BelongsTo
    {
        return $this->belongsTo(UserDevice::class, 'device_id');
    }

    protected function casts(): array
    {
        return [
            'attempts' => 'integer',
            'sms_parts' => 'integer',
            'claimed_at' => 'datetime',
            'sent_at' => 'datetime',
            'failed_at' => 'datetime',
            'schedule_at' => 'datetime',
        ];
    }
}
