using av_motion_api.Data;
using av_motion_api.Interfaces;
using av_motion_api.Models;
using av_motion_api.ViewModels;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace av_motion_api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly AppDbContext _appContext;
        public readonly IRepository _repository;
        public ProductController(AppDbContext _context, IRepository repository)
        {

            _appContext = _context;
            _repository = repository;
        }
        // GET: api/<ProductController>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<ProductViewModel>>> GetProducts()
        {
            var products = await _repository.GetProducts();

            return Ok(products);
        }

        // GET api/<ProductController>/5
        [HttpGet("{id}")]
        public async Task<ActionResult<ProductViewModel>> GetProduct(int id)
        {
            if (_appContext.Products == null)
            {
                return NotFound();
            }
            var product = await _repository.GetProduct(id);
            if (product == null)
            {
                return NotFound();
            }

            return Ok(product);
        }
        //POST api/<ProductController>
        [HttpPost]
        public async Task<ActionResult<Product>> PostProduct([FromBody] ProductViewModel product)
        {
            var newProduct = new Product()
            {
                Product_Name = product.name,
                Product_Description = product.description,
                Create_Date = DateTime.Now,
                Last_Update_Date = DateTime.Now,
                IsActive = product.IsActive,
                Size = product.Size,
                Product_Category_ID = product.Product_Category_ID,
                Supplier_ID = product.Supplier_ID,

            };



            if (_appContext.Products == null)
            {
                return Problem("Entity set 'AppDbContext.Products'  is null.");
            }
            _appContext.Products.Add(newProduct);
            await _appContext.SaveChangesAsync();

            return Ok(newProduct);
        }


        // PUT api/<ProductController>/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutProduct(int id, [FromBody] ProductViewModel updatedProduct)
        {
            var existingproduct = await _appContext.Products.FindAsync(id);
            if (existingproduct == null)
            {
                return NotFound();
            }

            existingproduct.Product_Name = updatedProduct.name;
            existingproduct.Product_Description = updatedProduct.description;
            existingproduct.Last_Update_Date = DateTime.Now;
            existingproduct.Product_Category_ID = updatedProduct.Product_Category_ID;
            existingproduct.Size = updatedProduct.Size;
           
            return null;
        }

        // DELETE api/<ProductController>/5
        [HttpPut("hide-product/{id}")]
      
        public async Task<IActionResult> HideProduct(int id)
        {
            if (_appContext.Products == null)
            {
                return NotFound();
            }
            var product = await _appContext.Products.FindAsync(id);
            if (product == null)
            {
                return NotFound();
            }

            product.IsActive = false; 
            await _appContext.SaveChangesAsync();

            return NoContent();
        }

        [HttpPut("unhide-product/{id}")]

        public async Task<IActionResult> UnHideProduct(int id)
        {
            if (_appContext.Products == null)
            {
                return NotFound();
            }
            var product = await _appContext.Products.FindAsync(id);
            if (product == null)
            {
                return NotFound();
            }

            product.IsActive = true;
            await _appContext.SaveChangesAsync();

            return NoContent();
        }

        private bool ProductExists(int id)
        {
            return (_appContext.Products?.Any(e => e.Product_ID == id)).GetValueOrDefault();
        }

    }
}