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

namespace Tiku.Api.Contracts;

public sealed class ContentNavigationQueryDto
{
    [StringLength(100)]
    public string? TenantCode { get; set; }

    public Guid? RegionId { get; set; }

    public Guid? EntryId { get; set; }

    public Guid? NodeId { get; set; }

    public Guid? CollectionId { get; set; }

    [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; }

    [StringLength(50)]
    public string? EntryType { get; set; }

    [StringLength(50)]
    public string? CollectionType { get; set; }

    [StringLength(50)]
    public string? Mode { get; set; }

    [StringLength(50)]
    public string? MarkerType { get; set; }

    [StringLength(100)]
    public string? Keyword { get; set; }

    public bool IncludeHidden { get; set; }

    public bool IncludeInactive { get; set; }

    [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);
    }
}
