using Microsoft.AspNetCore.Mvc;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Enum;

namespace QYZH.InteractiveMagazine.WebApi.Controllers;

/// <summary>
/// AIPrompt配置管理控制器
/// </summary>
[Route("api/[controller]")]
[ApiController]
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
public class AiBasePromptController : BaseController
{
    private readonly IAiBasePromptService _aiBasePromptService;
    private readonly ILogger<AiBasePromptController> _logger;

    public AiBasePromptController(IAiBasePromptService aiBasePromptService, ILogger<AiBasePromptController> logger)
    {
        _aiBasePromptService = aiBasePromptService;
        _logger = logger;
    }

    /// <summary>
    /// 创建Prompt配置
    /// </summary>
    /// <param name="input">Prompt配置信息</param>
    /// <returns>创建的Prompt配置信息</returns>
    [HttpPost]
    public async Task<BaseResponse<AiBasePromptOutput>> CreateAsync([FromBody] AiBasePromptInput input)
    {
        try
        {
            var result = await _aiBasePromptService.CreateAsync(input);
            return Success(result, "创建Prompt配置成功");
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "创建Prompt配置业务异常: {Message}", ex.Message);
            return BaseResponse<AiBasePromptOutput>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "创建Prompt配置系统异常，参数：{Input}", input);
            return BaseResponse<AiBasePromptOutput>.Fail("创建Prompt配置失败，请稍后重试");
        }
    }

    /// <summary>
    /// 更新Prompt配置
    /// </summary>
    /// <param name="id">Prompt配置ID</param>
    /// <param name="input">Prompt配置信息</param>
    /// <returns>更新后的Prompt配置信息</returns>
    [HttpPut("{id}")]
    public async Task<BaseResponse<AiBasePromptOutput>> UpdateAsync(long id, [FromBody] AiBasePromptInput input)
    {
        try
        {
            var result = await _aiBasePromptService.UpdateAsync(id, input);
            return Success(result, "更新Prompt配置成功");
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "更新Prompt配置业务异常: {Message}", ex.Message);
            return BaseResponse<AiBasePromptOutput>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "更新Prompt配置系统异常，ID：{Id}，参数：{Input}", id, input);
            return BaseResponse<AiBasePromptOutput>.Fail("更新Prompt配置失败，请稍后重试");
        }
    }

    /// <summary>
    /// 删除Prompt配置
    /// </summary>
    /// <param name="id">Prompt配置ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("{id}")]
    public async Task<BaseResponse<object>> DeleteAsync(long id)
    {
        try
        {
            await _aiBasePromptService.DeleteAsync(id);
            return Success(new object(), "删除Prompt配置成功");
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "删除Prompt配置业务异常: {Message}", ex.Message);
            return BaseResponse<object>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "删除Prompt配置系统异常，ID：{Id}", id);
            return BaseResponse<object>.Fail("删除Prompt配置失败，请稍后重试");
        }
    }

    /// <summary>
    /// 根据ID获取Prompt配置
    /// </summary>
    /// <param name="id">Prompt配置ID</param>
    /// <returns>Prompt配置信息</returns>
    [HttpGet("{id}")]
    public async Task<BaseResponse<AiBasePromptOutput>> GetByIdAsync(long id)
    {
        try
        {
            var result = await _aiBasePromptService.GetByIdAsync(id);
            return Success(result);
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "获取Prompt配置业务异常: {Message}", ex.Message);
            return BaseResponse<AiBasePromptOutput>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取Prompt配置系统异常，ID：{Id}", id);
            return BaseResponse<AiBasePromptOutput>.Fail("获取Prompt配置信息失败，请稍后重试");
        }
    }

    /// <summary>
    /// 分页查询Prompt配置列表
    /// </summary>
    /// <param name="input">查询条件</param>
    /// <returns>分页结果</returns>
    [HttpPost("list")]
    public async Task<BaseResponse<PageListModel<AiBasePromptOutput>>> GetListAsync([FromBody] AiBasePromptQueryInput input)
    {
        try
        {
            var result = await _aiBasePromptService.GetListAsync(input);
            return Success(result);
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "查询Prompt配置列表业务异常: {Message}", ex.Message);
            return BaseResponse<PageListModel<AiBasePromptOutput>>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "查询Prompt配置列表系统异常，参数：{Input}", input);
            return BaseResponse<PageListModel<AiBasePromptOutput>>.Fail("查询Prompt配置列表失败，请稍后重试");
        }
    }

    /// <summary>
    /// 获取全部Prompt配置（无分页）
    /// </summary>
    /// <returns>全部Prompt配置列表</returns>
    [HttpGet("all")]
    public async Task<BaseResponse<List<AiBasePromptOutput>>> GetAllAsync()
    {
        try
        {
            var result = await _aiBasePromptService.GetAllAsync();
            return Success(result);
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "查询全部Prompt配置业务异常: {Message}", ex.Message);
            return BaseResponse<List<AiBasePromptOutput>>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "查询全部Prompt配置系统异常");
            return BaseResponse<List<AiBasePromptOutput>>.Fail("查询Prompt配置列表失败，请稍后重试");
        }
    }

    /// <summary>
    /// 切换Prompt启用/禁用状态
    /// </summary>
    /// <param name="id">Prompt配置ID</param>
    /// <returns>操作结果</returns>
    [HttpPut("{id}/toggle-status")]
    public async Task<BaseResponse<object>> ToggleStatusAsync(long id)
    {
        try
        {
            await _aiBasePromptService.ToggleStatusAsync(id);
            return Success(new object(), "切换Prompt状态成功");
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "切换Prompt状态业务异常: {Message}", ex.Message);
            return BaseResponse<object>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "切换Prompt状态系统异常，ID：{Id}", id);
            return BaseResponse<object>.Fail("切换Prompt状态失败，请稍后重试");
        }
    }
}
