using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using Tiku.Application.Growth;

namespace Tiku.Api.Contracts;

/// <summary>
///     新增或更新CRM配置请求 DTO。
/// </summary>
public sealed class UpsertCrmConfigDto
{
    /// <summary>
    ///     是否启用。
    /// </summary>
    public bool Enabled { get; set; }

    /// <summary>
    ///     访问地址。
    /// </summary>
    [StringLength(2048)]
    public string? Url { get; set; }

    /// <summary>
    ///     密钥引用。
    /// </summary>
    [StringLength(300)]
    public string? SecretRef { get; set; }

    /// <summary>
    ///     密钥内容。
    /// </summary>
    public string? Secret { get; set; }

    /// <summary>
    ///     表单名称。
    /// </summary>
    [StringLength(200)]
    public string? FormName { get; set; }

    /// <summary>
    ///     考试类型。
    /// </summary>
    [StringLength(100)]
    public string? ExamType { get; set; }

    /// <summary>
    ///     超时时长，单位为秒。
    /// </summary>
    [Range(1, 120)]
    public int? TimeoutSeconds { get; set; }

    /// <summary>
    ///     延迟秒数。
    /// </summary>
    [Range(0, 86400)]
    public int? DelaySeconds { get; set; }

    /// <summary>
    ///     分配模式。
    /// </summary>
    [StringLength(50)]
    public string? AssignmentMode { get; set; }

    /// <summary>
    ///     分配池。
    /// </summary>
    public JsonElement? AssignmentPool { get; set; }

    /// <summary>
    ///     分配配置。
    /// </summary>
    public JsonElement? AssignmentConfig { get; set; }

    public UpsertCrmConfigCommand ToCommand()
    {
        return new UpsertCrmConfigCommand(
            Enabled,
            Url,
            SecretRef,
            Secret,
            FormName,
            ExamType,
            TimeoutSeconds,
            DelaySeconds,
            AssignmentMode,
            AssignmentPool,
            AssignmentConfig);
    }
}

/// <summary>
///     CRM队列查询参数。
/// </summary>
public sealed class CrmQueueQueryDto
{
    /// <summary>
    ///     状态。
    /// </summary>
    [StringLength(50)]
    public string? Status { get; set; }

    /// <summary>
    ///     队列任务 ID。
    /// </summary>
    public Guid? QueueId { get; set; }

    /// <summary>
    ///     来源。
    /// </summary>
    [StringLength(200)]
    public string? Source { get; set; }

    /// <summary>
    ///     返回数量上限。
    /// </summary>
    [Range(1, 500)]
    public int? Limit { get; set; }

    public CrmQueueQuery ToQuery()
    {
        return new CrmQueueQuery(Status, QueueId, Source, Limit);
    }
}

/// <summary>
///     CRM队列日志查询参数。
/// </summary>
public sealed class CrmQueueLogQueryDto
{
    /// <summary>
    ///     队列任务 ID。
    /// </summary>
    public Guid? QueueId { get; set; }

    /// <summary>
    ///     返回数量上限。
    /// </summary>
    [Range(1, 200)]
    public int? Limit { get; set; }

    public CrmQueueLogQuery ToQuery()
    {
        return new CrmQueueLogQuery(QueueId, Limit);
    }
}

/// <summary>
///     CRM队列Action请求 DTO。
/// </summary>
public sealed class CrmQueueActionDto
{
    /// <summary>
    ///     队列任务 ID。
    /// </summary>
    [Required]
    public Guid QueueId { get; set; }

    /// <summary>
    ///     操作。
    /// </summary>
    [StringLength(50)]
    public string? Action { get; set; }

    /// <summary>
    ///     备注。
    /// </summary>
    [StringLength(500)]
    public string? Note { get; set; }

    /// <summary>
    ///     扩展元数据。
    /// </summary>
    public JsonElement? Metadata { get; set; }

    public CrmQueueActionCommand ToCommand()
    {
        return new CrmQueueActionCommand(QueueId, Action, Note, Metadata);
    }
}