<?php

namespace App\Models;

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

class DriverReferralCode extends Model
{
    protected $fillable = ['driver_id', 'code'];

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

    public static function generateUnique(): string
    {
        do {
            $code = 'DRV' . strtoupper(substr(uniqid(), -6));
        } while (self::where('code', $code)->exists());
        return $code;
    }
}
