<?php

namespace App\Modules\Invoices;

use Illuminate\Support\Facades\Auth;
use App\Models\ValidationModel;
use App\Modules\Invoices\Invoice;
use App\Models\User;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['invoice_id', 'sent_to', 'subject', 'message', 'created_by'];

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

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    public static function boot()
    {
        parent::boot();

        // When a transaction is created we want to
        // update the created_by field
        static::creating(function ($model) {
            $model->created_by = Auth::user()->id;
        });
    }

    /**
     * Links to an invoice, by using the foreign key.
     *
     * @return void
     */
    public function invoice()
    {
        return $this->belongsTo(Invoice::class);
    }

    /**
     *	Defines a relationship on the users table.
     *	Retrieves the person who created the transaction.
     */
    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }
}
