 // Get Methoden um Metrics abzugreifen
 // Get Methden um Informationen über den Status des Servers einzuholen
 using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using watcher_monitoring.Models;

using watcher_monitoring.Data;
using watcher_monitoring.Attributes;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace watcher_monitoring.Controllers;

[ApiController]
[Route("[controller]")]
[ApiKeyAuth]
public class APIController : Controller
{
    private readonly WatcherDbContext _context;

    private readonly ILogger<APIController> _logger;

    public APIController(WatcherDbContext context, ILogger<APIController> logger)
    {
        _context = context;
        _logger = logger;
    }

    // Server Calls

    [HttpGet("get-server")]
    public async Task<IActionResult> Servers()
    {
        List<Server> servers = await _context.Servers.ToListAsync();
        return Ok();
    }

    [HttpPost("add-server")]
    public async Task<IActionResult> AddServer()
    {
        return Ok();
    }

    [HttpDelete("delete-server/{id}")]
    public async Task<IActionResult> DeleteServer(int id)
    {
        var server = await _context.Servers.FindAsync(id);
        if (server == null)
        {
            _logger.LogError("Server nicht gefunden");
            return BadRequest();
        }

        _context.Servers.Remove(server);
        await _context.SaveChangesAsync();

        _logger.LogInformation("Server '{server}' erfolgreich gelöscht", server.Name);


        return Ok();
    }

    [HttpPut("edit-server")]
    public async Task<IActionResult> EditServer()
    {
        return Ok();
    }

    // Container Calls
    [HttpGet("get-container")]
    public async Task<IActionResult> Containers()
    {
        List<Container> containers = await _context.Containers.ToListAsync();
        return Ok();
    }

    [HttpDelete("delete-container")]
    public async Task<IActionResult> DeleteContainer(int id)
    {
        var container = await _context.Containers.FindAsync(id);
        if (container == null)
        {
            _logger.LogError("Server nicht gefunden");
            return BadRequest();
        }
        try
        {
            _context.Containers.Remove(container);
            await _context.SaveChangesAsync();
            _logger.LogInformation("Container '{container}' erfolgreich gelöscht", container.Id);
            return Ok();
        } catch (Exception ex)
        {
            _logger.LogError(ex.Message);
            return BadRequest();
        }
    }
}