using Ablera.Serdica.Microservice.Consumer.Attributes;
using Ablera.Serdica.Microservice.Consumer.Contracts.Asynchronous;
using Ablera.Serdica.Common.Tools.Exceptions;
using Ablera.Serdica.Authority.Services;
using System.Threading.Tasks;
using Ablera.Serdica.Authentication.Constants;
using Ablera.Serdica.DBModels.Serdica;
using System.Linq;
using Ablera.Serdica.Authority.Models;
using Ablera.Serdica.Extensions.RabbitMQ.Contracts;
using Microsoft.EntityFrameworkCore;
using System;
using Ablera.Serdica.Microservice.Consumer.Contracts;
using static Ablera.Serdica.Authority.Constants.ConstantsClass;

namespace Ablera.Serdica.NotificationService.Endpoints;

public record UserGetLoginEnabledRequest
{
    public required string UserGuid { get; init; }
}

public record UserGetLoginEnabledResponse
{
    public required bool LoginEnabled { get; init; }
}

[Command("users_user_login_enabled", timeoutInSeconds: 10, methodName: "POST", allowedRoles: [SerdicaClaims.RoleSuperUser])]
public class UserLoginEnabledEndpoint(
    SerdicaDbContext dbContext)
        : Microservice.Consumer.Contracts.Asynchronous.IEndpointWithRequest<UserGetLoginEnabledRequest, UserGetLoginEnabledResponse>
{
    public async Task<UserGetLoginEnabledResponse> ConsumeAsync(UserGetLoginEnabledRequest request)
    {
        var userAccount = await dbContext.UserAccounts
            .Where(x => x.UserGuid == request.UserGuid)
            .FirstOrDefaultAsync()
            ?? throw new BaseResultException("account_not_found".AsCode(), "Account associated with the session not found!");

        return new UserGetLoginEnabledResponse { LoginEnabled = userAccount.LockAccount != YesKey };
    }
}
