<?php

namespace App\Models\Polymorphs;

use App\Models\Traits\HasComments;
use App\Models\Traits\HasDates;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class AuditLog extends Model{
    use HasFactory,SoftDeletes,
        HasComments,HasDates;

    protected $guarded = ['id','created_at'];
    protected $casts = [
        'originals_properties' => 'collection',
        'changes_properties' => 'collection',
    ];

    protected $table= "audit_logs";

    const MORPH_ID_COLUMN = "auditable_id";
    const MORPH_TYPE_COLUMN = "auditable_type";
    const MORPH_FUNCTION_NAME = "auditable";

    const MORPH_AUTHOR_ID_COLUMN = "author_id";
    const MORPH_AUTHOR_TYPE_COLUMN = "author_type";
    const MORPH_AUTHOR_FUNCTION_NAME = "authorable";
    const FK_ID = "audit_id";


     //Relations
    /**Le model audité
     * @return mixed
     */
    public function auditable(){
        return $this->morphTo(__FUNCTION__,
            self::MORPH_TYPE_COLUMN,self::MORPH_ID_COLUMN)
            ->withDefault("author");
    }

    /**L'auteur de l'audit
     * @return mixed
     */
    public function authorable(){
        return $this->morphTo(__FUNCTION__,
            self::MORPH_AUTHOR_TYPE_COLUMN,self::MORPH_AUTHOR_ID_COLUMN);
    }

//    /**L'auteur de l'audit
//     * @return mixed
//     */
//    public function author(){
//        return $this->morphOne(self::class, self::MORPH_AUTHOR_FUNCTION_NAME,
//            self::MORPH_AUTHOR_TYPE_COLUMN,self::MORPH_AUTHOR_ID_COLUMN);
//    }

}
