using Hotline.Ai.Visit;
using Hotline.Share.Dtos;
using Hotline.Share.Enums.Order;
using Hotline.Users;
using SqlSugar;
using System.ComponentModel;
using XF.Domain.Repository;

namespace Hotline.Orders;

/// <summary>
/// 已回访工单
/// </summary>
[Description("工单回访")]
[SugarIndex("index_order_no", nameof(OrderVisit.No), OrderByType.Desc)]
[SugarIndex("index_visit_orderId", nameof(OrderVisit.OrderId), OrderByType.Asc)]
public class OrderVisit : CreationEntity
{
    /// <summary>
    /// 工单编码（冗余）
    /// </summary>
    public string No { get; set; }

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

    /// <summary>
    /// 工单
    /// </summary>
    [Navigate(NavigateType.OneToOne, nameof(OrderId))]
    public Order Order { get; set; }

    /// <summary>
    /// 回访状态
    /// </summary>
    public EVisitState VisitState { get; set; }

    /// <summary>
    /// 回访方式
    /// </summary>
    [SugarColumn(IsNullable = true)]
    public EVisitType? VisitType { get; set; }

    /// <summary>
    /// 发布时间
    /// </summary>
    public DateTime PublishTime { get; set; }

    /// <summary>
    /// 回访人
    /// </summary>
    [SugarColumn(IsNullable = true)]
    public string? EmployeeId { get; set; }

    /// <summary>
    /// 回访人（对象）
    /// </summary>
    [Navigate(NavigateType.OneToOne, nameof(EmployeeId))]
    public User? Employee { get; set; }

    /// <summary>
    /// 是否接通
    /// </summary>
    [SugarColumn(IsNullable = true)]
    public bool? IsPutThrough { get; set; }

    /// <summary>
    /// 重办状态
    /// </summary>
    [SugarColumn(IsNullable = true)]
    public EAgainState? AgainState { get; set; }

    /// <summary>
    /// 回访时间
    /// </summary>
    [SugarColumn(IsNullable = true)]
    public DateTime? VisitTime { get; set; }

    /// <summary>
    /// 回访明细
    /// </summary>
    [Navigate(NavigateType.OneToMany, nameof(OrderVisitDetail.VisitId))]
    public List<OrderVisitDetail> OrderVisitDetails { get; set; }

    /// <summary>
    /// 当前评价结果
    /// </summary>
    [SugarColumn(ColumnDataType = "json", IsJson = true, IsNullable = true)]
    public Kv? NowEvaluate { get; set; }

    /// <summary>
    /// 是否可以处理
    /// </summary>
    public bool IsCanHandle { get; set; }

    /// <summary>
    /// 是否可以AI回访
    /// </summary>
    public bool? IsCanAiVisit { get; set; }

    /// <summary>
    /// 渠道为电话时，此字段存在
    /// </summary>
    public string? CallId { get; set; }

    /// <summary>
    /// 第一次电话回访拨打时间
    /// </summary>
    public DateTime? FirstVisitTime { get; set; }

    /// <summary>
    /// 最后一次电话回访拨打时间
    /// </summary>
    public DateTime? LastVisitTime { get; set; }

    /// <summary>
    /// 智能回访次数
    /// </summary>
    [SugarColumn(DefaultValue = "0")]
    public int AiVisitCount { get; set; }

    /// <summary>
    /// 智能回访录音地址
    /// </summary>
    public string? RecordUrl { get; set; }

    /// <summary>
    /// 部门扭转
    /// </summary>
    public bool? OrgJudge { get; set; }
    /// <summary>
    /// 坐席扭转
    /// </summary>
    public bool? SeatJudge { get; set; }
    /// <summary>
    /// 评判状态
    /// </summary>
    public EJudgeState? JudgeState { get; set; }
    /// <summary>
    /// 评判意见
    /// </summary>
    public string? JudgeContent { get; set; }

    /// <summary>
    /// 评判人Id
    /// </summary>
    public string? JudgeUserId { get; set; }

    /// <summary>
    /// 评判人名称
    /// </summary>
    public string? JudgeUserName { get; set; }

    /// <summary>
    /// 评判时间
    /// </summary>
    public DateTime? JudgeTime { get; set; }

    /// <summary>
    /// 是否有效智能回访
    /// </summary>
    public bool? IsEffectiveAiVisit { get; set; }

    /// <summary>
    /// 是否批量回访
    /// </summary>
    public bool? IsBatchVisit { get; set; }

    /// <summary>
    /// 是否回访结果修改
    /// </summary>
    public bool? IsUpdate { get; set; }

    /// <summary>
    /// 语音段列表信息
    /// </summary>
    [SugarColumn(ColumnDataType = "json", IsJson = true, IsNullable = true)]
    public List<SentenceList>? SentenceList { get; set; }

    public void AiVisitTime()
    {
        LastVisitTime = DateTime.Now;
        if (FirstVisitTime is null)
        {
            FirstVisitTime = LastVisitTime;
        }
    }

}