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

namespace Tiku.Api.Contracts;

/// <summary>
///     视频Search查询参数。
/// </summary>
public sealed class VideoSearchQueryDto
{
    /// <summary>
    ///     关键字。
    /// </summary>
    [StringLength(100)]
    public string? Keyword { get; set; }

    /// <summary>
    ///     科目 ID。
    /// </summary>
    public Guid? SubjectId { get; set; }

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

    public VideoSearchQuery ToQuery()
    {
        return new VideoSearchQuery(Keyword, SubjectId, Limit);
    }
}

/// <summary>
///     视频Play请求 DTO。
/// </summary>
public sealed class VideoPlayDto
{
    /// <summary>
    ///     视频 ID。
    /// </summary>
    [Required]
    public Guid VideoId { get; set; }

    /// <summary>
    ///     题目 ID。
    /// </summary>
    public Guid? QuestionId { get; set; }

    public VideoPlayCommand ToCommand()
    {
        return new VideoPlayCommand(VideoId, QuestionId);
    }
}

/// <summary>
///     视频Progress请求 DTO。
/// </summary>
public sealed class VideoProgressDto
{
    /// <summary>
    ///     视频 ID。
    /// </summary>
    [Required]
    public Guid VideoId { get; set; }

    /// <summary>
    ///     题目 ID。
    /// </summary>
    public Guid? QuestionId { get; set; }

    /// <summary>
    ///     播放位置，单位为秒。
    /// </summary>
    [Range(0, int.MaxValue)]
    public int PositionSeconds { get; set; }

    /// <summary>
    ///     时长，单位为秒。
    /// </summary>
    [Range(0, int.MaxValue)]
    public int? DurationSeconds { get; set; }

    /// <summary>
    ///     已观看时长，单位为秒。
    /// </summary>
    [Range(0, int.MaxValue)]
    public int? WatchedSeconds { get; set; }

    /// <summary>
    ///     是否已完成。
    /// </summary>
    public bool? IsCompleted { get; set; }

    /// <summary>
    ///     扩展元数据。
    /// </summary>
    public JsonElement Metadata { get; set; } = JsonSerializer.SerializeToElement(new { });

    public VideoProgressCommand ToCommand()
    {
        return new VideoProgressCommand(
            VideoId,
            QuestionId,
            PositionSeconds,
            DurationSeconds,
            WatchedSeconds,
            IsCompleted,
            Metadata);
    }
}

/// <summary>
///     题目视频查询参数。
/// </summary>
public sealed class QuestionVideoQueryDto
{
    /// <summary>
    ///     题目 ID。
    /// </summary>
    public Guid? QuestionId { get; set; }

    /// <summary>
    ///     题目 ID 列表。
    /// </summary>
    [MaxLength(100)]
    public IReadOnlyCollection<Guid>? QuestionIds { get; set; }

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

    public QuestionVideoQuery ToQuery()
    {
        return new QuestionVideoQuery(QuestionId, QuestionIds, Limit);
    }
}