using System.Security.Claims;
using Managing.Application.Abstractions.Services;
using Managing.Domain.Users;
using Microsoft.AspNetCore.Mvc;

namespace Managing.Api.Controllers;

[ApiController]
[Route("[controller]")]
[Produces("application/json")]
public abstract class BaseController : ControllerBase
{
    public readonly IUserService _userService;

    public BaseController(IUserService userService)
    {
        _userService = userService;
    }

    protected async Task<User> GetUser()
    {
        var identity = HttpContext?.User.Identity as ClaimsIdentity;
        if (identity != null)
        {
            var address = identity.Claims.FirstOrDefault(c => c.Type == "address")?.Value;
            if (address != null)
            {
                var user = await _userService.GetUserByAddressAsync(address);
                return user;
            }

            throw new Exception("User not found for this token");
        }

        throw new Exception("Not identity assigned to this token");
    }
}