using System.Text.Json;
using Tiku.Application.Jobs;

namespace Tiku.Api.Contracts;

/// <summary>
/// 创建租户后台任务请求。
/// </summary>
public sealed class CreateBackgroundJobDto
{
    /// <summary>
    /// 任务类型。
    /// </summary>
    public string JobType { get; set; } = string.Empty;
    /// <summary>
    /// 任务载荷。
    /// </summary>
    public JsonElement Payload { get; set; }
    /// <summary>
    /// 计划运行时间。
    /// </summary>
    public DateTimeOffset? RunAfter { get; set; }
    /// <summary>
    /// 最大重试次数。
    /// </summary>
    public int MaxRetries { get; set; } = 3;

    public CreateBackgroundJobCommand ToCommand(Guid tenantId)
    {
        return new CreateBackgroundJobCommand(
            tenantId,
            JobType,
            Payload.ValueKind == JsonValueKind.Undefined ? JsonSerializer.SerializeToElement(new { }) : Payload,
            RunAfter,
            MaxRetries);
    }
}
