<?php

namespace App\Models;

use App\Models\BaseModel;

class LoginActivity extends BaseModel
{
    protected $table = 'login_activity';

    // ✅ CI: useTimestamps = true (created_at/updated_at)
    public $timestamps = true;

    protected $fillable = [
        'user_id',
        'email',
        'login_time',
        'logout_time',
        'ip_address',
        'user_agent',
        'created_at',
        'updated_at',
        'semester',
        'school_year',
    ];

    protected $casts = [
        'user_id'     => 'integer',
        'login_time'  => 'datetime',
        'logout_time' => 'datetime',
    ];

    /* Optional relationship */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * Equivalent of CI getLastActivities()
     */
    public static function getLastActivities(int $limit = 4)
    {
        return static::query()
            ->orderByDesc('login_time')
            ->limit(max(1, $limit))
            ->get();
    }
}