<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;

class SMSActivity extends Model
{
    use HasFactory;

    protected $table = 'sms_activities';

    protected $appends = ['status', 'time'];

    protected $fillable = [
        'user_id',
        'number_id',
        'message_id',
        'sender_id',
        'rate',
    ];
    public function validate($input)
    {
        $validate = Validator::make($input, [
            'user_id' => 'required',
            'number_id' => 'required',
            'message_id' => 'required',
            'rate' => 'required',
            'sender_id' => 'required',
        ]);
        return $validate;
    }
    public function getDataAttribute($value)
    {
        return json_decode($value, true);
    }
    protected $casts = [
        'created_at' => 'datetime',
    ];
    public function getCreatedAtAttribute($value)
    {
        $date = \Carbon\Carbon::parse($value)->addHours(6);
        return $date->format('d M Y ( H:i:s )');
    }


    public function getStatusAttribute()
    {
        $support = new \App\Helpers\TraitSupport();
        return $support->responseErrors($this->api_status)->message;
    }
    public function getTimeAttribute()
    {
        return date('d-M-Y H:m a');
    }

    public function message()
    {
        return $this->belongsTo(Message::class, 'message_id', 'id')
            ->selectRaw("id,body,ip_address, CONCAT(SUBSTRING(body, 1, 50), '..') as body, body as full_message,sms_count");
    }
    public function sender_id()
    {
        return $this->belongsTo(SenderIdRequest::class, 'sender_id', 'id')->selectRaw("id,cli as title");
    }
    public function sender()
    {
        return $this->belongsTo(SenderIdRequest::class, 'sender_id', 'id')->selectRaw("id,title,sender_id_type");
    }
    public function numbers()
    {
        return $this->belongsTo(Number::class, 'number_id')->selectRaw("id,phone,type");
    }
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id')->selectRaw('id,name');
    }

    public function message_apis(){
        return $this->belongsTo(SenderIdRequest::class, 'sender_id', 'id');
    }

}



