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 HandbookManagementController(
    IHandbookManagementService service,
    DirectContentActorResolver actorResolver) : ControllerBase
{
    [HttpGet("handbook-subjects")]
    [Authorize(Policy = BackendPermissions.TenantHandbookManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Handbook)]
    [EndpointSummary("查询管理侧知识手册科目")]
    [ProducesResponseType<CatalogList<HandbookSubject>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<HandbookSubject>>> GetHandbookSubjects(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetHandbookSubjectsAsync(actorResolver.Resolve(), query.ToFilter(),
            cancellationToken));
    }

    [HttpPut("handbook-subjects")]
    [Authorize(Policy = BackendPermissions.TenantHandbookManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Handbook)]
    [EndpointSummary("新增或更新知识手册科目")]
    [ProducesResponseType<ContentManagementResult<HandbookSubject>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<HandbookSubject>>> UpsertHandbookSubject(
        DirectHandbookSubjectDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertHandbookSubjectAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("handbook-chapters")]
    [Authorize(Policy = BackendPermissions.TenantHandbookManage)]
    [Authorize(Policy = TikuPolicies.TenantAllDataScope)]
    [RequireSaasFeature(SaasFeatureCatalog.Handbook)]
    [EndpointSummary("查询管理侧知识手册章节")]
    [ProducesResponseType<CatalogList<HandbookChapter>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<HandbookChapter>>> GetHandbookChapters(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetHandbookChaptersAsync(actorResolver.Resolve(), query.ToFilter(),
            cancellationToken));
    }

    [HttpPut("handbook-chapters")]
    [Authorize(Policy = BackendPermissions.TenantHandbookManage)]
    [Authorize(Policy = TikuPolicies.TenantAllDataScope)]
    [RequireSaasFeature(SaasFeatureCatalog.Handbook)]
    [EndpointSummary("新增或更新知识手册章节")]
    [ProducesResponseType<ContentManagementResult<HandbookChapter>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<HandbookChapter>>> UpsertHandbookChapter(
        DirectHandbookChapterDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertHandbookChapterAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("handbook-entries")]
    [Authorize(Policy = BackendPermissions.TenantHandbookManage)]
    [Authorize(Policy = TikuPolicies.TenantAllDataScope)]
    [RequireSaasFeature(SaasFeatureCatalog.Handbook)]
    [EndpointSummary("查询管理侧知识手册条目")]
    [ProducesResponseType<CatalogList<HandbookEntry>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<HandbookEntry>>> GetHandbookEntries(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetHandbookEntriesAsync(actorResolver.Resolve(), query.ToFilter(),
            cancellationToken));
    }

    [HttpPut("handbook-entries")]
    [Authorize(Policy = BackendPermissions.TenantHandbookManage)]
    [Authorize(Policy = TikuPolicies.TenantAllDataScope)]
    [RequireSaasFeature(SaasFeatureCatalog.Handbook)]
    [EndpointSummary("新增或更新知识手册条目")]
    [ProducesResponseType<ContentManagementResult<HandbookEntry>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<HandbookEntry>>> UpsertHandbookEntry(
        DirectHandbookEntryDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertHandbookEntryAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }
}
