<?php

namespace App\Models\Loyalty;

use App\Models\Concerns\Auditable;
use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class LoyaltyTier extends Model
{
    use Auditable, BelongsToTenant;

    protected $fillable = [
        'uuid', 'program_id', 'name', 'code', 'min_lifetime_points',
        'earn_multiplier', 'color', 'sort_order', 'created_by', 'updated_by',
    ];

    protected function casts(): array
    {
        return [
            'min_lifetime_points' => 'decimal:2',
            'earn_multiplier' => 'decimal:4',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (self $tier): void {
            if (! $tier->uuid) {
                $tier->uuid = (string) Str::ulid();
            }
        });
    }

    public function program(): BelongsTo
    {
        return $this->belongsTo(LoyaltyProgram::class, 'program_id');
    }
}
