using SWIFTFramework;
using SWIFTFramework.Messages.Category1;

namespace CreateMessage;

/// <summary>
/// Build an MT103 from code and emit FIN text.
/// License key is never shipped — set SWIFTFRAMEWORK_RUNTIME_LICENSE_KEY or User Secrets.
/// </summary>
class Program
{
    static int Main()
    {
        if (!TryApplyLicense())
        {
            Console.Error.WriteLine("No runtime license key found.");
            Console.Error.WriteLine("Set SWIFTFRAMEWORK_RUNTIME_LICENSE_KEY, or add SWIFTFramework:RuntimeLicenseKey to .NET User Secrets.");
            return 1;
        }

        var mt103 = new MT103();

        mt103.Block1.ApplicationIdentifier = "F";
        mt103.Block1.ServiceIdentifier = "01";
        mt103.Block1.LTAddress = "BANKUSPPAXXX";
        mt103.Block1.SessionNumber = "0000";
        mt103.Block1.SequenceNumber = "000000";

        mt103.Block2.MessageType = "103";
        mt103.Block2.MessagePriority = "N";
        ((SwiftBlock2Input)mt103.Block2).DestinationAddress = "BANKUKZHAXXX";

        mt103.SendersReference_20.Value = "ABCDEREF";
        mt103.BankOperationCode_23B.Value = "CRED";
        mt103.ValueDateCurrencyInterbankSettledAmount_32A.Amount = 14551m;
        mt103.ValueDateCurrencyInterbankSettledAmount_32A.Currency = "USD";
        mt103.ValueDateCurrencyInterbankSettledAmount_32A.Date = new DateTime(2026, 7, 24);
        mt103.OrderingCustomer_50K.Value = "/0123456789";
        mt103.BeneficiaryCustomer_59.Value = "/9988776655";
        mt103.DetailsOfCharges_71A.Value = "SHA";

        string fin = SwiftParser.Instance.GetMessageAsText(mt103);
        Console.WriteLine(fin);
        return 0;
    }

    static bool TryApplyLicense()
    {
        string? key = Environment.GetEnvironmentVariable("SWIFTFRAMEWORK_RUNTIME_LICENSE_KEY");
        if (!string.IsNullOrWhiteSpace(key))
            Licensing.RunTimeLicenseKey = key.Trim();

        return !string.IsNullOrWhiteSpace(Licensing.RunTimeLicenseKey);
    }
}
