using Core.Interfaces;
using Domain.Entities;
using Models.Interfaces;
using System.Reflection;

namespace Core.Services
{
    public class BusinessUnitService: IBusinessUnitDom
    {
        #region Declaraciones y Constructor
        private readonly IPhSBusinessUnitRepository _repository;
        public BusinessUnitService(IPhSBusinessUnitRepository repository)
        {
            _repository = repository ?? throw new ArgumentNullException(nameof(repository));
        }
        #endregion
        #region Metodos de clases
        public async Task<IEnumerable<EBusinessUnit>> GetAllAsync()
        {
            try
            {
                return await _repository.GetAllAsync();
            }
            catch (Exception ex)
            {
                var method = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                throw new Exception($"{method} Message: {ex.Message}", ex);
            }
        }
        public async Task<EBusinessUnit?> GetByIdAsync(int id)
        {
            try
            {
                return await _repository.GetByIdAsync(id);
            }
            catch (Exception ex)
            {
                var method = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                throw new Exception($"{method} Message: {ex.Message}", ex);
            }
        }
        public async Task<IEnumerable<EBusinessUnit>> SearchAsync(string term)
        {
            try
            {
                return await _repository.SearchAsync(term);
            }
            catch (Exception ex)
            {
                var method = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                throw new Exception($"{method} Message: {ex.Message}", ex);
            }
        }
        public async Task<EBusinessUnit> CreateAsync(EBusinessUnit entity)
        {
            if (entity == null)
                throw new ArgumentNullException(nameof(entity), "La unidad de negocio no puede ser nula.");
            try 
            { 
                return await _repository.CreateAsync(entity);
            }
            catch (Exception ex)
            {
                var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                throw new Exception($"{ex.Message}", ex);
            }
        }
        public async Task<bool> UpdateAsync(EBusinessUnit unit)
        {
            if (unit == null)
                throw new ArgumentNullException(nameof(unit), "La unidad de negocio no puede ser nula.");

            try
            {
                return await _repository.UpdateAsync(unit);
            }
            catch (Exception ex)
            {
                var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                throw new Exception($"{methodName} Message: {ex.Message}", ex);
            }
        }
        public async Task<bool> DeleteAsync(int id)
        {
            try
            {
                return await _repository.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                throw new Exception($"{methodName} Message: {ex.Message}", ex);
            }
        }
        #endregion
    }
}
