using RackPeek.Domain.Helpers;
using RackPeek.Domain.Persistence;
using RackPeek.Domain.Resources;
using RackPeek.Domain.Resources.Connections;
using RackPeek.Domain.Resources.Servers;
using RackPeek.Domain.Resources.SubResources;

namespace RackPeek.Domain.UseCases.Ports;

public interface IUpdatePortUseCase<T> : IResourceUseCase<T>
    where T : Resource {
    public Task ExecuteAsync(
        string name,
        int index,
        string? type,
        double? speed,
        int? ports);
}

public class UpdatePortUseCase<T>(IResourceCollection repository) : IUpdatePortUseCase<T> where T : Resource {
    public async Task ExecuteAsync(
        string name,
        int index,
        string? type,
        double? speed,
        int? ports) {
        // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
        // ToDo validate / normalize all inputs

        name = Normalize.HardwareName(name);
        ThrowIfInvalid.ResourceName(name);

        var nicType = Normalize.NicType(type ?? string.Empty);
        ThrowIfInvalid.NicType(nicType);

        T resource = await repository.GetByNameAsync<T>(name)
                     ?? throw new NotFoundException($"Resource '{name}' not found.");

        if (resource is not IPortResource pr) throw new NotFoundException($"Resource '{name}' not found.");

        if (pr.Ports == null || index < 0 || index >= pr.Ports.Count)
            throw new NotFoundException($"Port index {index} not found on '{name}'.");

        Port nic = pr.Ports[index];

        var oldCount = nic.Count ?? 0;
        var newCount = ports ?? oldCount;

        if (newCount < oldCount)
            for (var i = newCount; i < oldCount; i++)
                await repository.RemoveConnectionsForPortAsync(new PortReference {
                    Resource = name,
                    PortGroup = index,
                    PortIndex = i
                });

        nic.Type = nicType;
        nic.Speed = speed;
        nic.Count = ports;

        await repository.UpdateAsync(resource);
    }
}
