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

namespace Core.Services
{
    public class ProductCategoryService : IProductCategoryDom
    {
        private readonly IPhSProductCategoryRepository _repository;
        public ProductCategoryService(IPhSProductCategoryRepository repository)
        {
            _repository = repository ?? throw new ArgumentNullException(nameof(repository));
        }
        public async Task<IEnumerable<EProductCategory>> 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<EProductCategory?> 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<EProductCategory>> 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<EProductCategory> AddAsync(EProductCategory entity)
        {
            if (entity == null)
                throw new ArgumentNullException(nameof(entity), "La categoría no puede ser nula.");

            return await _repository.CreateAsync(entity);
        }
        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);
            }
        }
        public async Task<bool> UpdateAsync(EProductCategory unit)
        {
            if (unit == null)
                throw new ArgumentNullException(nameof(unit), "La categoría 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);
            }
        }
    }
}
