using Hotline.Application.Snapshot.Contracts;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Share.Tools;
using Hotline.Snapshot;
using Hotline.Snapshot.IRepository;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;

namespace Hotline.Api.Controllers.Snapshot;

/// <summary>
/// 邀请码管理
/// </summary>
[Description("邀请码管理")]
public class InviteCodeController : BaseController
{
    private readonly IInviteCodeApplication _inviteCodeApplication;

    public InviteCodeController(IInviteCodeApplication inviteCodeApplication)
    {
        _inviteCodeApplication = inviteCodeApplication;
    }

    /// <summary>
    /// 获取添加邀请码基础数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("basedata")]
    public async Task<Dictionary<string, object>> GetBasedataAsync()
    {
        return new Dictionary<string, object>
        {
            { "OrgName", await _inviteCodeApplication.GetInviteCodeItems().Where(m => m.ParentOrgId == null)
            .Select(m => new Kv{ Key = m.Id, Value = m.OrgName}).ToListAsync() }
        };
    }

    /// <summary>
    /// 获取邀请码集合
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<PagedDto<InviteCode>> GetInviteCodeItems([FromQuery] GetInviteCodeItemsInDto dto)
        => (await _inviteCodeApplication.GetInviteCodeItems().ToPagedListAsync(dto)).ToPaged();


    /// <summary>
    /// 删除邀请码
    /// </summary>
    /// <returns></returns>
    [HttpDelete]
    public async Task DeleteInviteCodeAsync([FromBody] IList<string> ids)
        => await _inviteCodeApplication.DeleteInviteCodeAsync(ids);

    /// <summary>
    /// 邀请码详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>

    [HttpGet("{id}")]
    public async Task<InviteCode> GetInviteCodeDetailAsync( string id)
        => await _inviteCodeApplication.GetInviteCodeItems().Where(m => m.Id == id).FirstAsync();

    /// <summary>
    /// 修改邀请码
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut]
    public async Task UpdateInviteCodeAsync([FromBody] UpdateInviteCodeInDto dto)
        => await _inviteCodeApplication.UpdateInviteCodeAsync(dto);

    /// <summary>
    /// 添加部门邀请码
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task AddInviteCodeAsync([FromBody] AddInviteCodeInDto dto)
        => await _inviteCodeApplication.AddInviteCodeAsync(dto);

    /// <summary>
    /// 邀请码统计
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("statistic")]
    public async Task<IList<InviteCodeStatisticOutDto>> GetInviteCodeStatisticAsync([FromQuery] GetInviteCodeStatisticInDto dto)
        => await _inviteCodeApplication.GetInviteCodeStatisticAsync(dto).ToListAsync();

    /// <summary>
    /// 邀请码统计明细
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("statistic/detail")]
    public async Task<PagedDto<InviteCodeStatisticDetailOutDto>> GetInviteCodeStatisticDetail([FromQuery] GetInviteCodeStatisticDetailInDto dto)
        => (await _inviteCodeApplication.GetInviteCodeStatisticDetail(dto).ToPagedListAsync(dto)).ToPaged();
}
