using System.ComponentModel.DataAnnotations;
using Tiku.Application.Content;

namespace Tiku.Api.Contracts;

/// <summary>
/// 内容Navigation查询参数。
/// </summary>
public sealed class ContentNavigationQueryDto
{
    /// <summary>
    /// 租户编码。
    /// </summary>
    [StringLength(100)]
    public string? TenantCode { get; set; }

    /// <summary>
    /// 地区 ID。
    /// </summary>
    public Guid? RegionId { get; set; }

    /// <summary>
    /// 内容入口 ID。
    /// </summary>
    public Guid? EntryId { get; set; }

    /// <summary>
    /// 节点 ID。
    /// </summary>
    public Guid? NodeId { get; set; }

    /// <summary>
    /// 题集 ID。
    /// </summary>
    public Guid? CollectionId { get; set; }

    /// <summary>
    /// 父节点 ID；传 root 表示根节点。
    /// </summary>
    [StringLength(64)]
    [RegularExpression("^(root|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$")]
    public string? ParentId { get; set; }

    /// <summary>
    /// 入口类型。
    /// </summary>
    [StringLength(50)]
    public string? EntryType { get; set; }

    /// <summary>
    /// 题集类型。
    /// </summary>
    [StringLength(50)]
    public string? CollectionType { get; set; }

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

    /// <summary>
    /// 标记类型。
    /// </summary>
    [StringLength(50)]
    public string? MarkerType { get; set; }

    /// <summary>
    /// 关键字。
    /// </summary>
    [StringLength(100)]
    public string? Keyword { get; set; }

    /// <summary>
    /// 是否包含隐藏数据。
    /// </summary>
    public bool IncludeHidden { get; set; }

    /// <summary>
    /// 是否包含停用数据。
    /// </summary>
    public bool IncludeInactive { get; set; }

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

    public ContentNavigationFilter ToFilter(Guid tenantId)
    {
        var parentWasSpecified = ParentId is not null;
        var parentIsRoot = string.Equals(ParentId, "root", StringComparison.OrdinalIgnoreCase);
        Guid? parentId = parentIsRoot || string.IsNullOrWhiteSpace(ParentId)
            ? null
            : Guid.Parse(ParentId);

        return new ContentNavigationFilter(
            tenantId,
            RegionId,
            EntryId,
            NodeId,
            CollectionId,
            parentId,
            parentWasSpecified,
            parentIsRoot,
            EntryType,
            CollectionType,
            Mode,
            MarkerType,
            Keyword,
            IncludeHidden,
            IncludeInactive,
            Limit);
    }
}
