using Gremlin_BlazorServer.Data.DBClasses;
using Gremlin_BlazorServer.Data.EntityClasses;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;

namespace Gremlin_BlazorServer.Services
{
	public class ProductLineService
	{
		private readonly GremlinContext gremlinContext;
		public ProductLineService(GremlinContext gC) => gremlinContext = gC;

		public void ConfigureServices(IServiceCollection services) => services.AddDbContext<GremlinContext>(ServiceLifetime.Scoped);

		public async Task<List<ProductLine>> GetAllProductLinesAsync() => await gremlinContext.ProductLines.ToListAsync();

		public async Task<bool> InsertProductLineAsync(ProductLine productLine)
		{
			try
			{
				_ = await gremlinContext.ProductLines.AddAsync(productLine);
				_ = await gremlinContext.SaveChangesAsync();
				return true;
			}
			catch (Exception exception)
			{
				Debug.WriteLine(exception.Message);
				return false;
			}
		}

		public async Task<ProductLine?> GetProductLineAsync(string productLineCode)
		{
			return await gremlinContext.ProductLines.FirstOrDefaultAsync(ProductLine => ProductLine.ProductLineCode.Equals(productLineCode));
		}

		public async Task<bool> UpdateProductLineAsync(ProductLine ProductLine)
		{
			_ = gremlinContext.ProductLines.Update(ProductLine);
			_ = await gremlinContext.SaveChangesAsync();
			return true;
		}

		public async Task<bool> DeleteProductLineAsync(ProductLine ProductLine)
		{
			_ = gremlinContext.Remove(ProductLine);
			_ = await gremlinContext.SaveChangesAsync();
			return true;
		}
	}
}
