using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Snapshot;
using Microsoft.AspNetCore.Mvc;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Share.Tools;
using Hotline.Caching.Interfaces;
using XF.Utility.EnumExtensions;
using Hotline.Share.Enums.Snapshot;
using Quartz.Impl.Triggers;
using Hotline.Orders;
using XF.Domain.Exceptions;
using Mapster;
using Hotline.Snapshot.IRepository;
using Hotline.Application.Snapshot.Contracts;
using Hotline.Api.Filter;

namespace Hotline.Api.Controllers.Snapshot;

public class RedPackController : BaseController
{
    private readonly IRedPackApplication _redPackApplication;
    private readonly IIndustryRepository _industryRepository;
    private readonly ISystemDicDataCacheManager _sysDic;
    private readonly ISnapshotSMSTemplateRepository _snapshotSMSTemplateRepository;
    private readonly IOrderSnapshotRepository _orderSnapshotRepository;
    private readonly IOrderRepository _orderRepository;

    public RedPackController(IRedPackApplication redPackApplication, IIndustryRepository industryRepository, ISystemDicDataCacheManager sysDic, ISnapshotSMSTemplateRepository snapshotSMSTemplateRepository, IOrderSnapshotRepository orderSnapshotRepository, IOrderRepository orderRepository)
    {
        _redPackApplication = redPackApplication;
        _industryRepository = industryRepository;
        _sysDic = sysDic;
        _snapshotSMSTemplateRepository = snapshotSMSTemplateRepository;
        _orderSnapshotRepository = orderSnapshotRepository;
        _orderRepository = orderRepository;
    }

