using System;
using System.Collections.Generic;
using System.IO;


namespace AIRuntime.Editor
{
    /// <summary>
    /// Canonical names and backwards-compatible aliases used by saved editor
    /// agent configurations.
    /// </summary>
    internal static class AIRuntimeEditorProviderNames
    {
        public const string NvidiaDeveloper =
            "NvidiaDeveloper";


    }


    internal static class AIRuntimeEditorAgentConfigurationPaths
    {
        public static string ResolveInstanceId(
            string configuredAgentId,
            string configurationPath)
        {
            string configured =
                configuredAgentId?.Trim() ??
                string.Empty;

            string fileName =
                string.IsNullOrWhiteSpace(
                    configurationPath)
                    ? string.Empty
                    : Path.GetFileNameWithoutExtension(
                        configurationPath)?.Trim() ??
                      string.Empty;

            if (string.IsNullOrEmpty(
                    fileName) ||
                fileName.Equals(
                    "DefaultAgent",
                    StringComparison.OrdinalIgnoreCase))
            {
                return configured;
            }

            // The saved file is the Kanban agent instance. This permits
            // DeveloperAgent.json and DeveloperAgent2.json to share the same
            // role while retaining independent providers and task queues.
            return fileName;
        }


        public static bool ShouldPrefer(
            string agentId,
            string candidatePath,
            string currentPath)
        {
            if (string.IsNullOrWhiteSpace(
                    currentPath))
            {
                return true;
            }

            bool candidateIsExact =
                IsExactAgentFile(
                    agentId,
                    candidatePath);

            bool currentIsExact =
                IsExactAgentFile(
                    agentId,
                    currentPath);

            if (candidateIsExact !=
                currentIsExact)
            {
                return candidateIsExact;
            }

            return File.GetLastWriteTimeUtc(
                       candidatePath) >
                   File.GetLastWriteTimeUtc(
                       currentPath);
        }


        private static bool IsExactAgentFile(
            string agentId,
            string path)
        {
            return !string.IsNullOrWhiteSpace(
                       agentId) &&
                   !string.IsNullOrWhiteSpace(
                       path) &&
                   string.Equals(
                       Path.GetFileNameWithoutExtension(
                           path),
                       agentId.Trim(),
                       StringComparison.OrdinalIgnoreCase);
        }
    }


    /// <summary>
    /// Serialized agent configuration shared by the chat console and
    /// editor automation such as the Kanban task runner.
    /// </summary>
    [Serializable]
    internal sealed class AIRuntimeEditorAgentConfiguration
    {
        public int Version =
            1;

        public string Provider;

        public string Model;

        public bool SupportsImageInput;

        public string BaseUrl;

        public string ApiKey;

        public string WorkspaceRoot;

        public string ChatSystemPrompt;

        public string HttpReferer;

        public string ApplicationTitle;

        public bool UseAgent;

        public string AgentPreset;

        public string AgentId;

        public string AgentDisplayName;

        public string AgentDescription;

        public string AgentSystemPrompt;

        public float AgentTemperature;

        public int AgentMaxTokens;

        public bool EnableTools;

        public List<string> EnabledTools;

        public string WritePermission;

        public string DestructivePermission;

        public string ProcessPermission;

        public string NetworkPermission;
    }
}
