<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class AffiliateReferral extends Model
{
    protected $keyType = 'string';
    public $incrementing = false;

    protected $fillable = [
        'affiliate_id',
        'referred_user_id',
        'affiliate_code',
        'status',
        'registered_at',
        'qualifies_at',
        'paid_at',
        'credits_awarded',
    ];

    protected $casts = [
        'registered_at' => 'datetime',
        'qualifies_at'  => 'datetime',
        'paid_at'       => 'datetime',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if (!$model->id) {
                $model->id = (string) Str::uuid();
            }
        });
    }

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

    public function referredUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'referred_user_id');
    }
}
