<?php

namespace App\Models\Traits\Bootables;

use App\Models\Polymorphs\AuditLog;
use App\Models\StockManagement\Importation;
use Illuminate\Database\Eloquent\Model;

trait Auditable{

    protected function getExceptsModels(){
        return [
            //StockRequest::class,
            Importation::class,
            //Warehouse::class,
            //Adjustment::class
        ];
    }

    public static function bootAuditable(){
        static::created(function (Model $model) {
            //if (!in_array(get_class($model), (new static)->getExceptsModels())){
                $model->addAudit('created');
            //}
        });

        static::updated(function (Model $model) {
            //if (!in_array(get_class($model), (new static)->getExceptsModels())){
                $model->addAudit('updated');
            //}
        });

        static::deleted(function (Model $model) {
            //if (!in_array(get_class($model), (new static)->getExceptsModels())){
                $model->addAudit('deleted');
            //}
        });
    }


    /** Tous les audits liés au model
     * @return mixed
     */
    public function auditLogs(){
        return $this->morphMany(
            AuditLog::class,
            AuditLog::MORPH_FUNCTION_NAME,
            AuditLog::MORPH_TYPE_COLUMN,
            AuditLog::MORPH_ID_COLUMN
        );
    }


    /**Enregistre un nouvel audit sur le model
     * @param $type
     * @param bool $registed_audit
     */
    protected function addAudit($type, $registed_audit = true){
        //l'auteur de l'audit
        $author = auth(activeGuard())->user();  // [User,Customer]
        // $properties = [ "before" => $this->getBeforeValues(), "after" => $this->getAfterValues(), ];
        //if($this instanceof Job){
        //    $properties = [ "before" => $this->getBeforeValues(), "after" => $this->getAfterValues(), ];
        //}

        //si un auteur existe
        if ($registed_audit){
            $this->auditLogs()->create([
                'type'                                   => $type,
                'originals_properties'                   => $this->getOriginal() ?? null,
                'changes_properties'                     => $this->getChanges() ?? null,
                'host'                                   => request()->ip() ?? null,
                'agent'                                  => request()->userAgent() ?? null,
                AuditLog::MORPH_AUTHOR_ID_COLUMN         => (filled($author)) ? $author->getKey() : null,
                AuditLog::MORPH_AUTHOR_TYPE_COLUMN       => (filled($author)) ? $author->getMorphClass() : null,
            ]);

            //if ($description == "created"){}
        }
    }

    public function getAuthorAttribute(){
        $author = $this->auditLogs()
                    ->firstWhere("description","created")
                    ->{AuditLog::MORPH_AUTHOR_FUNCTION_NAME} ?? null;

        if ($author){
            $type = $author->getMorphClass();
            $author = $author->only($author->getKeyName(),"fullName");
            $author['type'] = $type;
        }

        return $author;
    }



}
