// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

using Catalogger.Backend.Bot;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Gateway.Commands;
using Remora.Discord.API.Objects;

namespace Catalogger.Backend.Services;

public class StatusUpdateService(ILogger logger, ShardedGatewayClient shardedClient, Config config)
    : BackgroundService
{
    private readonly ILogger _logger = logger.ForContext<StatusUpdateService>();

    protected override async Task ExecuteAsync(CancellationToken ct)
    {
        using var timer = new PeriodicTimer(TimeSpan.FromMinutes(3));
        while (await timer.WaitForNextTickAsync(ct))
            UpdateShardStatuses(ct);
    }

    private void UpdateShardStatuses(CancellationToken ct = default)
    {
        _logger.Information(
            "Updating status for {TotalShards} shards. Guild count is {GuildCount}",
            shardedClient.TotalShards,
            CataloggerMetrics.GuildsCached.Value
        );

        if (config.Discord.TestMode)
        {
            _logger.Debug("Not updating shard statuses because test mode is enabled.");
            return;
        }

        foreach (var (shardId, client) in shardedClient.Shards)
        {
            if (!ShardedGatewayClient.IsConnected(client))
            {
                _logger.Warning(
                    "Cannot update status for shard {ShardId} as it is not connected",
                    shardId
                );
                continue;
            }

            client.SubmitCommand(PresenceFor(shardId));
        }
    }

    private UpdatePresence PresenceFor(int shardId)
    {
        var status = $"/catalogger help | in {CataloggerMetrics.GuildsCached.Value} servers";

        if (shardedClient.TotalShards != 1)
            status += $" | shard {shardId + 1}/{shardedClient.TotalShards}";

        return new UpdatePresence(
            Status: UserStatus.Online,
            IsAFK: false,
            Since: null,
            Activities: [new Activity(Name: "Beep", Type: ActivityType.Custom, State: status)]
        );
    }
}
