<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Log extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'ip',
        'email',
    ];

    public static function deleteLogsFromDB() {
        $cutoff = Carbon::now('UTC')->subMonths(3)->toDateTimeString();
        $count = count(self::where('created_at', '<', $cutoff)
            ->orderBy('created_at', 'desc')
            ->get());

        if ($count > 0) {
            self::where('created_at', '<', $cutoff)->delete();
            return "$count old log(s) deleted from the database.";
        }
        return "No logs older than 3 months found.";
    }
}
