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 TenantSiteSettingsController(
    ITenantSiteSettingsService siteSettingsService,
    TenantAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("audit-logs")]
    [Authorize(Policy = BackendPermissions.TenantStaffManage)]
    [EndpointSummary("查询租户审计日志")]
    [ProducesResponseType<CatalogList<TenantAdminAuditLogItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantAdminAuditLogItem>>> GetAuditLogs(
        [FromQuery] TenantAdminAuditLogQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await siteSettingsService.GetAuditLogsAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("branding")]
    [Authorize(Policy = BackendPermissions.TenantSettingsManage)]
    [EndpointSummary("更新租户品牌信息")]
    [ProducesResponseType<ContentManagementResult<TenantBrandingItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantBrandingItem>>> UpsertBranding(
        UpsertTenantBrandingDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await siteSettingsService.UpsertBrandingAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

    [HttpPut("settings")]
    [Authorize(Policy = BackendPermissions.TenantSettingsManage)]
    [EndpointSummary("更新租户公开设置与功能开关")]
    [ProducesResponseType<ContentManagementResult<TenantSettingsItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantSettingsItem>>> UpsertSettings(
        UpsertTenantSettingsDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await siteSettingsService.UpsertSettingsAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

    [HttpGet("theme-templates")]
    [Authorize(Policy = BackendPermissions.TenantSettingsManage)]
    [EndpointSummary("查询可用租户主题模板")]
    [ProducesResponseType<CatalogList<TenantThemeTemplateItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantThemeTemplateItem>>> GetThemeTemplates(
        CancellationToken cancellationToken)
    {
        return Ok(await siteSettingsService.GetThemeTemplatesAsync(actorResolver.Resolve(), cancellationToken));
    }

    [HttpGet("theme")]
    [Authorize(Policy = BackendPermissions.TenantSettingsManage)]
    [EndpointSummary("查询租户当前主题与草稿")]
    [ProducesResponseType<ContentManagementResult<TenantThemeItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantThemeItem>>> GetTheme(
        CancellationToken cancellationToken)
    {
        return Ok(await siteSettingsService.GetThemeAsync(actorResolver.Resolve(), cancellationToken));
    }

    [HttpPost("theme/preview")]
    [Authorize(Policy = BackendPermissions.TenantSettingsManage)]
    [EndpointSummary("生成租户主题草稿")]
    [ProducesResponseType<ContentManagementResult<TenantThemeItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantThemeItem>>> PreviewTheme(
        PreviewTenantThemeDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await siteSettingsService.PreviewThemeAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

    [HttpPost("theme/publish")]
    [Authorize(Policy = BackendPermissions.TenantSettingsManage)]
    [EndpointSummary("发布租户主题")]
    [ProducesResponseType<ContentManagementResult<TenantThemeItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantThemeItem>>> PublishTheme(
        PublishTenantThemeDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await siteSettingsService.PublishThemeAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }
}

