using AzureRestApi.Repositories;
using Spectre.Console;

namespace AzureRestApi.Services
{
    public class DeploymentTypeService
    {
        private readonly AzureSubscriptionRepository _azureSubscriptionRepository;
        private readonly AzureDeploymentRepository _azureDeploymentRepository;
        private readonly AzureResourceGroupRepository _azureResourceGroupRepository;

        public DeploymentTypeService(AzureSubscriptionRepository azureSubscriptionRepository, AzureDeploymentRepository azureDeploymentRepository, AzureResourceGroupRepository azureResourceGroupRepository)
        {
            _azureSubscriptionRepository = azureSubscriptionRepository;
            _azureDeploymentRepository = azureDeploymentRepository;
            _azureResourceGroupRepository = azureResourceGroupRepository;
        }

        public async Task CheckDeploymentTypes()
        {
            var rule = new Rule("[skyblue1]Checking deployment types[/]");
            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 =>
                {
                    // Retrieve subscriptions
                    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 deployments for subscription '{subscription.displayName}'");
                        var deployments = await _azureDeploymentRepository.GetAllBySubscription(subscription.subscriptionId);

                        if (deployments == null || !deployments.Any()) continue;

                        foreach (var deployment in deployments)
                        {
                            AnsiConsole.WriteLine(Markup.Escape(deployment?.name));
                        }

                        ctx.Status($"Getting the resource groups for subscription '{subscription.displayName}'");
                        var resourceGroups = await _azureResourceGroupRepository.GetAll(subscription.subscriptionId);
                        foreach (var resourceGroup in resourceGroups)
                        {
                            ctx.Status($"Getting the deployments for resourcegroup {resourceGroup.name} in subscription '{subscription.displayName}'");
                        
                            var resourceGroupDeployments = await _azureDeploymentRepository.GetAllByResourceGroup(subscription.subscriptionId, resourceGroup.name);
                            foreach (var deployment in resourceGroupDeployments)
                            {
                                AnsiConsole.WriteLine(Markup.Escape($"{resourceGroup.name} -  {deployment?.name}"));
                            }
                        }



                    }
                });
        }
    }
}
