<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;
use Throwable;

class BonusClaimLog extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    protected $casts = [
        'user_id' => 'integer',
        'review_id' => 'integer',
        'bonus_amount' => 'float',
        'bonus_points' => 'integer',
        'granted_status' => 'integer',
        'context' => 'array',
        'attempted_at' => 'datetime',
        'granted_at' => 'datetime',
    ];

    public static function safeCreate(array $attributes): void
    {
        try {
            if (! Schema::hasTable((new static())->getTable())) {
                return;
            }

            static::create($attributes);
        } catch (Throwable $exception) {
            report($exception);
        }
    }

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