using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Application.Backtests;
using Managing.Application.Bots;
using Managing.Infrastructure.Databases;
using Managing.Infrastructure.Databases.InfluxDb;
using Managing.Infrastructure.Databases.InfluxDb.Models;
using Managing.Infrastructure.Evm;
using Managing.Infrastructure.Evm.Abstractions;
using Managing.Infrastructure.Evm.Services;
using Managing.Infrastructure.Evm.Subgraphs;
using Managing.Infrastructure.Exchanges;
using Managing.Infrastructure.Exchanges.Abstractions;
using Managing.Infrastructure.Exchanges.Exchanges;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using static Managing.Common.Enums;

namespace Managing.Application.Tests
{
    public static class TradingBaseTests
    {
        public static IExchangeService GetExchangeService()
        {
            ILoggerFactory loggerFactory = new NullLoggerFactory();

            var ChainlinkGmx = new ChainlinkGmx(SubgraphService.GetSubgraphClient(SubgraphProvider.ChainlinkGmx));
            var Chainlink = new Chainlink(SubgraphService.GetSubgraphClient(SubgraphProvider.ChainlinkPrice));
            var GbcFeed = new Gbc(SubgraphService.GetSubgraphClient(SubgraphProvider.Gbc));

            var Subgraphs = new List<ISubgraphPrices>
            {
                ChainlinkGmx,
                Chainlink,
                GbcFeed
            };
            var cacheService = new Mock<ICacheService>();
            var evmManager = new EvmManager(Subgraphs, CreateWebProxyService(), cacheService.Object);
            var evmProcessor = new EvmProcessor(new Mock<ILogger<EvmProcessor>>().Object, evmManager);

            var exchangeProcessors = new List<IExchangeProcessor>()
            {
                //new Mock<FtxProcessor>().Object,
                //new Mock<BinanceProcessor>().Object,
                evmProcessor
            };

            return new ExchangeService(loggerFactory.CreateLogger<ExchangeService>(), GetCandleRepository(),
                exchangeProcessors);
        }

        public static ILogger<TradingBotBase> CreateTradingBotLogger()
        {
            ILoggerFactory loggerFactory = new NullLoggerFactory();

            return loggerFactory.CreateLogger<TradingBotBase>();
        }

        public static ILogger<Backtester> CreateBacktesterLogger()
        {
            ILoggerFactory loggerFactory = new NullLoggerFactory();

            return loggerFactory.CreateLogger<Backtester>();
        }

        public static ILogger<CandleRepository> CreateCandleRepositoryLogger()
        {
            ILoggerFactory loggerFactory = new NullLoggerFactory();

            return loggerFactory.CreateLogger<CandleRepository>();
        }

        public static ICandleRepository GetCandleRepository()
        {
            var settings = new InfluxDbSettings()
            {
                Url = "http://localhost:8086/",
                Token = "6b-OjFNaZRprYroZEx8zeLScvPqvOp9la1lEksXl8xRT0d96UyuN18iKpB6jKYFt8JJEX1NaxVMXhk-Sgy8sgg==",
                Organization = "managing-org"
            };
            var influxdb = new InfluxDbRepository(settings);
            var candleRepository = new CandleRepository(influxdb, CreateCandleRepositoryLogger());

            return candleRepository;
        }

        // Helper method to create Web3ProxyService for tests
        public static IWeb3ProxyService CreateWebProxyService(string baseUrl = "http://localhost:4111")
        {
            var settings = new Web3ProxySettings { BaseUrl = baseUrl };
            var options = Options.Create(settings);
            return new Web3ProxyService(options);
        }
    }
}