<?php

namespace MoneyTransfer\AuditLog\Models;

use App\Models\User;
use MoneyTransfer\AuditLog\Enums\DataRetentionPeriod;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\MassPrunable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AuditHistory extends Model
{
    use MassPrunable;

    protected $table = 'audit_histories';

    protected $fillable = [
        'user_agent',
        'ip_address',
        'module',
        'action',
        'user_id',
        'reference_user',
        'reference_id',
        'reference_name',
        'type',
        'request',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class)->select('id', 'name')->withDefault();
    }

    public function prunable()
    {
        $days = DataRetentionPeriod::ONE_MONTH;

       /* if ($days === DataRetentionPeriod::NEVER) {
            return $this->query()->where('id', '<', 0);
        } */

        return $this->query()->where('created_at', '<', Carbon::now()->subDays($days));
    }
}
