using AzureRestApi.Repositories;
using Spectre.Console;

namespace AzureRestApi.Services
{
    public class CreatedOnDateService
    {
        private const string TagName = "CreatedOnDate";
        private readonly AzureSubscriptionRepository _azureSubscriptionRepository;
        private readonly AzureResourceRepository _azureResourceRepository;
        private readonly AzureTagRepository _azureTagRepository;

        public CreatedOnDateService(AzureSubscriptionRepository azureSubscriptionRepository, AzureResourceRepository azureResourceRepository, AzureTagRepository azureTagRepository)
        {
            _azureSubscriptionRepository = azureSubscriptionRepository;
            _azureResourceRepository = azureResourceRepository;
            _azureTagRepository = azureTagRepository;
        }

        public async Task SetCreatedOnDateTags(bool skipDone = true)
        {
            var rule = new Rule("[skyblue1]Setting CreatedOnDateTags[/]");
            rule.Alignment = Justify.Left;
            rule.Style = Style.Parse("skyblue1");
            AnsiConsole.Write(rule);

            await AnsiConsole.Status()
                 .AutoRefresh(true)
                .Spinner(Spinner.Known.Default)
                .StartAsync("Retrieving subscriptions...", async ctx =>
                {
                    var subscriptions = await _azureSubscriptionRepository.GetAll();
                    if (subscriptions == null || !subscriptions.Any()) throw new Exception("No subscriptions found");
                    subscriptions = subscriptions.Where(s => s.Enabled && !string.IsNullOrWhiteSpace(s.subscriptionId)).ToList();
                    if (!subscriptions.Any()) throw new Exception("No subscriptions found");

                    foreach (var subscription in subscriptions)
                    {
                        rule = new Rule($"[skyblue1]{subscription.displayName}[/]");
                        rule.Alignment = Justify.Left;
                        rule.Style = Style.Parse("skyblue1 dim");
                        AnsiConsole.Write(rule);

                        ctx.Status($"Getting the resources for subscription '{subscription.displayName}'");

                        var resources = await _azureResourceRepository.GetAll(subscription.subscriptionId);
                        if (resources == null || !resources.Any()) continue;
                        resources = resources.Where(r => !string.IsNullOrWhiteSpace(r.CreatedOn) && !string.IsNullOrWhiteSpace(r.id)).ToList();

                        if (skipDone)
                        {
                            resources = resources.Where(r => !r.tags.Any(t => t.Key == TagName && !string.IsNullOrWhiteSpace(t.Value))).ToList();
                        }

                        ctx.Status($"Setting created date tag on resources in subscription '{subscription.displayName}'");
                        foreach (var resource in resources)
                        {
                            var tagSuccess = await _azureTagRepository.SetTag(resource.id, TagName, resource.CreatedOn);
                            AnsiConsole.MarkupLine($"{resource.id} " + (tagSuccess ? "[green]V[/]" : "[red]X[/]"));
                        }
                    }
                });
        }
    }
}
