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

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

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

		public async Task<List<AccountType>> GetAllAccountTypesAsync() => await gremlinContext.AccountTypes.ToListAsync();

		public async Task<bool> InsertAccountTypeAsync(AccountType accountType)
		{
			try
			{
				_ = await gremlinContext.AccountTypes.AddAsync(accountType);
				_ = await gremlinContext.SaveChangesAsync();
				return true;
			}
			catch (Exception exception)
			{
				Debug.WriteLine(exception.Message);
				return false;
			}
		}

		public async Task<AccountType?> GetAccountTypeAsync(string accountTypeCode)
		{
			return await gremlinContext.AccountTypes.FirstOrDefaultAsync(AccountType => AccountType.AccountTypeCode.Equals(accountTypeCode));
		}

		public async Task<bool> UpdateAccountTypeAsync(AccountType AccountType)
		{
			_ = gremlinContext.AccountTypes.Update(AccountType);
			_ = await gremlinContext.SaveChangesAsync();
			return true;
		}

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