using Dapper;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using Microsoft.Data.SqlClient;
using System.Linq;

namespace EVCB_OCPP.WEBAPI.Services
{
    public interface ICustomerService
    {
        string GetAPIKey(Guid partnerId);

    }

    public class CustomerService : ICustomerService
    {
        readonly string mainConnectionString;
        public CustomerService(IConfiguration configuration)
        {
            mainConnectionString = configuration.GetConnectionString("MainDBContext");
        }

        public string GetAPIKey(Guid partnerId)
        {
            string key = string.Empty;
            var parameters = new DynamicParameters();
            parameters.Add("@Id", partnerId, DbType.Guid, ParameterDirection.Input);
            using (SqlConnection conn = new SqlConnection(mainConnectionString))
            {
                string strSql = "Select ApiKey from [dbo].[Customer] where Id=@Id; ";
                key = conn.ExecuteScalarAsync<string>(strSql, parameters, null, EVCBConfiguration.DB_DefaultConnectionTimeout).Result;
            }
            return key;

        }
    }
}