using Amazon.Runtime.Internal.Transform;
using Hotline.Application.Snapshot.Contracts;
using Hotline.Caching.Interfaces;
using Hotline.Configurations;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Settings;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Settings;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Share.Tools;
using Hotline.Snapshot;
using Hotline.Snapshot.IRepository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using SqlSugar;

namespace Hotline.Api.Controllers.Snapshot;

/// <summary>
/// 随手拍行业管理接口
/// </summary>
public class IndustryController : BaseController
{
    private readonly IIndustryRepository _industryRepository;
    private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;
    private readonly IIndustryApplication _industryApplication;
    private readonly ISystemAreaDomainService _systemAreaDomainService;
    private readonly IOptionsSnapshot<AppConfiguration> _appOptions;
    private readonly ISystemOrganizeRepository _systemOrganizeRepository;

    public IndustryController(IIndustryRepository industryRepository, IIndustryApplication industryApplication, ISystemDicDataCacheManager systemDicDataCacheManager, ISystemAreaDomainService systemAreaDomainService, IOptionsSnapshot<AppConfiguration> appOptions, ISystemOrganizeRepository systemOrganizeRepository)
    {
        _industryRepository = industryRepository;
        _industryApplication = industryApplication;
        _systemDicDataCacheManager = systemDicDataCacheManager;
        _systemAreaDomainService = systemAreaDomainService;
        _appOptions = appOptions;
        _systemOrganizeRepository = systemOrganizeRepository;
    }

    /// <summary>
    /// 添加行业页面基础数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("basedata")]
    public async Task<Dictionary<string, object>> GetBaseData()
    {
        return new Dictionary<string, object>
        {
            { "department", await _systemOrganizeRepository.GetOrgEnabled() },
            { "acceptType", _systemDicDataCacheManager.AcceptType},
            { "bulletinType", _systemDicDataCacheManager.SnapshotBulletinType}
        };
    }
    /// <summary>
    /// 新增行业
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<string> AddIndustry([FromBody] AddIndustryDto dto)
        => await _industryApplication.AddIndustryAsync(dto, HttpContext.RequestAborted);

    /// <summary>
    /// 行业详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<IndustryDetailOutDto> GetIndustryDetailAsync(string id)
        => await _industryApplication.GetIndustryDetailAsync(id, HttpContext.RequestAborted);

    /// <summary>
    /// 获取行业集合
    /// </summary>
    /// <returns></returns>
    [HttpGet("industry")]
    public async Task<PagedDto<IndustryItemsOutDto>> GetIndustres([FromQuery] IndustryListInDto dto)
        => (await _industryApplication.GetIndustres(dto).ToPagedListAsync(dto, HttpContext.RequestAborted)).ToPaged();

