<?php

namespace App\Models\System;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Traits\TenantScoped;
use App\Models\System\User;

class SystemOperationLog extends Model
{
    use HasFactory, TenantScoped;
    
    protected $appends = [
        'translated_note',
    ];

    /**
     * 動態翻譯備註
     */
    public function getTranslatedNoteAttribute(): ?string
    {
        if (empty($this->note)) {
            return null;
        }

        return __($this->note, array_merge(
            $this->old_values ?? [],
            $this->new_values ?? []
        ));
    }


    protected $fillable = [
        'company_id',
        'user_id',
        'module',
        'action',
        'target_id',
        'target_type',
        'old_values',
        'new_values',
        'note',
        'ip_address',
        'user_agent',
    ];

    protected $casts = [
        'old_values' => 'array',
        'new_values' => 'array',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
