<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class TopspenderCharacterCache extends Model
{
    protected $fillable = [
        'user_id',
        'month',
        'year',
        'character_name',
        'character_class',
    ];

    /**
     * Get cached character data for a user in a specific month/year.
     */
    public static function getCachedCharacter(int $userId, int $month, int $year): ?self
    {
        return self::where('user_id', $userId)
            ->where('month', $month)
            ->where('year', $year)
            ->first();
    }

    /**
     * Store or update cached character data.
     */
    public static function storeCache(int $userId, int $month, int $year, string $characterName, ?int $characterClass): self
    {
        return self::updateOrCreate(
            [
                'user_id' => $userId,
                'month' => $month,
                'year' => $year,
            ],
            [
                'character_name' => $characterName,
                'character_class' => $characterClass,
            ]
        );
    }
}
