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

namespace QYZH.InteractiveMagazine.WebApi.Controllers;

/// <summary>
/// 期刊二维码管理
/// </summary>
[ApiController]
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
[Route("api/userJournalQrCode")]
public class UserJournalQrCodeController(IUserJournalService userJournalService) : BaseController
{
    /// <summary>
    /// 生成期刊二维码
    /// </summary>
    /// <param name="input">生成输入</param>
    /// <returns>二维码记录</returns>
    [HttpPost("add")]
    public async Task<BaseResponse<CreateUserJournalQrCodeOutput>> AddAsync([FromBody] CreateUserJournalQrCodeInput input)
    {
        var result = await userJournalService.CreateQrCodesAsync(input, GetCurrentUserName() ?? "System");
        return BaseResponse<CreateUserJournalQrCodeOutput>.Success(result);
    }

    /// <summary>
    /// 根据期刊Id获取未绑定用户的二维码地址
    /// </summary>
    /// <param name="journalId">期刊Id</param>
    /// <returns>未绑定二维码列表</returns>
    [HttpGet("unbound/{journalId:long}")]
    public async Task<BaseResponse<List<UserJournalQrCodeOutput>>> GetUnboundByJournalIdAsync(long journalId)
    {
        var result = await userJournalService.GetUnboundQrCodesByJournalIdAsync(journalId);
        return BaseResponse<List<UserJournalQrCodeOutput>>.Success(result);
    }

    /// <summary>
    /// 分页查询期刊二维码
    /// </summary>
    /// <param name="input">查询条件</param>
    /// <returns>分页结果</returns>
    [HttpPost("pagelist")]
    public async Task<BaseResponse<PageListModel<UserJournalQrCodeOutput>>> GetPageListAsync([FromBody] UserJournalQrCodeQueryInput input)
    {
        var result = await userJournalService.GetQrCodePageListAsync(input);
        return BaseResponse<PageListModel<UserJournalQrCodeOutput>>.Success(result);
    }

    /// <summary>
    /// 获取期刊二维码详情
    /// </summary>
    /// <param name="id">二维码记录Id</param>
    /// <returns>二维码记录</returns>
    [HttpGet("detail/{id:long}")]
    public async Task<BaseResponse<UserJournalQrCodeOutput>> GetDetailAsync(long id)
    {
        var result = await userJournalService.GetQrCodeDetailAsync(id);
        return BaseResponse<UserJournalQrCodeOutput>.Success(result);
    }

    /// <summary>
    /// 删除未绑定的期刊二维码
    /// </summary>
    /// <param name="input">删除输入</param>
    /// <returns>是否成功</returns>
    [HttpPost("delete")]
    public async Task<BaseResponse<bool>> DeleteAsync([FromBody] DeleteUserJournalQrCodeInput input)
    {
        var result = await userJournalService.DeleteQrCodeAsync(input, GetCurrentUserName() ?? "System");
        return BaseResponse<bool>.Success(result);
    }
}