    /// <summary>
    /// 修改行业
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut]
    public async Task UpdateIndustry([FromBody] UpdateIndustryInDto dto)
        => await _industryApplication.UpdateIndustryAsync(dto, HttpContext.RequestAborted);

    #region 行业线索
    /// <summary>
    /// 行业线索集合
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("case")]
    public async Task<PagedDto<IndustryCaseItemOutDto>> GetIndustryCaseItems([FromQuery] IndustryCaseItemInDto dto)
        => (await _industryApplication.GetIndustryCaseItems(dto).ToPagedListAsync(dto, HttpContext.RequestAborted)).ToPaged();

    /// <summary>
    /// 添加行业线索
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("case")]
    public async Task AddIndustryCaseAsync([FromBody] AddIndustryCaseDto dto)
        => await _industryApplication.AddIndustryCaseAsync(dto);

    /// <summary>
    /// 更新行业线索
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("case")]
    public async Task UpdateIndustryCaseAsync([FromBody] UpdateIndustryCaseDto dto)
        => await _industryApplication.UpdateIndustryCaseAsync(dto);

    /// <summary>
    /// 获取行业线索详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("case/{id}")]
    public async Task<IndustryCase> GetIndustryCaseDetailAsync(string id)
        => await _industryApplication.GetIndustryCaseAsync(id);

    /// <summary>
    /// 页面基础数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("case/database")]
    public async Task<Dictionary<string, object>> GetIndustryCaseDataBaseAsync()
    {
        var items = await _industryRepository.Queryable()
            .Select(m => new { m.Id, m.Name })
            .ToListAsync();
        return new Dictionary<string, object>
        {
            { "industry", items }
        };
    }
    #endregion

    #region 行业短信模板
    /// <summary>
    /// 行业短信模板集合
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("sms_template")]
    public async Task<PagedDto<SnapshotSMSTemplateItemsOutDto>> GetSMSTemplates([FromQuery] SnapshotSMSTemplateItemsInDto dto)
        => (await _industryApplication.GetSMSTemplates(dto).ToPagedListAsync(dto, HttpContext.RequestAborted)).ToPaged();

    /// <summary>
    /// 添加行业短信模板
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("sms_template")]
    public async Task AddSmsTemplateAsync([FromBody] AddSnapshotSMSTemplateInDto dto)
        => await _industryApplication.AddSMSTemplateAsync(dto);

    /// <summary>
    /// 修改行业短信模板
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("sms_template")]
    public async Task UpdateSmsTemplateAsync([FromBody] UpdateSnapshotSMSTemplateInDto dto)
        => await _industryApplication.UpdateSMSTemplateAsync(dto);

    /// <summary>
    /// 短信详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("sms_template/{id}")]
    public async Task<SnapshotSMSTemplateItemsOutDto> GetSMSTemplateDetailAsync(string id)
        => await _industryApplication.GetSMSTemplateDetailAsync(id);

    /// <summary>
    /// 删除短信模板
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete("sms_template")]
    public async Task DeleteSmsTemplateAsync([FromBody] IList<string> ids)
        => await _industryApplication.DeleteSMSTemplateAsync(ids);
    #endregion

    #region 区域从业人员
    /// <summary>
    /// 区域从业人员集合
    /// </summary>
    /// <returns></returns>
    [HttpGet("practitioner")]
    public async Task<PagedDto<PractitionerItemsOutDto>> GetPractitionerItems([FromQuery] PractitionerItemsInDto dto)
        => (await _industryApplication.GetPractitionerItems(dto).ToPagedListAsync(dto, HttpContext.RequestAborted)).ToPaged();

    /// <summary>
    /// 添加区域从业人员
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("practitioner")]
    public async Task<string> AddPractitionerAsync([FromBody] AddPractitionerInDto dto)
        => await _industryApplication.AddPractitionerAsync(dto);

    /// <summary>
    /// 删除区域从业人员
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete("practitioner")]
    public async Task DeletePractitionerAsync([FromBody] IList<string> ids)
        => await _industryApplication.DeletePractitionerAsync(ids);

    /// <summary>
    /// 修改区域从业人员
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("practitioner")]
    public async Task UpdatePractitionerAsync(UpdatePractitionerInDto dto)
        => await _industryApplication.UpdatePractitionerAsync(dto);

    /// <summary>
    /// 从业人员详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("practitioner/{id}")]
    public async Task<PractitionerItemsOutDto> GetPractitionerAsync(string id)
        => await _industryApplication.GetPractitionerAsync(id);

    /// <summary>
    /// 添加从业人员基础数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("practitioner/basedata")]
    public async Task<Dictionary<string, object>> GetPractitionerDataBaseAsync()
    {
        var parentId = "";
        if (_appOptions.Value.IsZiGong) parentId = _appOptions.Value.ZiGong.AreaCode;
        return new Dictionary<string, object>
        {
            { "area", await _systemAreaDomainService.GetAreaTree(parentId: parentId)}
        };
    }
    #endregion

    #region 志愿者
    /// <summary>
    /// 志愿者集合
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("volunteer")]
    public async Task<PagedDto<VolunteerItemsOutDto>> GetVolunteerItems([FromQuery] VolunteerItemsInDto dto)
        => (await _industryApplication.GetVolunteerItems(dto).ToPagedListAsync(dto, HttpContext.RequestAborted)).ToPaged();

    /// <summary>
    /// 添加志愿者
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("volunteer")]
    public async Task<string> AddVolunteerAsync([FromBody] AddVolunteerInDto dto)
        => await _industryApplication.AddVolunteerAsync(dto);

    /// <summary>
    /// 批量删除志愿者
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete("volunteer")]
    public async Task DeleteVolunteerAsync([FromBody] IList<string> ids)
        => await _industryApplication.DeleteVolunteerAsync(ids);

    /// <summary>
    /// 志愿者详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("volunteer/{id}")]
    public async Task<Volunteer> GetVolunteerAsync(string id)
        => await _industryApplication.GetVolunteerAsync(id);

    /// <summary>
    /// 修改志愿者
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("volunteer")]
    public async Task UpdateVolunteerAsync([FromBody] UpdateVolunteerInDto dto)
        => await _industryApplication.UpdateVolunteerAsync(dto);

    /// <summary>
    /// 志愿者上报集合
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("volunteer/report")]
    public async Task<PagedDto<VolunteerReportItemsOutDto>> GetVolunteerReportItems([FromQuery] VolunteerReportItemsInDto dto)
        => (await _industryApplication.GetVolunteerReportItems(dto).ToPagedListAsync(dto)).ToPaged();
    #endregion

    #region 修改行业记录
    /// <summary>
    /// 行业修改记录
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("order/industry/log")]
    public async Task<PagedDto<IndustryLogItemsOutDto>> GetIndustryLogItems([FromQuery] IndustryLogItemsInDto dto)
        => (await _industryApplication.GetIndustryLogItems(dto).ToPagedListAsync(dto)).ToPaged();
    #endregion
}
