<?php

namespace App\Models;

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

class PointTransaction extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'type',
        'points',
        'description',
        'source',
        'referral_id',
        'metadata',
    ];

    protected $casts = [
        'metadata' => 'array',
    ];

    /**
     * المستخدم المالك للمعاملة
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * الإحالة المرتبطة (إن وجدت)
     */
    public function referral()
    {
        return $this->belongsTo(Referral::class);
    }

    /**
     * إنشاء معاملة جديدة
     */
    public static function createTransaction($userId, $type, $points, $description, $source = null, $referralId = null, $metadata = null)
    {
        return self::create([
            'user_id' => $userId,
            'type' => $type,
            'points' => $points,
            'description' => $description,
            'source' => $source,
            'referral_id' => $referralId,
            'metadata' => $metadata,
        ]);
    }
}
