using Strata.Code.Business.Services.Interfaces;
using Strata.Code.DataAccess.Models;
using Strata.Code.DataAccess.Repositories.Interfaces;

namespace Strata.Code.Business.Services
{
    public class BudgetConfigDefaultSettingService : IBudgetConfigDefaultSettingService
    {
        private readonly IBudgetConfigDefaultSettingRepository _repository;

        public BudgetConfigDefaultSettingService(IBudgetConfigDefaultSettingRepository repository)
        {
            _repository = repository;
        }

        public async Task<IEnumerable<BudgetConfigDefaultSettingDto>> GetAllAsync()
        {
            var settings = await _repository.GetAllAsync();
            return settings.Select(s => MapToDto(s));
        }

        public async Task<BudgetConfigDefaultSettingDto?> GetByIdAsync(int id)
        {
            var setting = await _repository.GetByIdAsync(id);
            return setting != null ? MapToDto(setting) : null;
        }

        public async Task<BudgetConfigDefaultSettingDto> CreateAsync(BudgetConfigDefaultSettingDto settingDto)
        {
            var setting = MapToEntity(settingDto);
            var result = await _repository.CreateAsync(setting);
            return MapToDto(result);
        }

        public async Task<BudgetConfigDefaultSettingDto?> UpdateAsync(BudgetConfigDefaultSettingDto settingDto)
        {
            var setting = MapToEntity(settingDto);
            var result = await _repository.UpdateAsync(setting);
            return result != null ? MapToDto(result) : null;
        }

        public async Task<bool> DeleteAsync(int id)
        {
            return await _repository.DeleteAsync(id);
        }

        private BudgetConfigDefaultSettingDto MapToDto(BudgetConfigDefaultSetting entity)
        {
            return new BudgetConfigDefaultSettingDto
            {
                SettingId = entity.SettingId,
                Name = entity.Name,
                DefaultValue = entity.DefaultValue,
                Description = entity.Description,
                DateCreated = entity.DateCreated
            };
        }

        private BudgetConfigDefaultSetting MapToEntity(BudgetConfigDefaultSettingDto dto)
        {
            return new BudgetConfigDefaultSetting
            {
                SettingId = dto.SettingId,
                Name = dto.Name,
                DefaultValue = dto.DefaultValue,
                Description = dto.Description,
                DateCreated = dto.DateCreated
            };
        }
    }
}
