<?php

declare(strict_types=1);

namespace App\Application\GraphQL;

use App\Framework\GraphQL\Attributes\GraphQLField;
use App\Framework\GraphQL\Attributes\GraphQLSubscription;
use App\Framework\GraphQL\Execution\ExecutionContext;

/**
 * GraphQL Subscriptions for real-time notifications
 *
 * Example usage from client:
 *
 * subscription {
 *   notificationReceived(userId: "123") {
 *     id
 *     message
 *     type
 *     createdAt
 *   }
 * }
 */
#[GraphQLSubscription(description: 'Real-time notification subscriptions')]
final readonly class NotificationSubscriptions
{
    /**
     * Subscribe to notifications for a specific user
     *
     * Resolver returns the notification data when published by the server
     * The actual publishing happens via SubscriptionManager::publish()
     */
    #[GraphQLField(description: 'Receive real-time notifications for a user')]
    public function notificationReceived(string $userId, ExecutionContext $context): array
    {
        // The payload is passed from SubscriptionManager::publish()
        // This resolver just formats the data for the GraphQL response
        $notification = $context->root;

        return [
            'id' => $notification['id'],
            'message' => $notification['message'],
            'type' => $notification['type'],
            'createdAt' => $notification['createdAt'],
        ];
    }

    /**
     * Subscribe to status updates for an order
     */
    #[GraphQLField(description: 'Receive real-time order status updates')]
    public function orderStatusChanged(string $orderId, ExecutionContext $context): array
    {
        $payload = $context->root;

        return [
            'orderId' => $payload['orderId'],
            'status' => $payload['status'],
            'updatedAt' => $payload['updatedAt'],
            'estimatedDelivery' => $payload['estimatedDelivery'] ?? null,
        ];
    }

    /**
     * Subscribe to system-wide announcements
     */
    #[GraphQLField(description: 'Receive system-wide announcements')]
    public function systemAnnouncement(ExecutionContext $context): array
    {
        $payload = $context->root;

        return [
            'title' => $payload['title'],
            'message' => $payload['message'],
            'priority' => $payload['priority'] ?? 'normal',
            'timestamp' => $payload['timestamp'],
        ];
    }
}
