using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NexusIdentity.Api.Data;

namespace NexusIdentity.Api.Controllers;

[ApiController]
[Route("api/roles")]
[AllowAnonymous]
public sealed class RolesController : ControllerBase
{
    private readonly IdentityDbContext _db;

    public RolesController(IdentityDbContext db)
    {
        _db = db;
    }

    [HttpGet]
    public async Task<IActionResult> List(CancellationToken ct)
    {
        var roles = await _db.Roles
            .AsNoTracking()
            .OrderBy(r => r.Name)
            .Select(r => new RoleDto { Id = r.Id.ToString(), Name = r.Name })
            .ToListAsync(ct);

        return Ok(roles);
    }
}

public sealed class RoleDto
{
    public required string Id { get; init; }
    public required string Name { get; init; }
}
