using log4net;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Common.Http
{
    /// <summary>
    /// Helper class to build API responses with string error codes
    /// </summary>
    public static class ApiResponseHelper
    {
        /// <summary>
        /// Build HTTP response with string error code
        /// </summary>
        /// <param name="log">Logger instance</param>
        /// <param name="url">Request URL</param>
        /// <param name="request">Request JSON</param>
        /// <param name="errorCode">Error code as string (e.g., "0", "-801")</param>
        /// <param name="message">Response message</param>
        /// <param name="data">Response data object</param>
        /// <returns>IActionResult</returns>
        public static IActionResult BuildResponse(
            ILog log,
            string url,
            string request,
            string errorCode,
            string message,
            object data
        )
        {
            // Create response object with string errorCode
            var response = new
            {
                errorCode = errorCode,  // String type
                message = message,
                data = data
            };

            // Log the response
            string responseJson = JsonConvert.SerializeObject(response);
            log.Info($"URL: {url} | Request: {request} | Response: {responseJson}");

            // Return as JSON result with proper content type
            return new ContentResult
            {
                Content = responseJson,
                ContentType = "application/json",
                StatusCode = 200
            };
        }
    }
}
