<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ActivationKey extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'activation_key',
        'price_id',
        'is_activated',
    ];

    protected $casts = [
        'is_activated' => 'boolean',
    ];

    /**
     * Relationship: the user who redeemed the activation key (optional).
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Scope to filter unactivated keys
     */
    #[Scope]
    protected function unactivated($query)
    {
        return $query->where('is_activated', false);
    }

    /**
     * Scope to filter activated keys
     */
    #[Scope]
    protected function activated($query)
    {
        return $query->where('is_activated', true);
    }
}
