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 ScorelineManagementController(
    IScorelineManagementService service,
    DirectContentActorResolver actorResolver) : ControllerBase
{
    [HttpGet("scoreline/fields")]
    [Authorize(Policy = BackendPermissions.TenantScorelineManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
    [EndpointSummary("查询管理侧分数线字段")]
    [ProducesResponseType<CatalogList<ScorelineField>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<ScorelineField>>> GetScorelineFields(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetScorelineFieldsAsync(actorResolver.Resolve(), query.ToFilter(),
            cancellationToken));
    }

    [HttpPut("scoreline/fields")]
    [Authorize(Policy = BackendPermissions.TenantScorelineManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
    [EndpointSummary("新增或更新动态分数线字段")]
    [ProducesResponseType<ContentManagementResult<ScorelineField>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<ScorelineField>>> UpsertScorelineField(
        DirectScorelineFieldDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertScorelineFieldAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("scoreline/records")]
    [Authorize(Policy = BackendPermissions.TenantScorelineManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
    [EndpointSummary("查询管理侧分数线记录")]
    [ProducesResponseType<CatalogList<ScorelineRecord>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<ScorelineRecord>>> GetScorelineRecords(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetScorelineRecordsAsync(actorResolver.Resolve(), query.ToFilter(),
            cancellationToken));
    }

    [HttpPut("scoreline/records")]
    [Authorize(Policy = BackendPermissions.TenantScorelineManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
    [EndpointSummary("新增或更新分数线记录")]
    [ProducesResponseType<ContentManagementResult<ScorelineRecord>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<ScorelineRecord>>> UpsertScorelineRecord(
        DirectScorelineRecordDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertScorelineRecordAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("scoreline/years")]
    [Authorize(Policy = BackendPermissions.TenantScorelineManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
    [EndpointSummary("查询分数线年份")]
    [ProducesResponseType<CatalogList<int>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<int>>> GetScorelineYears(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(
            await service.GetScorelineYearsAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpGet("scoreline/trend")]
    [Authorize(Policy = BackendPermissions.TenantScorelineManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
    [EndpointSummary("查询分数线趋势摘要")]
    [ProducesResponseType<CatalogList<ScorelineTrendItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<ScorelineTrendItem>>> GetScorelineTrend(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(
            await service.GetScorelineTrendAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }
}
