using Core.Abstractions;
using DataProviders.Collections;
using DomainResults.Common;
using JWTService;
using WeatherForecast.Models.Requests;

namespace WeatherForecast.Services {

  /// <summary>
  /// 
  /// </summary>
  public interface IAuthenticationService {

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="requestData"></param>
    /// <returns></returns>
    (string?, IDomainResult) Post(Guid siteId, AuthenticationRequestModel requestData);
  }

  /// <summary>
  /// 
  /// </summary>
  public class AutheticationService : ServiceBase<AutheticationService>, IAuthenticationService {

    private readonly IUserDataProvider _userDataProvider;
    private readonly IJWTService _jwtService;
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="logger"></param>
    /// <param name="userDataProvider"></param>
    /// <param name="jwtService"></param>
    public AutheticationService (
      ILogger<AutheticationService> logger,
      IUserDataProvider userDataProvider,
      IJWTService jwtService
    ) : base(logger) {
      _userDataProvider = userDataProvider;
      _jwtService = jwtService;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="requestData"></param>
    /// <returns></returns>
    public (string?, IDomainResult) Post (Guid siteId, AuthenticationRequestModel requestData) {
      try {
        var token = _jwtService.CreateJwtToken();

        return token != null
          ? IDomainResult.Success(token)
          : IDomainResult.Failed<string?>();
      }
      catch (Exception ex) {
        _logger.LogError("Unhandled exception", ex);
        return IDomainResult.Failed<string?>();
      }
    }
  }
}
