using System;
using System.Collections.Generic;
using AIRuntime.Runtime.Models;

namespace AIRuntime.Runtime.Providers
{
    /// <summary>Explicit capabilities of the configured model and endpoint, not the provider brand.</summary>
    public interface IProviderInputCapabilities
    {
        string Model { get; }
        bool SupportsImageInput { get; }
    }

    public sealed class ProviderInputNotSupportedException : InvalidOperationException
    {
        public ProviderInputNotSupportedException(string message) : base(message) { }
    }

    public static class ProviderInputGuard
    {
        public static void Validate(IProvider provider, IReadOnlyList<ConversationMessage> messages)
        {
            if (messages == null) return;
            foreach (ConversationMessage message in messages)
            {
                if (message?.Attachments == null) continue;
                foreach (ConversationAttachment attachment in message.Attachments)
                {
                    if (attachment == null) continue;
                    bool image = string.Equals(attachment.Kind, "Image", StringComparison.OrdinalIgnoreCase) ||
                        (attachment.MediaType?.StartsWith("image/", StringComparison.OrdinalIgnoreCase) ?? false);
                    if (!image) continue;
                    var capabilities = provider as IProviderInputCapabilities;
                    if (capabilities?.SupportsImageInput == true) continue;
                    throw new ProviderInputNotSupportedException(
                        $"Image input blocked before sending: {provider.Name} model '{capabilities?.Model ?? "unknown"}' " +
                        "has not been configured to accept images. Select a model and endpoint with image support " +
                        "and enable Image input in its settings. The image was preserved; visual verification is incomplete.");
                }
            }
        }
    }
}
