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

namespace Tiku.Api.Contracts;

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

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

    /// <summary>
    ///     ModuleId。
    /// </summary>
    public Guid? ModuleId { 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>
    ///     院校 ID。
    /// </summary>
    public Guid? SchoolId { get; set; }

    /// <summary>
    ///     专业 ID。
    /// </summary>
    public Guid? MajorId { get; set; }

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

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

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

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

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

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

        return new CatalogFilter(
            tenantId,
            RegionId,
            ModuleId,
            parentId,
            parentIsRoot,
            SchoolId,
            MajorId,
            SubjectId,
            NodeId,
            Keyword,
            Type,
            Limit);
    }
}