using IWS5.DatabaseAccess.Interfaces.IAdmin;
using IWS5.DatabaseAccess.IwsDataContext;
using IWS5.DatabaseAccess.IwsDataModels;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Data;

namespace IWS5.DatabaseAccess.Repositories.IAdmin;

public class ServerRepository(GreenfieldPd2015Context context)
    : IWSRepository<Atserver>(context), IServerRepository
{
    public async Task<List<Atserver>> ListAsync()
    {
        return await context.Atservers
            .OrderBy(s => s.HostName)
            .ToListAsync();
    }

    public async Task<Atserver?> GetByNameAsync(string name)
    {
        return await context.Atservers
            .Include(s => s.AtscheduledItems)
                .ThenInclude(si => si.Status)
            .Include(s => s.AtscheduledItems)
                .ThenInclude(si => si.LastRunStatus)
            .Include(s => s.AteventDefinitions)
            .FirstOrDefaultAsync(s => s.HostName == name);
    }

    public async Task AddAsync(Atserver entity)
    {
        context.Atservers.Add(entity);
        await context.SaveChangesAsync();
    }

    public async Task UpdateAsync(Atserver entity)
    {
        context.Atservers.Update(entity);
        await context.SaveChangesAsync();
    }

    public async Task DeleteAsync(Atserver entity)
    {
        context.Atservers.Remove(entity);
        await context.SaveChangesAsync();
    }
}
