<?php

namespace App\Models;

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

class SmsCount extends Model
{
    public const TYPE_PHONE = 1;
    public const TYPE_API = 2;

    protected $fillable = [
        'user_id',
        'month',
        'operator_name',
        'operator_number',
        'sim_slot',
        'type',
        'total_request',
        'total_sent',
        'total_failed',
        'last_update',
    ];

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

    /**
     * @param Builder<SmsCount> $query
     * @return Builder<SmsCount>
     */
    public function scopeForMonth(Builder $query, string $month): Builder
    {
        return $query->where('month', $month);
    }

    /**
     * @param Builder<SmsCount> $query
     * @return Builder<SmsCount>
     */
    public function scopeCurrentMonth(Builder $query): Builder
    {
        return $query->where('month', Carbon::now()->format('Y-m'));
    }

    protected $casts = [
        'sim_slot' => 'integer',
        'type' => 'integer',
        'total_request' => 'integer',
        'total_sent' => 'integer',
        'total_failed' => 'integer',
        'last_update' => 'datetime',
    ];
}
