namespace MyOffice.Web.Controllers;

using System.Security.Authentication;
using Microsoft.AspNetCore.Mvc;
using Core.Extensions;
using IdentityModel;
using MyOffice.Web.Infrastructure.Attributes;
using MyOffice.Web.Models;

[DefaultFromBody]
public class BaseApiController : ControllerBase
{
	public Guid UserId
	{
		get
		{
			var subject = User.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject)?.Value;
			if (subject.IsPresent() && Guid.TryParse(subject, out var guid)) return guid;

			throw new AuthenticationException("Get UserId failed");
		}
	}

	public ObjectResult ProblemBadResponse(string? detail = null)
	{
		return Problem(detail, statusCode: StatusCodes.Status400BadRequest);
	}

	public ObjectResult OkResponse(IResponseModel response)
	{
		return Ok(response);
	}

	public ObjectResult OkResponse<T>(IEnumerable<T> response) where T : IResponseModel
	{
		return Ok(response);
	}
}