using Hotline.Share.Dtos.File;
using Hotline.Share.Dtos.Planlibrary;
using Hotline.Share.Enums.Caselibrary;
using Hotline.Share.Enums.Planlibrary;
using Hotline.Share.Requests;

namespace Hotline.Share.Dtos.Caselibrary
{
    /// <summary>
    /// 案例库列表
    /// </summary>
    public record CaseListDto : PagedRequest
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 关键词
        /// </summary>
        public string? Keywords { get; set; }

        /// <summary>
        /// 摘要
        /// </summary>
        public string? Abstract { get; set; }

        /// <summary>
        /// 案例描述
        /// </summary>
        public string? Describe { get; set; }

        /// <summary>
        /// 案例结果
        /// </summary>
        public string? Result { get; set; }

        /// <summary>
        /// 推荐理由
        /// </summary>
        public string? Reason { get; set; }

        /// <summary>
        /// 是否热门
        /// </summary>
        public bool? IsPopular { get; set; }

        /// <summary>
        /// 案例状态
        /// </summary>
        public ECaseStatus? Status { get; set; }

        /// <summary>
        /// 分类ID
        /// </summary>
        public string? CaseTypeID { get; set; }

        /// <summary>
        /// 工单ID
        /// </summary>
        public string? OrderId { get; set; }

        /// <summary>
        /// 知识库ID
        /// </summary>
        public string? KnowledgeId { get; set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        public bool? IsDeleted { get; set; } = false;

        /// <summary>
        /// 阅读次数
        /// </summary>
        public int PageView { get; set; } = 0;

        /// <summary>
        /// 创建开始时间
        /// </summary>
        public DateTime? CreationTimeStart { get; set; }

        /// <summary>
        /// 创建结束时间
        /// </summary>
        public DateTime? CreationTimeEnd { get; set; }

        /// <summary>
        /// 上架开始时间
        /// </summary>
        public DateTime? OnShelfTimeStart { get; set; }
        /// <summary>
        /// 上架结束时间
        /// </summary>
        public DateTime? OnShelfTimeEnd { get; set; }

        /// <summary>
        /// 下架开始时间
        /// </summary>
        public DateTime? OffShelfTimeStart { get; set; }

        /// <summary>
        /// 下架结束时间
        /// </summary>
        public DateTime? OffShelfTimeEnd { get; set; }

        /// <summary>
        /// 更新开始时间
        /// </summary>
        public DateTime? UpdateTimeStart { get; set; }

        /// <summary>
        /// 更新结束时间
        /// </summary>
        public DateTime? UpdateTimeEnd { get; set; }

        /// <summary>
        /// 审核人ID
        /// </summary>
        public string? ExaminManId { get; set; }

        /// <summary>
        /// 审核部门ID
        /// </summary>
        public string? ExaminOrganizeId { get; set; }

        /// <summary>
        /// 当前部门ID
        /// </summary>
        public string CreateOrgId { get; set; }

        /// <summary>
        /// 审核开始时间
        /// </summary>
        public DateTime? ExaminTimeStart { get; set; }

        /// <summary>
        /// 审核结束时间
        /// </summary>
        public DateTime? ExaminTimeEnd { get; set; }

        /// <summary>
        /// 失效时间
        /// </summary>
        public DateTime? ExpiredTime { get; set; }

        /// <summary>
        /// 评分
        /// </summary>
        public decimal? Score { get; set; } = decimal.Zero;

        /// <summary>
        /// 排序(需要被排序的字段名)
        /// </summary>
        public string? SortField { get; set; }

        /// <summary>
        /// 排序方向 (0: 升序Asc; 1: 降序Desc)
        /// </summary>
        public int OrderByType { get; set; } = 1;
    }


    public record AddCaseListDto
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 关键词
        /// </summary>
        public string? Keywords { get; set; }

        /// <summary>
        /// 摘要
        /// </summary>
        public string? Abstract { get; set; }

        /// <summary>
        /// 案例描述
        /// </summary>
        public string Describe { get; set; }

        /// <summary>
        /// 案例结果
        /// </summary>
        public string Result { get; set; }

        /// <summary>
        /// 推荐理由
        /// </summary>
        public string Reason { get; set; }

        /// <summary>
        /// 是否热门
        /// </summary>
        public bool? IsPopular { get; set; } = false;

        /// <summary>
        /// 工单ID
        /// </summary>
        public string? OrderId { get; set; }

        /// <summary>
        /// 知识库ID
        /// </summary>
        public string? KnowledgeId { get; set; }

        /// <summary>
        /// 分类
        /// </summary>
        public List<CaseRelationTypeDto> CaseTypes { get; set; }

        /// <summary>
        /// 失效时间
        /// </summary>
        public DateTime? ExpiredTime { get; set; }

        /// <summary>
        /// 案例状态
        /// </summary>
        public ECaseApplyStatus? ApplyStatus { get; set; }

        /// <summary>
        /// 案例状态
        /// </summary>
        public ECaseStatus? Status { get; set; }

