using BTCPayServer.Client;
using BTCPayServer.Client.Models;

namespace LittleShop.Testing;

/// <summary>
/// Test script to verify BTCPay Server connection and store configuration
/// Run this after configuring your BTCPay Server credentials
/// </summary>
public class BTCPayConnectionTest
{
    public static async Task TestConnection(string baseUrl, string apiKey, string storeId)
    {
        try
        {
            Console.WriteLine("Testing BTCPay Server connection...");
            Console.WriteLine($"Base URL: {baseUrl}");
            Console.WriteLine($"Store ID: {storeId}");
            Console.WriteLine();

            var client = new BTCPayServerClient(new Uri(baseUrl), apiKey);

            // Test 1: Get server info
            Console.WriteLine("1. Testing server connection...");
            var serverInfo = await client.GetServerInfo();
            Console.WriteLine($"   ✅ Connected to BTCPay Server v{serverInfo.Version}");
            Console.WriteLine($"   Supported cryptocurrencies: {string.Join(", ", serverInfo.SupportedPaymentMethods)}");
            Console.WriteLine();

            // Test 2: Get store information
            Console.WriteLine("2. Testing store access...");
            var store = await client.GetStore(storeId);
            Console.WriteLine($"   ✅ Store found: {store.Name}");
            Console.WriteLine($"   Store website: {store.Website}");
            Console.WriteLine();

            // Test 3: Test invoice creation
            Console.WriteLine("3. Testing invoice creation...");
            var invoiceRequest = new CreateInvoiceRequest
            {
                Amount = 0.001m,
                Currency = "BTC",
                Metadata = new Dictionary<string, object>
                {
                    ["orderId"] = "TEST-001",
                    ["description"] = "BTCPay Server connection test"
                }
            };

            var invoice = await client.CreateInvoice(storeId, invoiceRequest);
            Console.WriteLine($"   ✅ Test invoice created: {invoice.Id}");
            Console.WriteLine($"   Invoice status: {invoice.Status}");
            Console.WriteLine($"   Payment URL: {invoice.CheckoutLink}");
            Console.WriteLine();

            // Test 4: Get invoice details
            Console.WriteLine("4. Testing invoice retrieval...");
            var retrievedInvoice = await client.GetInvoice(storeId, invoice.Id);
            Console.WriteLine($"   ✅ Invoice retrieved: {retrievedInvoice.Id}");
            Console.WriteLine($"   Amount: {retrievedInvoice.Amount} {retrievedInvoice.Currency}");
            Console.WriteLine();

            Console.WriteLine("🎉 All tests passed! BTCPay Server integration is ready.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"❌ Test failed: {ex.Message}");
            if (ex.InnerException != null)
            {
                Console.WriteLine($"   Inner exception: {ex.InnerException.Message}");
            }
        }
    }

    // Example usage - replace with your actual credentials
    public static async Task Main(string[] args)
    {
        const string baseUrl = "https://pay.silverlabs.uk";
        const string apiKey = "YOUR_API_KEY_HERE";
        const string storeId = "YOUR_STORE_ID_HERE";

        if (apiKey == "YOUR_API_KEY_HERE" || storeId == "YOUR_STORE_ID_HERE")
        {
            Console.WriteLine("Please update the credentials in this file before running the test.");
            return;
        }

        await TestConnection(baseUrl, apiKey, storeId);
    }
}