<?php

namespace App\Models;

use App\Models\BaseModel;

class Stats extends BaseModel
{
    protected $table = 'stats';

    /**
     * If your stats table has created_at/updated_at, keep true (default).
     * If it doesn't, set to false.
     */
    public $timestamps = true;

    protected $fillable = [
        'students',
        'teachers',
        'admins',
        'users',
    ];

    protected $casts = [
        'students' => 'integer',
        'teachers' => 'integer',
        'admins'   => 'integer',
        'users'    => 'integer',
    ];

    public function getFillable(): array
    {
        return $this->fillable;
    }

    /**
     * CI-compatible: getStats()
     * Returns all rows.
     */
    public static function getStats()
    {
        return static::query()->get();
    }

    /**
     * Enhancement: if you treat stats as a single row (common),
     * this returns the first row, creating it with zeros if missing.
     */
    public static function singleton(): self
    {
        return static::query()->first() ?? static::query()->create([
            'students' => 0,
            'teachers' => 0,
            'admins'   => 0,
            'users'    => 0,
        ]);
    }

    /**
     * Enhancement: update singleton stats safely.
     */
    public static function updateSingleton(array $data): self
    {
        $row = static::singleton();
        $row->fill($data);
        $row->save();
        return $row;
    }
}
