using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Domain.Bots;
using Managing.Domain.Workflows;
using Microsoft.Extensions.Logging;
using static Managing.Common.Enums;

namespace Managing.Application.Bots.Base
{
    public class BotFactory : IBotFactory
    {
        private readonly IExchangeService _exchangeService;
        private readonly IMessengerService _messengerService;
        private readonly IAccountService _accountService;
        private readonly ILogger<TradingBot> _tradingBotLogger;
        private readonly ITradingService _tradingService;
        private readonly IBotService _botService;

        public BotFactory(
            IExchangeService exchangeService,
            ILogger<TradingBot> tradingBotLogger,
            IMessengerService messengerService,
            IAccountService accountService,
            ITradingService tradingService,
            IBotService botService)
        {
            _tradingBotLogger = tradingBotLogger;
            _exchangeService = exchangeService;
            _messengerService = messengerService;
            _accountService = accountService;
            _tradingService = tradingService;
            _botService = botService;
        }

        IBot IBotFactory.CreateSimpleBot(string botName, Workflow workflow)
        {
            return new SimpleBot(botName, _tradingBotLogger, workflow, _botService);
        }

        ITradingBot IBotFactory.CreateTradingBot(TradingBotConfig config)
        {
            return new TradingBot(
                _exchangeService,
                _tradingBotLogger,
                _tradingService,
                _accountService,
                _messengerService,
                _botService,
                config);
        }

        ITradingBot IBotFactory.CreateBacktestTradingBot(TradingBotConfig config)
        {
            config.IsForBacktest = true;
            return new TradingBot(
                _exchangeService,
                _tradingBotLogger,
                _tradingService,
                _accountService,
                _messengerService,
                _botService,
                config);
        }

        // Legacy methods for backward compatibility - will be deprecated
        ITradingBot IBotFactory.CreateScalpingBot(TradingBotConfig config)
        {
            config.BotType = BotType.ScalpingBot;
            config.FlipPosition = false;
            return new TradingBot(
                _exchangeService,
                _tradingBotLogger,
                _tradingService,
                _accountService,
                _messengerService,
                _botService,
                config);
        }

        ITradingBot IBotFactory.CreateBacktestScalpingBot(TradingBotConfig config)
        {
            config.BotType = BotType.ScalpingBot;
            config.IsForBacktest = true;
            config.FlipPosition = false;
            return new TradingBot(
                _exchangeService,
                _tradingBotLogger,
                _tradingService,
                _accountService,
                _messengerService,
                _botService,
                config);
        }

        public ITradingBot CreateFlippingBot(TradingBotConfig config)
        {
            config.BotType = BotType.FlippingBot;
            config.FlipPosition = true;
            return new TradingBot(
                _exchangeService,
                _tradingBotLogger,
                _tradingService,
                _accountService,
                _messengerService,
                _botService,
                config);
        }

        public ITradingBot CreateBacktestFlippingBot(TradingBotConfig config)
        {
            config.BotType = BotType.FlippingBot;
            config.IsForBacktest = true;
            config.FlipPosition = true;
            return new TradingBot(
                _exchangeService,
                _tradingBotLogger,
                _tradingService,
                _accountService,
                _messengerService,
                _botService,
                config);
        }
    }
}
