<?php

namespace Functional\Streaks\Models;

use Functional\Streaks\Database\Factories\AchievementFactory;
use Functional\Users\Models\User;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Badge débloqué par un user à un palier donné (ex: streak_7, streak_30, first_workout).
 * Unique par (user, key) : un badge n'est débloqué qu'une seule fois.
 *
 * @property int $user_id
 * @property string $key
 * @property \Illuminate\Support\Carbon $earned_at
 * @property array|null $meta
 */
#[UseFactory(AchievementFactory::class)]
class Achievement extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id', 'key', 'earned_at', 'meta',
    ];

    protected $casts = [
        'earned_at' => 'datetime',
        'meta' => 'array',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}
