using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Strela.Models;
using Strela.Services;

namespace Strela.Controllers;

[ApiController]
[Route("api/[controller]")]
public class DeviceController : ControllerBase
{
    private readonly ILogger<UserController> _logger;

    public DeviceController(ILogger<UserController> logger)
    {
        _logger = logger;
    }
    //=========================== GET =================================//
    [HttpGet("GetTypes")]
    [ProducesResponseType<List<DeviceType>>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public IActionResult GetList()
    {
        using (var db = new SQLiteService())
        {
            var tps = db.DeviceTypes.ToList();
            if (tps.Count  != 0)
                return Ok(tps);
            else return NoContent();
        }
    }

    [HttpGet("GetDevice")]
    [ProducesResponseType<DeviceItem>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public IActionResult GetById(int id)
    {
        using (var db = new SQLiteService())
        {
            var d = db.Devices.Where(d => d.Id == id).FirstOrDefault();
            if (d != null)
                return Ok(d);
            else return NoContent();
        }
    }

    //========================= POST ====================================//
    [HttpPost("AddDevice")]
    public IActionResult AddDevice(DeviceItem d)
    {
        using (var db = new SQLiteService())
        {
            db.Devices.Add(d);//.Where(u => u.Name == name).FirstOrDefault();
            db.SaveChanges();
            return Created();
        }
    }

    [HttpPost("AddType")]
    public IActionResult AddType(DeviceType d)
    {
        using (var db = new SQLiteService())
        {
            db.DeviceTypes.Add(d);//.Where(u => u.Name == name).FirstOrDefault();
            db.SaveChanges();
            return Created();
        }
    }
}
