using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Api.Security;
using Tiku.Application.Assets;
using Tiku.Application.Catalog;
using Tiku.Application.Content;
using Tiku.Application.Jobs;
using Tiku.Application.Security;
using Tiku.Domain.Catalog;
using Tiku.Domain.Content;

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("租户端-内容直接管理")]
[Produces("application/json")]
[Route("api/tenant/content")]
public sealed class VocabularyManagementController(
    IVocabularyManagementService service,
    DirectContentActorResolver actorResolver) : ControllerBase
{
    [HttpGet("vocabulary-units")]
    [Authorize(Policy = BackendPermissions.TenantVocabularyManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Vocabulary)]
    [EndpointSummary("查询管理侧词汇单元")]
    [ProducesResponseType<CatalogList<VocabularyUnit>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<VocabularyUnit>>> GetVocabularyUnits(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetVocabularyUnitsAsync(actorResolver.Resolve(), query.ToFilter(),
            cancellationToken));
    }

    [HttpPut("vocabulary-units")]
    [Authorize(Policy = BackendPermissions.TenantVocabularyManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Vocabulary)]
    [EndpointSummary("新增或更新词汇单元")]
    [ProducesResponseType<ContentManagementResult<VocabularyUnit>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<VocabularyUnit>>> UpsertVocabularyUnit(
        DirectVocabularyUnitDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertVocabularyUnitAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("vocabulary-words")]
    [Authorize(Policy = BackendPermissions.TenantVocabularyManage)]
    [Authorize(Policy = TikuPolicies.TenantAllDataScope)]
    [RequireSaasFeature(SaasFeatureCatalog.Vocabulary)]
    [EndpointSummary("查询管理侧词汇")]
    [ProducesResponseType<CatalogList<VocabularyWord>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<VocabularyWord>>> GetVocabularyWords(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetVocabularyWordsAsync(actorResolver.Resolve(), query.ToFilter(),
            cancellationToken));
    }

    [HttpPut("vocabulary-words")]
    [Authorize(Policy = BackendPermissions.TenantVocabularyManage)]
    [Authorize(Policy = TikuPolicies.TenantAllDataScope)]
    [RequireSaasFeature(SaasFeatureCatalog.Vocabulary)]
    [EndpointSummary("新增或更新词汇")]
    [ProducesResponseType<ContentManagementResult<VocabularyWord>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<VocabularyWord>>> UpsertVocabularyWord(
        DirectVocabularyWordDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertVocabularyWordAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }
}
