using LSFE.MobApp.Contracts;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LSFE.MobApp.Services
{
    public class QueryService : IQueryService
    {
        private readonly IDatabaseService _databaseService;
        private readonly ILogger<QueryService> _logger;

        public QueryService(IDatabaseService databaseService, ILogger<QueryService> logger)
        {
            _databaseService = databaseService;
            _logger = logger;
        }

        public async Task<List<T>> ExecuteQueryAsync<T>(string query, params object[] parameters) where T : new()
        {
            try
            {
                return await _databaseService.Database.QueryAsync<T>(query, parameters);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error executing query: {Query}", query);
                throw;
            }
        }

        public async Task<int> ExecuteNonQueryAsync(string query, params object[] parameters)
        {
            try
            {
                return await _databaseService.Database.ExecuteAsync(query, parameters);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error executing non-query: {Query}", query);
                throw;
            }
        }
    }
}
