<?php

namespace App\Domain\Subscription;

use App\Models\ActivityLog;
use Illuminate\Database\Eloquent\Model;

/**
 * Central billing/SaaS activity logger. Every billing action funnels through
 * here so the platform has one append-only audit stream (distinct from the
 * payment-proof review trail, which is per-payment).
 */
class ActivityLogger
{
    public function log(
        string $event,
        ?string $description = null,
        ?Model $subject = null,
        ?int $tenantId = null,
        ?int $userId = null,
        array $properties = [],
        ?string $ip = null,
    ): ActivityLog {
        return ActivityLog::create([
            'tenant_id' => $tenantId ?? optional($subject)->tenant_id,
            'user_id' => $userId ?? auth()->id(),
            'event' => $event,
            'description' => $description,
            'subject_type' => $subject ? $subject->getMorphClass() : null,
            'subject_id' => $subject?->getKey(),
            'properties' => $properties ?: null,
            'ip' => $ip ?? request()->ip(),
        ]);
    }
}
