using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Application.Auth;
using Tiku.Application.Catalog;
using Tiku.Application.Content;
using Tiku.Application.Security;
using Tiku.Application.TenantAdmin;

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("租户端-运营管理")]
[Produces("application/json")]
[Route("api/tenant")]
public sealed class TenantMemberAccessController(
    ITenantMemberAccessService memberAccessService,
    IAuthAdministrationService authAdministrationService,
    TenantAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("members")]
    [Authorize(Policy = BackendPermissions.TenantStaffManage)]
    [EndpointSummary("查询租户成员")]
    [ProducesResponseType<CatalogList<TenantAdminMemberItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantAdminMemberItem>>> GetMembers(
        [FromQuery] TenantAdminMemberQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await memberAccessService.GetMembersAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("members")]
    [Authorize(Policy = BackendPermissions.TenantStaffManage)]
    [EndpointSummary("新增或更新租户成员")]
    [ProducesResponseType<ContentManagementResult<TenantAdminMemberItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminMemberItem>>> UpsertMember(
        UpsertTenantAdminMemberDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await memberAccessService.UpsertMemberAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

    [HttpPost("members/disable")]
    [Authorize(Policy = BackendPermissions.TenantStaffManage)]
    [EndpointSummary("停用租户成员并撤销会话")]
    [ProducesResponseType<ContentManagementResult<TenantAdminMemberItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminMemberItem>>> DisableMember(
        DisableTenantAdminMemberDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await memberAccessService.DisableMemberAsync(actorResolver.Resolve(), request.MembershipId, cancellationToken));
    }

    [HttpPost("members/{userId:guid}/password-reset")]
    [Authorize(Policy = BackendPermissions.TenantStaffManage)]
    [EndpointSummary("为租户成员设置一次性临时密码")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public async Task<IActionResult> ResetMemberPassword(
        Guid userId,
        AdministrativePasswordResetDto request,
        CancellationToken cancellationToken)
    {
        var actor = actorResolver.Resolve();
        await authAdministrationService.ResetPasswordAsync(
            new AdministrativePasswordResetRequest(
                actor.UserId,
                userId,
                actor.TenantId,
                request.TemporaryPassword,
                request.Reason),
            cancellationToken);
        return NoContent();
    }
}
