// validation_rest_min.cs
// A minimal program to test the FirmaSAT Validation REST API

using System.Text;
using System.Text.Json;
using System.Xml.Linq;

namespace RestValidator
{
    internal class Program
    {
        const string API_TOKEN = "YOUR_API_TOKEN_HERE";
        const string API_URL = "https://di-mgt.com.au/cryptosys/api/fsa_validation";
        public static async Task<string> PostJsonWithEncodedXmlAsync(string apiUrl)
        {
            // As a test, send some well-formed XML that is not a valid SAT CFDi document
            // This should return a status "partialsuccess" with an xml-error 
            // "XML file is not a supported document."
            string xmlString = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Root>
    <Data>Sample Value</Data>
</Root>";

            XDocument xDoc = XDocument.Parse(xmlString);
            string encodedXml = xDoc.ToString();

            var payload = new Dictionary<string, string>
            {
                ["api-token"] = API_TOKEN,
                ["xmldata"] = encodedXml,
            };
            string jsonContent = JsonSerializer.Serialize(payload);
            using var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
            using var client = new HttpClient();

            try {
                using HttpResponseMessage response = await client.PostAsync(apiUrl, content);
                response.EnsureSuccessStatusCode();
                return await response.Content.ReadAsStringAsync();
            } 
            catch (Exception ex) {
                return $"Error: {ex.Message}";
            }
        }

        public static async Task Main(string[] args)
        {
            string result = await PostJsonWithEncodedXmlAsync(API_URL);
            Console.WriteLine(result);
        }
    }
}