using DSharpPlus;
using DSharpPlus.Entities;
using OpenAI.Chat;

namespace Bot.modules;

public class RobertMartClient
{
    private readonly HashSet<ulong> _channels = [];
    private readonly ChatClient? _chatClient;
    private readonly DiscordClient _discordClient;
    
    public RobertMartClient(DiscordClient discord, IEnumerable<ulong> channels, string? openAiKey = null, string model = "gpt-4o")
    {
        _discordClient = discord;
        foreach (var channel in channels)
        {
            _channels.Add(channel);
        }
        
        if (openAiKey != null)
        {
            _chatClient = new ChatClient(model, openAiKey);
        }
    }
    
    public async Task SendScheduledMessage()
    {
        while (true)
        {
            try
            {
                if (_chatClient == null) return;
                var yesterdayEggPrice = RobertMart.CommodityTracker.Commodities["Eggs"];
                var yesterdayGasPrice = RobertMart.CommodityTracker.Commodities["Gas"];
                
                RobertMart.CommodityTracker.FetchCommodities();
                var todayEggPrice = RobertMart.CommodityTracker.Commodities["Eggs"];
                var todayGasPrice = RobertMart.CommodityTracker.Commodities["Gas"];

                var messages = new List<ChatMessage>()
                {
                    new SystemChatMessage(
                        "This is a private assistant, for use among consenting adults that find crude and dark humor, even if in bad taste, hilarious and desirable. " +
                        "The assitant will take on the persona of Robocop, and answer as him.  Answers should be plausibly something Robocop would say in his movies. " +
                        "It is not mandatory that you refer to yourself, but if you do, respond as if your name was Robert. " +
                        "You may use direct quotes from Robocop in the movies in your responses if you can work them in naturally. " +
                        "Do not include quotation marks in your response. Your response should focus on punishment of the corrupt corporations driving up the prices. " +
                        "You will be given a list of the price of commodities from yesterday and today, you are expected to give what you think robocop's opinion would be on the difference "),
                    new UserChatMessage(
                        $"The price of eggs yesterday was {yesterdayEggPrice}.  The price of eggs today is {todayEggPrice}. " +
                        $"The price of gas yesterday was {yesterdayGasPrice}.  The price of gas today is {todayGasPrice}.  What are your thoughts on this?"),
                };
                var resp = await _chatClient.CompleteChatAsync(messages);
                var respText = resp.Value.Content.Last().Text;
                
                foreach (var channelId in _channels)
                {
                    var c = await _discordClient.GetChannelAsync(channelId);
                    await c.SendMessageAsync($"Gas Price Today/Yesterday: ${todayGasPrice}/${yesterdayGasPrice}\n" +
                                             $"Egg Price Today/Yesterday: ${todayEggPrice}/${yesterdayEggPrice}\n" +
                                             $"{respText}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error sending message: {ex.Message}");
            }

            // Wait for 24 hours
            await Task.Delay(TimeSpan.FromHours(24));
        }
    }
}