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 TenantClassController(
    ITenantClassService classService,
    TenantAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("classes")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询租户班级")]
    [ProducesResponseType<TenantAdminClassList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantAdminClassList>> GetClasses(
        [FromQuery] TenantAdminClassQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await classService.GetClassesAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("classes")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("新增或更新租户班级")]
    [ProducesResponseType<ContentManagementResult<TenantAdminClassItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminClassItem>>> UpsertClass(
        UpsertTenantAdminClassDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await classService.UpsertClassAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

    [HttpPost("classes/disable")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("停用租户班级")]
    [ProducesResponseType<ContentManagementResult<TenantAdminClassItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminClassItem>>> DisableClass(
        [FromBody] Guid classId,
        CancellationToken cancellationToken)
    {
        return Ok(await classService.DisableClassAsync(actorResolver.Resolve(), classId, cancellationToken));
    }

    [HttpGet("classes/members")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询班级成员")]
    [ProducesResponseType<CatalogList<TenantAdminClassMemberItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantAdminClassMemberItem>>> GetClassMembers(
        [FromQuery] TenantAdminClassMemberQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await classService.GetClassMembersAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("classes/members")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("新增或更新班级成员")]
    [ProducesResponseType<ContentManagementResult<TenantAdminClassMemberItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminClassMemberItem>>> UpsertClassMember(
        UpsertTenantAdminClassMemberDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await classService.UpsertClassMemberAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("classes/members/remove")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("移除班级成员")]
    [ProducesResponseType<ContentManagementResult<TenantAdminClassMemberItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminClassMemberItem>>> RemoveClassMember(
        RemoveTenantAdminClassMemberDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await classService.RemoveClassMemberAsync(actorResolver.Resolve(), request.ClassMemberId,
            cancellationToken));
    }
}

