using Microsoft.AspNetCore.Mvc;
using MilitaryTrainingManagement.Models.Enums;

namespace MilitaryTrainingManagement.Controllers;

/// <summary>
/// API控制器基类
/// </summary>
[ApiController]
[Route("api/admin/[controller]")]
public abstract class BaseApiController : ControllerBase
{
    protected int? GetCurrentUserId()
    {
        var userIdClaim = User.FindFirst("userId");
        if (userIdClaim != null && int.TryParse(userIdClaim.Value, out var userId))
        {
            return userId;
        }
        return null;
    }

    protected int? GetCurrentUnitId()
    {
        var unitIdClaim = User.FindFirst("unitId");
        if (unitIdClaim != null && int.TryParse(unitIdClaim.Value, out var unitId))
        {
            return unitId;
        }
        return null;
    }

    protected OrganizationalLevel? GetCurrentUnitLevel()
    {
        var unitLevelClaim = User.FindFirst("unitLevel");
        if (unitLevelClaim != null && int.TryParse(unitLevelClaim.Value, out var level))
        {
            return (OrganizationalLevel)level;
        }
        return null;
    }
}
