using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Messaging.Queries.GetTicketThread;
using Baya.Application.Features.Messaging.Queries.ListTicketsForAdmin;
using Baya.Application.Models.Common;
using Baya.Application.Models.Messaging;
using Baya.Infrastructure.Identity.Identity.PermissionManager;
using Baya.WebFramework.Attributes;
using Baya.WebFramework.BaseController;
using Mediator;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Baya.Web.Api.Controllers.V1;

/// <summary>Admin ticket console (support/admin): the global queue and the <b>admin</b> thread view (internal
/// notes included). Internal-only, RBAC-gated, audited.</summary>
[ApiVersion("1")]
[ApiController]
[Route("api/v{version:apiVersion}/admin/tickets")]
[Authorize(ConstantPolicies.DynamicPermission)]
[Display(Description = "Admin ticket queue + full (internal-inclusive) thread view")]
public sealed class AdminTicketsController(ISender sender) : BaseController
{
    [HttpGet]
    [ProducesOkApiResponseType<PagedResult<TicketSummaryDto>>]
    public async Task<IActionResult> List([FromQuery] ListTicketsForAdminQuery query, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(query, cancellationToken));

    // Admin view — internal notes ARE returned.
    [HttpGet("{id}")]
    [ProducesOkApiResponseType<TicketThreadDto>]
    public async Task<IActionResult> Thread(long id, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new GetTicketThreadQuery(id, AsAdmin: true), cancellationToken));
}
