<?php

namespace App\Models;

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

class ContactActivity extends Model
{
    protected $table = 'contact_activities';

    protected $fillable = ['contact_id', 'type', 'description', 'occurred_at'];

    protected $casts = [
        'occurred_at' => 'datetime',
    ];

    protected static function booted(): void
    {
        static::created(function (ContactActivity $activity) {
            try {
                \App\Services\AutomationEngine::check($activity);
            } catch (\Throwable $e) {
                \Illuminate\Support\Facades\Log::error("Workflow trigger failed: " . $e->getMessage());
            }
        });
    }

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