using System;
using LevelCode.License.DataAccess;
using LevelCode.License.Models;

namespace LevelCode.License.Business
{
    public class LicencaLogService
    {
        private readonly LicencaLogDAL _logDal;

        public LicencaLogService(LicencaLogDAL logDal)
        {
            _logDal = logDal;
        }

        // =========================
        // REGISTRAR LOG
        // =========================
        public void Registrar(
            int? licencaId,
            string acao,
            string detalhes = null,
            string ipCliente = null,
            string hwidHash = null)
        {
            ModeloLicencaLog log = new ModeloLicencaLog
            {
                LicencaId = licencaId,
                Acao = acao,
                Detalhes = detalhes,
                IpCliente = ipCliente,
                HWID_Hash = hwidHash,
                DataHora = DateTime.Now
            };

            _logDal.Inserir(log);
        }

        // =========================
        // LOG DE ERRO
        // =========================
        public void RegistrarErro(
            int? licencaId,
            Exception ex,
            string contexto = null,
            string hwidHash = null)
        {
            string detalhes = ex.Message;

            if (!string.IsNullOrWhiteSpace(contexto))
                detalhes = contexto + " | " + detalhes;

            Registrar(
                licencaId,
                "ERRO",
                detalhes,
                null,
                hwidHash);
        }
    }
}
