<?php

namespace Licenzo\Services\Logging\Concerns;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

use Licenzo\Framework\Database\Model;

/**
 * Trait DetectsRelations
 * 
 * Automatically detects related model types for polymorphic relationships
 */
trait DetectsRelations
{
    /**
     * Detect related type from model class
     *
     * @param Model $model
     * @return string
     */
    protected static function detectRelatedType(Model $model): string
    {
        return match (get_class($model)) {
            \Licenzo\Model\SubscriptionTransaction::class => 'transaction',
            \Licenzo\Model\WebhookLog::class => 'webhook',
            \Licenzo\Model\License::class => 'license',
            \Licenzo\Model\SubscriptionSchedule::class => 'schedule',
            \Licenzo\Model\LicenseActivation::class => 'activation',
            \Licenzo\Model\Product::class => 'product',
            \Licenzo\Model\ProductVariation::class => 'variation',
            default => strtolower(class_basename($model))
        };
    }

    /**
     * Extract related model data
     *
     * @param Model|null $model
     * @return array|null
     */
    protected static function extractRelatedData(?Model $model): ?array
    {
        if (!$model) {
            return null;
        }

        return [
            'id' => $model->id,
            'type' => static::detectRelatedType($model),
        ];
    }
}

