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

namespace Core.Services
{
    public class TicketService : ITicketDom
    {
        //#region Declaraciones y Constructor
        //private readonly ITicketRepository contract;
        //public TicketService() => contract = new TicketRepository();
        //#endregion
        #region Declaraciones y Constructor
        private readonly ITicketRepository _contract;
        public TicketService(ITicketRepository contract)
        {
            _contract = contract ?? throw new ArgumentNullException(nameof(contract));
        }
        #endregion

        #region Metodos de clase
        public async Task<IEnumerable<ETicket>> GetAllAsync()
        {
            try
            {
                return await _contract.GetAllAsync();
            }
            catch (Exception ex)
            {
                var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                var message = ex.Message ?? "No message provided";
                throw new Exception($"{methodName} Message: {message}", ex);
            }
        }
        public async Task<ETicket> GetByIdAsync(Guid ticketId)
        {
            try
            {
                var ticket = await _contract.GetByIdAsync(ticketId);
                return ticket;
            }
            catch (Exception ex)
            {
                var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                var message = ex.Message ?? "No message provided";
                throw new Exception($"{methodName} Message: {message}", ex);
            }
        }
        public async Task<IEnumerable<ETickets_GetSummary>> GetSummaryAsync()
        {
            try
            {
                return await _contract.GetSummaryAsync();
            }
            catch (Exception ex)
            {
                var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                var message = ex.Message ?? "No message provided";
                throw new Exception($"{methodName} Message: {message}", ex);
            }
        }
        public async Task<IEnumerable<ETicket_Dashboard>> GetTicketDashboardAsync(string estado, string orden)
        {
            try
            {
                return await _contract.GetTicketDashboardAsync(estado, orden);
            }
            catch (Exception ex)
            {
                var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                var message = ex.Message ?? "No message provided";
                throw new Exception($"{methodName} Message: {message}", ex);
            }
        }
        public async Task InsertTicketAsync(ETicket eTicket)
        {
            try
            {
                await _contract.InsertTicketAsync(eTicket);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public async Task<byte[]> ExcelTicketDashboardAsync(string estado, string orden)
        {
            try
            {
                var stream = new XLSXExportBase();
                var dashboardData = await _contract.GetTicketDashboardAsync(estado, orden);
                return stream.ExportExcel(dashboardData.ToList());
            }
            catch (Exception ex)
            {
                var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
                var message = ex.Message ?? "No message provided";
                throw new Exception($"{methodName} Message: {message}", ex);
            }
        }
        #endregion
    }
}