    #region 特殊红包
    /// <summary>
    /// 特殊红包审批集合
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("special")]
    public async Task<PagedDto<SnapshotOrderAuditItemsOutDto>> GetRedPackSpecialAuditItems([FromQuery] SnapshotOrderAuditItemsInDto dto)
        => (await _redPackApplication.GetRedPackSpecialAuditItems(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 特殊红包审核通过或拒绝
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("audit/special")]
    public async Task UpdateRedPackSpecialAuditAsync([FromBody] UpdateRedPackSpecialAuditInDto dto)
        => await _redPackApplication.AuditRedPackSpecialAuditAsync(dto);

    /// <summary>
    /// 特殊红包审核详情信息(OrderId)
    /// </summary>
    /// <returns></returns>
    [HttpGet("audit/special/{id}")]
    public async Task<SnapshotOrderAuditSpecialDetailOutDto> GetRedPackSpecialAuditBaseDataAsync(string id)
    {
        return await _redPackApplication.GetRedPackSpecialAuditBaseDataAsync(id);
    }

    /// <summary>
    /// 撤销特殊红包审批
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("audit/special/revocation")]
    public async Task RevocationRedPackSpecialAuditAsync([FromBody] IList<string> ids)
        => await _redPackApplication.RevocationRedPackSpecialAuditAsync(ids);

    #endregion

    #region 红包审核
    /// <summary>
    /// 获取市民红包审批列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("audit")]
    public async Task<PagedDto<SnapshotOrderAuditItemsOutDto>> GetRedPackAuditItems([FromQuery] SnapshotOrderAuditItemsInDto dto)
        => (await _redPackApplication.GetRedPackAuditItems(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 获取网格员红包审批集合(政法委或应急管理局:根据账号登录返回数据)
    /// </summary>
    /// <returns></returns>
    [HttpGet("audit/guider")]
    public async Task<PagedDto<SnapshotOrderGuiderAuditItemsOutDto>> GetRedPackGuiderAuditItems([FromQuery] SnapshotOrderGuiderAuditItemsInDto dto)
        => (await _redPackApplication.GetRedPackGuiderAuditItems(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 获取网格员审核详情
    /// </summary>
    /// <param name="id">工单Id</param>
    /// <returns></returns>
    [HttpGet("audit/guider/{id}")]
    public async Task<SnapshotOrderAuditDetailOutDto> GetRedPackGuiderAuditDetailAsync(string id)
        => await _redPackApplication.GetRedPackGuiderAuditDetailAsync(id);

    /// <summary>
    /// 网格员红包审核通过或拒绝
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("audit/guider")]
    public async Task UpdateRedPackGuiderAuditAsync([FromBody] UpdateRedPackGuiderAuditInDto dto)
        => await _redPackApplication.AuditRedPackGuiderAuditAsync(dto);

    /// <summary>
    /// 获取审核市民详情
    /// </summary>
    /// <param name="id">工单Id</param>
    /// <returns></returns>
    [HttpGet("audit/{id}")]
    public async Task<SnapshotOrderAuditDetailOutDto> GetRedPackAuditDetailAsync(string id)
        => await _redPackApplication.GetRedPackAuditDetailAsync(id);

    /// <summary>
    /// 获取特提(退回)参数
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("audit/back/{id}")]
    public async Task<GetAuditBackBaseDataOutDto> GetAuditBackBaseDataAsync(string id)
        => await _redPackApplication.GetAuditBackBaseDataAsync(id);

    /// <summary>
    /// 添加备注
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("audit/remark")]
    public async Task UpdateRedPackAuditRemarkAsync([FromBody] UpdateRedPackAuditRemarkInDto dto)
        => await _redPackApplication.UpdateRedPackAuditRemarkAsync(dto);

    /// <summary>
    /// 添加备注页面基础信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("audit/remark/basedata")]
    public async Task<Dictionary<string, object>> UpdateRedPackAuditRemarkBaseDataAsync()
    {
        return new Dictionary<string, object>()
        {
            { "failCase", EnumExts.GetDescriptions<ERedPackPickupFailCase>() }
        };
    }

    /// <summary>
    /// 添加补充发放信息页面基础信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("record/basedata/{id}")]
    public async Task<GetRedPackRecordBaseDataOutDto> UpdateRedPackRecordBaseDataAsync(string id)
    {
        return await _redPackApplication.GetRedPackRecordBaseDataAsync(id);
    }

    /// <summary>
    /// 添加补充发放信息
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("record")]
    [LogFilterAlpha("添加红包补充发放信息")]
    public async Task UpdateRedPackRecordAsync([FromBody] UpdateRedPackRecordInDto dto)
        => await _redPackApplication.UpdateRedPackRecordAsync(dto);

    /// <summary>
    /// 添加补充发放信息(特殊红包)
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("record/special")]
    [LogFilterAlpha("添加特殊红包补充发放信息")]
    public async Task UpdateRedPackSpecialRecordAsync([FromBody] UpdateRedPackRecordInDto dto)
        => await _redPackApplication.UpdateRedPackSpecialRecordAsync(dto);

    /// <summary>
    /// 补充发放集合基础信息
    /// </summary>
    [HttpGet("record/supplement/basedata")]
    public async Task<Dictionary<string, object>> GetRedPackRecordSupplementBaseDataAsync()
    {
        return new Dictionary<string, object>()
        {
            { "industry", await _industryRepository.GetDataBaseAsync() }
        };
    }

    /// <summary>
    /// 补充发放集合
    /// </summary>
    [HttpGet("record/supplement")]
    public async Task<PagedDto<SnapshotRedPackRecordSupplementItemsOutDto>> GetRedPackRecordSupplementItems([FromQuery] SnapshotRedPackRecordSupplementItemsInDto dto)
        => (await _redPackApplication.GetRedPackRecordSupplementItems(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 红包审核通过或拒绝
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("audit")]
    public async Task UpdateRedPackAuditAsync([FromBody] UpdateRedPackAuditInDto dto)
        => await _redPackApplication.AuditRedPackAuditAsync(dto, HttpContext.RequestAborted);

    /// <summary>
    /// 获取审核短信模板
    /// </summary>
    /// <returns></returns>
    [HttpGet("audit/sms_template")]
    public async Task<IList<GetRedPackAuditSMSTemplateOutDto>> GetRedPackAuditSMSTemplateAsync([FromQuery] GetRedPackAuditSMSTemplateInDto dto)
        => await _redPackApplication.GetRedPackAuditSMSTemplateAsync(dto);

    /// <summary>
    /// 红包审批页面基础数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("audit/basedata")]
    public async Task<Dictionary<string, object>> GetAuditBaseDataAsync()
    {
        var industry = await _industryRepository.GetDataBaseAsync();

        var configAmount = await _industryRepository.Queryable()
            .Select(m => m.CitizenReadPackAmount)
            .Distinct()
            .ToListAsync();
        var sms = await _snapshotSMSTemplateRepository.Queryable()
            .ToListAsync();
        return new Dictionary<string, object>
        {
            { "industry", industry },
            { "configAmount",  configAmount },
            { "status", EnumExts.GetDescriptions<ERedPackAuditStatus>() }
        };
    }
    #endregion

    #region 红包发放记录

    /// <summary>
    /// 市民红包发放记录
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("record")]
    public async Task<PagedDto<SnapshotRedPackRecordItemsOutDto>> GetRedPackRecordItems([FromQuery] SnapshotRedPackRecordItemsInDto dto)
        => (await _redPackApplication.GetRedPackRecordItems(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 网格员红包发放记录
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("record/guider")]
    public async Task<PagedDto<SnapshotRedPackRecordItemsGuiderOutDto>> GetRedPackRecordGuiderItems([FromQuery] SnapshotRedPackRecordItemsGuiderInDto dto)
        => (await _redPackApplication.GetRedPackRecordGuiderItems(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 红包发放明细基础数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("record/send/database")]
    public async Task<Dictionary<string, object>> GetRedPackRecordSendBaseDataAsync()
    {
        return new Dictionary<string, object>()
        {
            { "industry", await _industryRepository.Queryable()
            .Select(m => new { m.Id, m.Name })
            .ToListAsync()},
            { "sendStatus", EnumExts.GetDescriptions<EReadPackSendStatus>().Where(m => m.Key != 2).ToList() },
            { "userType", EnumExts.GetDescriptions<EReadPackUserType>() }
        };
    }

    /// <summary>
    /// 红包发放明细
    /// </summary>
    /// <returns></returns>
    [HttpGet("record/send")]
    public async Task<PagedDto<SnapshotRedPackRecordSendOutDto>> GetRedPackRecordDetail([FromQuery] SnapshotRedPackRecordSendInDto dto)
        => (await _redPackApplication.GetRedPackRecordDetail(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 批量发送红包
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpPut("record/send_bath")]
    public async Task<string> BathSendRedPackAsync([FromBody] List<string> ids)
    {
        return "ok";
    }
    #endregion
}
