File: /home/xedaptot/ai.naniguide.com/app/Model/AdAutomationRule.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Library\Traits\HasUid;
class AdAutomationRule extends Model
{
use HasFactory;
use HasUid;
const STATUS_ACTIVE = 'active'; // for HasUid trait compatibility
const TRIGGER_EMAIL_OPEN = 'email_open';
const TRIGGER_EMAIL_CLICK = 'email_click';
const TRIGGER_EMAIL_UNSUBSCRIBE = 'email_unsubscribe';
const TRIGGER_PURCHASE = 'purchase';
const TRIGGERS = [self::TRIGGER_EMAIL_OPEN, self::TRIGGER_EMAIL_CLICK, self::TRIGGER_EMAIL_UNSUBSCRIBE, self::TRIGGER_PURCHASE];
const ACTION_ADD_TO_AUDIENCE = 'add_to_audience';
const ACTION_REMOVE_FROM_AUDIENCE = 'remove_from_audience';
const ACTION_START_CAMPAIGN = 'start_campaign';
const ACTION_PAUSE_CAMPAIGN = 'pause_campaign';
const ACTIONS = [self::ACTION_ADD_TO_AUDIENCE, self::ACTION_REMOVE_FROM_AUDIENCE, self::ACTION_START_CAMPAIGN, self::ACTION_PAUSE_CAMPAIGN];
protected $table = 'ad_automation_rules';
protected $fillable = ['uid', 'customer_id', 'name', 'trigger_event', 'action_type', 'target_id', 'status', 'conditions', 'webhook_secret', 'executions_count', 'last_triggered_at', 'is_running', 'dry_run'];
protected $casts = [
'conditions' => 'json',
'last_triggered_at' => 'datetime',
'webhook_secret' => 'encrypted',
'is_running' => 'boolean',
'dry_run' => 'boolean',
];
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class, 'customer_id');
}
public function scopeOfCustomer($query, $customerId)
{
return $query->where('customer_id', $customerId);
}
public function scopeActive($query)
{
return $query->where('status', 'active');
}
public function scopeSearch($query, $keyword)
{
return $keyword ? $query->where('name', 'like', '%' . $keyword . '%') : $query;
}
public function getStatusBadgeAttribute(): string
{
return match ($this->status) {
'active' => 'green',
'paused' => 'orange',
'disabled' => 'red',
default => 'blue',
};
}
public function getTriggerLabelAttribute(): string
{
return ucwords(str_replace('_', ' ', $this->trigger_event));
}
public function getActionLabelAttribute(): string
{
return ucwords(str_replace('_', ' ', $this->action_type));
}
protected static function newFactory()
{
return \Database\Factories\AdAutomationRuleFactory::new();
}
}