using System.Text.Json.Serialization;
using Prospect.Server.Api.Services.Auth.Extensions;
using Prospect.Server.Api.Services.CloudScript;
using Prospect.Server.Api.Services.Squad;

public class SquadMemberSelectedMapRequest
{
    [JsonPropertyName("squadId")]
    public string SquadID { get; set; }
    [JsonPropertyName("selectedMapName")]
    public string SelectedMapName { get; set; }
}

public class SquadMemberSelectedMapResponse
{
    // TODO: Unknown structure
}

[CloudScriptFunction("SquadMemberSelectedMap")]
public class SquadMemberSelectedMapFunction : ICloudScriptFunction<SquadMemberSelectedMapRequest, SquadMemberSelectedMapResponse>
{
    private readonly ILogger<SquadMemberSelectedMapFunction> _logger;
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly SquadService _squadService;

    public SquadMemberSelectedMapFunction(ILogger<SquadMemberSelectedMapFunction> logger, IHttpContextAccessor httpContextAccessor, SquadService squadService)
    {
        _logger = logger;
        _httpContextAccessor = httpContextAccessor;
        _squadService = squadService;
    }

    public Task<SquadMemberSelectedMapResponse> ExecuteAsync(SquadMemberSelectedMapRequest request)
    {
        var context = _httpContextAccessor.HttpContext;
        if (context != null && !string.IsNullOrEmpty(request.SquadID))
        {
            var userId = context.User.FindAuthUserId();
            _squadService.JoinOrCreate(request.SquadID, userId, "");
            _squadService.SetMap(request.SquadID, userId, request.SelectedMapName ?? "");
            _logger.LogInformation("SquadMemberSelectedMap: {User} -> {Map} (squad {Squad})", userId, request.SelectedMapName, request.SquadID);
        }
        return Task.FromResult(new SquadMemberSelectedMapResponse { });
    }
}
