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 TenantStudentController(
    ITenantStudentService studentService,
    TenantAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("students")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询租户学生")]
    [ProducesResponseType<TenantAdminStudentList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantAdminStudentList>> GetStudents(
        [FromQuery] TenantAdminStudentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await studentService.GetStudentsAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("students")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("新增或更新租户学生档案")]
    [ProducesResponseType<ContentManagementResult<TenantAdminStudentItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminStudentItem>>> UpsertStudent(
        UpsertTenantAdminStudentDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await studentService.UpsertStudentAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

    [HttpPost("students/status")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("更新租户学生状态")]
    [ProducesResponseType<ContentManagementResult<TenantAdminStudentStatusItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminStudentStatusItem>>> UpdateStudentStatus(
        UpdateTenantAdminStudentStatusDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await studentService.UpdateStudentStatusAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("students/import/preview")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("预览学生批量导入")]
    [ProducesResponseType<TenantAdminStudentImportPreview>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantAdminStudentImportPreview>> PreviewStudentImport(
        TenantAdminStudentImportDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await studentService.PreviewStudentImportAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("students/import")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("提交学生批量导入")]
    [ProducesResponseType<TenantAdminStudentImportResult>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantAdminStudentImportResult>> ImportStudents(
        TenantAdminStudentImportDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await studentService.ImportStudentsAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

    [HttpPost("students/bulk-assign-class")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("批量分班")]
    [ProducesResponseType<TenantAdminBulkOperationResult>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantAdminBulkOperationResult>> BulkAssignClass(
        TenantAdminBulkAssignClassDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await studentService.BulkAssignClassAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("students/bulk-status")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("批量调整学生状态")]
    [ProducesResponseType<TenantAdminBulkOperationResult>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantAdminBulkOperationResult>> BulkStudentStatus(
        TenantAdminBulkStatusDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await studentService.BulkUpdateStudentStatusAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }
}

