<?php

namespace App\Modules\Workflow;

use App\Models\ValidationModel;

class ScheduledEmail extends ValidationModel
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'scheduled_emails';

    /**
     * The attributes that won't be assigned during
     * a mass assignment.
     *
     * @var array
     */
    protected $fillable = ['event', 'to', 'settings', 'sent', 'automatic', 'failed', 'task_id', 'client_id', 'client_type', 'send_on'];

    /**
     * Array of validation rules.
     *
     * @var array
     */
    protected static $rules = [
        'event'   => ['required'],
        'to'      => ['required'],
        'send_on' => ['required'],
    ];

    /**
     * Encodes the setting array to a json string
     * in order to store it in the database.
     *
     * @param  array $value
     *
     * @return null
     */
    public function setSettingsAttribute($value)
    {
        $this->attributes['settings'] = json_encode($value);
    }

    /**
     * Decodes the settings attribute from a json format to
     * an array.
     *
     * @return array
     */
    public function getSettingsAttribute()
    {
        return json_decode($this->attributes['settings'], true);
    }
}
