<?php

namespace PloverCRM\Modules\Automations\Services;

/**
 * Serializes automation records into app-facing response shapes.
 */
class AutomationApiSerializer
{
    public static function summary(array $automation): array
    {
        $definition = is_array($automation['definition_json'] ?? null)
            ? $automation['definition_json']
            : [];
        $firstAction = is_array($definition['actions'][0] ?? null)
            ? $definition['actions'][0]
            : [];
        $meta = is_array($definition['meta'] ?? null) ? $definition['meta'] : [];

        return [
            'id' => (int) ($automation['id'] ?? 0),
            'name' => (string) ($automation['name'] ?? ''),
            'description' => (string) ($automation['description'] ?? ''),
            'status' => (string) ($automation['status'] ?? 'draft'),
            'builderMode' => (string) ($automation['builder_mode'] ?? 'guided'),
            'triggerEvent' => (string) ($automation['trigger_event'] ?? ''),
            'actionType' => (string) ($firstAction['type'] ?? ''),
            'templateKey' => (string) ($meta['template_key'] ?? ''),
            'templateName' => (string) ($meta['template_name'] ?? ''),
            'templateCategory' => (string) ($meta['template_category'] ?? ''),
            'lastRunAt' => $automation['last_run_at'] ?? null,
            'updatedAt' => $automation['updated_at'] ?? null,
            'createdAt' => $automation['created_at'] ?? null,
        ];
    }

    public static function detail(array $automation, array $recentRuns = []): array
    {
        return array_merge(self::summary($automation), [
            'definition' => is_array($automation['definition_json'] ?? null)
                ? $automation['definition_json']
                : [],
            'recentRuns' => array_map([self::class, 'run'], $recentRuns),
        ]);
    }

    public static function run(array $run): array
    {
        return [
            'id' => (int) ($run['id'] ?? 0),
            'automationId' => (int) ($run['automation_id'] ?? 0),
            'eventName' => (string) ($run['event_name'] ?? ''),
            'contactId' => isset($run['contact_id']) ? (int) $run['contact_id'] : null,
            'status' => (string) ($run['status'] ?? ''),
            'payload' => is_array($run['payload_json'] ?? null) ? $run['payload_json'] : [],
            'result' => is_array($run['result_json'] ?? null) ? $run['result_json'] : [],
            'errorMessage' => (string) ($run['error_message'] ?? ''),
            'startedAt' => $run['started_at'] ?? null,
            'finishedAt' => $run['finished_at'] ?? null,
        ];
    }

    public static function template(array $template): array
    {
        return [
            'key' => (string) ($template['key'] ?? ''),
            'name' => (string) ($template['name'] ?? ''),
            'category' => (string) ($template['category'] ?? 'General'),
            'description' => (string) ($template['description'] ?? ''),
            'builderMode' => (string) ($template['builder_mode'] ?? 'guided'),
            'definition' => is_array($template['definition'] ?? null)
                ? $template['definition']
                : [],
        ];
    }
}
