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 EducationCatalogManagementController(
    IEducationCatalogManagementService service,
    DirectContentActorResolver actorResolver) : ControllerBase
{
    [HttpGet("scoreline/schools")]
    [Authorize(Policy = BackendPermissions.TenantScorelineManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
    [EndpointSummary("查询管理侧分数线院校")]
    [ProducesResponseType<CatalogList<School>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<School>>> GetSchools(
        [FromQuery] DirectContentQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetSchoolsAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("scoreline/schools")]
    [Authorize(Policy = BackendPermissions.TenantScorelineManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
    [EndpointSummary("新增或更新分数线院校")]
    [ProducesResponseType<ContentManagementResult<School>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<School>>> UpsertSchool(
        DirectSchoolDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertSchoolAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

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

    [HttpPut("scoreline/majors")]
    [Authorize(Policy = BackendPermissions.TenantScorelineManage)]
    [RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
    [EndpointSummary("新增或更新分数线专业")]
    [ProducesResponseType<ContentManagementResult<Major>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<Major>>> UpsertMajor(
        DirectMajorDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertMajorAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }
}
