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

namespace Strela.Controllers;

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

    public TypeController(ILogger<TypeController> logger)
    {
        _logger = logger;
    }

    //=========================== GET =================================//
    #region GET
    [HttpGet("GetTypes")]
    public IActionResult GetList()// Получить лист типов устройств
    {
        using (var db = new SQLiteService())
        {
            var tps = db.DeviceTypes.ToList();
            return Ok(tps);
        }
    }

    [HttpGet("GetTypeById")]
    [ProducesResponseType<DeviceItem>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public IActionResult GetById(Guid id) // Получить устройство по id
    {
        using (var db = new SQLiteService())
        {
            var d = db.DeviceTypes.Where(d => d.Id == id).FirstOrDefault();
            if (d != null)
                return Ok(d);
            else return NoContent();
        }
    }

    
    #endregion

    //========================= POST ====================================//
    #region POST

    [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();
        }
    }
    #endregion

    //========================= DELETE ====================================//
    #region DELETE
    [HttpDelete("DelType")]
    public IActionResult DelType(Guid id) //Удалить тип устройства по id
    {
        using (var db = new SQLiteService())
        {
            var deldevtype = db.DeviceTypes.Where(u => u.Id == id).FirstOrDefault();
            db.DeviceTypes.Remove(deldevtype);
            db.SaveChanges();
            return Ok();
        }
    }    
    #endregion
}
