File: /home/xedaptot/be.naniguide.com/app/Model/AdAbTest.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Library\Traits\HasUid;
/**
* @property int $id
* @property string $uid
* @property int $ad_campaign_id
* @property string $status
* @property int|null $winner_variant_id
* @property float $confidence
* @property string|null $isolated_variable
* @property float $confidence_threshold
* @property array<string,mixed>|null $settings
* @property \Carbon\Carbon|null $started_at
* @property \Carbon\Carbon|null $completed_at
* @property-read AdCampaign|null $campaign
*/
class AdAbTest extends Model
{
use HasFactory;
use HasUid;
const STATUS_DRAFT = 'draft';
const STATUS_RUNNING = 'running';
const STATUS_COMPLETED = 'completed';
const STATUS_CANCELLED = 'cancelled';
const STATUS_ACTIVE = 'running'; // alias for HasUid trait compatibility
const STATUSES = [self::STATUS_DRAFT, self::STATUS_RUNNING, self::STATUS_COMPLETED, self::STATUS_CANCELLED];
protected $table = 'ad_ab_tests';
protected $fillable = ['uid', 'ad_campaign_id', 'status', 'winner_variant_id', 'confidence', 'isolated_variable', 'confidence_threshold', 'settings', 'started_at', 'completed_at'];
protected $casts = [
'settings' => 'json',
'confidence' => 'decimal:2',
'confidence_threshold' => 'decimal:2',
'started_at' => 'datetime',
'completed_at' => 'datetime',
];
/**
* @return BelongsTo<AdCampaign, $this>
*/
public function campaign(): BelongsTo
{
return $this->belongsTo(AdCampaign::class, 'ad_campaign_id');
}
/**
* @return HasMany<AdAbVariant, $this>
*/
public function variants(): HasMany
{
return $this->hasMany(AdAbVariant::class, 'ad_ab_test_id');
}
/**
* @return HasMany<AdAbTestEvaluationLog, $this>
*/
public function evaluationLogs(): HasMany
{
return $this->hasMany(AdAbTestEvaluationLog::class, 'ad_ab_test_id');
}
public function winner(): BelongsTo
{
return $this->belongsTo(AdAbVariant::class, 'winner_variant_id');
}
public function scopeOfCampaign($query, $campaignId) { return $query->where('ad_campaign_id', $campaignId); }
public function scopeOfCustomer($query, $customerId)
{
return $query->whereIn('ad_campaign_id', function ($q) use ($customerId) {
$q->select('id')->from('ad_campaigns')->where('customer_id', $customerId);
});
}
public function scopeSearch($query, $keyword)
{
if (!$keyword) return $query;
return $query->whereIn('ad_campaign_id', function ($q) use ($keyword) {
$q->select('id')->from('ad_campaigns')->where('name', 'like', '%' . $keyword . '%');
});
}
public function getStatusBadgeAttribute(): string
{
return match ($this->status) {
self::STATUS_RUNNING => 'blue',
self::STATUS_COMPLETED => 'green',
self::STATUS_CANCELLED => 'red',
default => 'orange',
};
}
protected static function newFactory()
{
return \Database\Factories\AdAbTestFactory::new();
}
}