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

namespace Strela.Controllers;

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

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

    //=========================== GET =================================//
    [HttpGet("GetObject")]
    public IActionResult GetList()
    {
        using (var db = new SQLiteService())
        {
            var oi = db.ObjectItems.ToList();
                return Ok(oi);
        }
    }

    [HttpGet("GetObjectById")]
    [ProducesResponseType<ObjectItem>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public IActionResult GetById(Guid id)
    {
        using (var db = new SQLiteService())
        {
            var o = db.ObjectItems.Where(o => o.Id == id).FirstOrDefault();
            if (o != null)
                return Ok(o);
            else return NoContent();
        }
    }

    [HttpGet("GetObjectByName")]
    [ProducesResponseType<ObjectItem>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public IActionResult GetByName(string name)
    {
        using (var db = new SQLiteService())
        {
            var o = db.ObjectItems.Where(ob => ob.NameObject == name).FirstOrDefault();
            if (o != null)
                return Ok(o);
            else return NoContent();
        }
    }


    //========================= POST ====================================//
    [HttpPost("AddObject")]
    public IActionResult AddObject(ObjectItem o)
    {
        using (var db = new SQLiteService())
        {
            db.ObjectItems.Add(o);
            db.SaveChanges();
            return Created();
        }
    }

    //========================= DELETE ====================================//
    [HttpDelete("DelObject")]
    public IActionResult DelObject(Guid id)
    {
        using (var db = new SQLiteService())
        {
            var delobj = db.ObjectItems.Where(o => o.Id == id).FirstOrDefault();
            db.ObjectItems.Remove(delobj);
            db.SaveChanges();
            return Ok();
        }
    }
}