        /// <summary>
        /// 上传附件
        /// </summary>
        public List<FileDto>? Files { get; set; }
    }

    public record UpdateCaseListDto : AddCaseListDto
    {
        /// <summary>
        /// 案例库ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 审核人ID
        /// </summary>
        public string? ExaminManId { get; set; }

        /// <summary>
        /// 审核部门ID
        /// </summary>
        public string? ExaminOrganizeId { get; set; }

        /// <summary>
        /// 审核时间
        /// </summary>
        public DateTime? ExaminTime { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }
        /// <summary>
        /// 申请理由
        /// </summary>
        public string ApplyReason { get; set; }

        /// <summary>
        /// 审核意见
        /// </summary>
        public string ExaminOpinion { get; set; }

        /// <summary>
        /// 上架时间
        /// </summary>
        public DateTime? OnShelfTime { get; set; }

        /// <summary>
        /// 下架时间
        /// </summary>
        public DateTime? OffShelfTime { get; set; }
    }

    public record DelCaseListDto
    {
        /// <summary>
        /// 案例库ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 申请理由
        /// </summary>
        public string ApplyReason { get; set; }
    }


    /// <summary>
    /// 案例库列表
    /// </summary>
    public record CaseInfoDto
    {
        /// <summary>
        /// 标识
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 关键词
        /// </summary>
        public string? Keywords { get; set; }

        /// <summary>
        /// 摘要
        /// </summary>
        public string? Abstract { get; set; }

        /// <summary>
        /// 案例描述
        /// </summary>
        public string Describe { get; set; }

        /// <summary>
        /// 案例结果
        /// </summary>
        public string Result { get; set; }

        /// <summary>
        /// 推荐理由
        /// </summary>
        public string Reason { get; set; }

        /// <summary>
        /// 是否热门
        /// </summary>
        public bool? IsPopular { get; set; }

        /// <summary>
        /// 阅读次数
        /// </summary>
        public int PageView { get; set; } = 0;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreationTime { get; set; }

        /// <summary>
        /// 失效时间
        /// </summary>
        public DateTime? ExpiredTime { get; set; }

        /// <summary>
        /// 评分
        /// </summary>
        public decimal? Score { get; set; } = decimal.Zero;

        /// <summary>
        /// 创建人
        /// </summary>
        public string? CreatorName { get; set; }

        /// <summary>
        /// 创建部门
        /// </summary>
        public string? CreatorOrgName { get; set; }





        /// <summary>
        /// 案例分类
        /// </summary>
        public List<CaseRelationTypeDto> CaseTypes { get; set; }

        /// <summary>
        /// 案例分类名称
        /// </summary>
        public string CaseTypeText => GetCaseTypeText(CaseTypes);

        /// <summary>
        /// 获取案例分类名称
        /// </summary>
        /// <returns></returns>
        public string GetCaseTypeText(List<CaseRelationTypeDto> items)
        {

            if (CaseTypes != null && CaseTypes.Any())
            {
                var names = CaseTypes.Select(x => x.Name).ToList();
                return string.Join(",", names);
            }
            return "";
        }

        /// <summary>
        /// 工单ID
        /// </summary>
        public string OrderId { get; set; }

        /// <summary>
        /// 工单
        /// </summary>
        public string OrderTitle { get; set; }

        /// <summary>
        /// 知识库ID
        /// </summary>
        public string? KnowledgeId { get; set; }

        /// <summary>
        /// 知识库
        /// </summary>
        public string? KnowledgeTitle { get; set; }


        public CaseCollectDto Collect { get; set; }

        /// <summary>
        /// 案例申请状态
        /// </summary>
        public ECaseApplyStatus ApplyStatus { get; set; }

        /// <summary>
        /// 案例状态
        /// </summary>
        public ECaseStatus Status { get; set; }



        public List<FileDto> Files { get; set; }

        /// <summary>
        /// 附件
        /// </summary>
        public List<FileJson>? FileJson { get; set; }
    }

    public class CaseCollectDto
    {
        /// <summary>
        /// 案例库ID
        /// </summary>
        public string CaseId { get; set; }

        /// <summary>
        /// 评分
        /// </summary>
        public decimal? Score { get; set; } = decimal.Zero;

        /// <summary>
        /// 是否收藏
        /// </summary>
        public bool? Collect { get; set; }
    }

    public class CaseApplyReasonDto
    {
        /// <summary>
        /// 案例库ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 申请理由
        /// </summary>
        public string? ApplyReason { get; set; }
    }
    public record AuditCaseListDto
    {
        /// <summary>
        /// 案例库ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 审核状态 0不同意 1同意
        /// </summary>
        public int State { get; set; }

        /// <summary>
        /// 审核意见
        /// </summary>
        public string ExaminOpinion { get; set; }
    }

    public record PvCaseListDto
    {
        /// <summary>
        /// 案例库ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 浏览量：默认不增加，false不增加，true增加浏览量
        /// </summary>
        public bool IsAddPv { get; set; } = false;

        /// <summary>
        /// 评分
        /// </summary>
        public decimal Score { get; set; }
    }
}
