using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Api.OpenApi;
using Tiku.Application.Auth;
using Tiku.Application.PlatformAdmin;
using Tiku.Application.Security;
using Tiku.Domain.Platform;

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("平台端-平台管理")]
[Authorize(Policy = BackendPermissions.PlatformDashboardView)]
[Produces("application/json")]
[Route("api/platform")]
public sealed class PlatformTenantDomainController(
    IPlatformTenantDomainService service,
    PlatformAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("domains")]
    [Authorize(Policy = BackendPermissions.PlatformTenantManage)]
    [EndpointSummary("查询租户域名状态")]
    [ProducesResponseType<PlatformDomainList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformDomainList>> Domains(
        [FromQuery] PlatformAdminQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetDomainsAsync(actorResolver.Resolve(), query.ToQuery(), cancellationToken));
    }

    [HttpPost("domains/{domainId:guid}/recheck")]
    [Authorize(Policy = BackendPermissions.PlatformTenantManage)]
    [EndpointSummary("重新触发租户域名 DNS/TLS 验证")]
    [ProducesResponseType<PlatformDomainRecheckResult>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformDomainRecheckResult>> RecheckDomain(
        Guid domainId,
        CancellationToken cancellationToken)
    {
        return Ok(await service.RecheckDomainAsync(actorResolver.Resolve(), domainId, cancellationToken));
    }
}
