using FreeSql.DataAnnotations;
using BookmarkApi.Shared.Enums;

namespace BookmarkApi.Data.Entities;

/// <summary>
/// 书签实体
/// </summary>
[Table(Name = "bookmarks")]
[Index("idx_bookmark_user_folder", nameof(UserId) + "," + nameof(FolderId) + "," + nameof(Order))]
[Index("idx_bookmark_url", nameof(UserId) + "," + nameof(Url))]
public class Bookmark
{
    /// <summary>
    /// 书签ID
    /// </summary>
    [Column(IsPrimary = true)]
    public Guid Id { get; set; }

    /// <summary>
    /// 所属用户ID
    /// </summary>
    [Column(IsNullable = false)]
    public Guid UserId { get; set; }

    /// <summary>
    /// 所属文件夹ID（null 为根目录）
    /// </summary>
    public Guid? FolderId { get; set; }

    /// <summary>
    /// 书签标题
    /// </summary>
    [Column(IsNullable = false, StringLength = 500)]
    public string Title { get; set; } = string.Empty;

    /// <summary>
    /// 书签URL
    /// </summary>
    [Column(IsNullable = false, StringLength = 2000)]
    public string Url { get; set; } = string.Empty;

    /// <summary>
    /// 书签描述
    /// </summary>
    [Column(StringLength = 2000)]
    public string? Description { get; set; }

    /// <summary>
    /// 网站图标（Base64或URL）
    /// </summary>
    [Column(StringLength = -1)]
    public string? Icon { get; set; }

    /// <summary>
    /// 文件夹内排序
    /// </summary>
    [Column(IsNullable = false)]
    public int Order { get; set; }

    /// <summary>
    /// 可见性类型
    /// </summary>
    [Column(MapType = typeof(int))]
    public VisibilityType Visibility { get; set; } = VisibilityType.Public;

    /// <summary>
    /// 访问次数
    /// </summary>
    [Column(IsNullable = false)]
    public int VisitCount { get; set; }

    /// <summary>
    /// 最后访问时间
    /// </summary>
    public DateTime? LastVisitAt { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(ServerTime = DateTimeKind.Utc, CanUpdate = false)]
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// 更新时间
    /// </summary>
    [Column(ServerTime = DateTimeKind.Utc)]
    public DateTime UpdatedAt { get; set; }

    /// <summary>
    /// 导航属性 - 所属用户
    /// </summary>
    [Navigate(nameof(UserId))]
    public virtual User? User { get; set; }

    /// <summary>
    /// 导航属性 - 所属文件夹
    /// </summary>
    [Navigate(nameof(FolderId))]
    public virtual Folder? Folder { get; set; }

    /// <summary>
    /// 导航属性 - 书签标签关联
    /// </summary>
    [Navigate(nameof(BookmarkTag.BookmarkId))]
    public virtual List<BookmarkTag>? BookmarkTags { get; set; }

    /// <summary>
    /// 导航属性 - 设备权限
    /// </summary>
    [Navigate(nameof(BookmarkDevicePermission.BookmarkId))]
    public virtual List<BookmarkDevicePermission>? DevicePermissions { get; set; }

    /// <summary>
    /// 导航属性 - 所属集合关联
    /// </summary>
    [Navigate(nameof(CollectionBookmark.BookmarkId))]
    public virtual List<CollectionBookmark>? CollectionBookmarks { get; set; }

    /// <summary>
    /// 导航属性 - 访问历史
    /// </summary>
    [Navigate(nameof(BookmarkVisit.BookmarkId))]
    public virtual List<BookmarkVisit>? Visits { get; set; }
}

