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

namespace BookmarkApi.Data.Entities;

/// <summary>
/// 书签实体
/// </summary>
[Table(Name = "bookmarks")]
[Index("idx_bookmark_user_order", nameof(UserId) + "," + 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>
    /// 书签标题
    /// </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>
    /// 标签数组（JSON存储）
    /// </summary>
    [Column(MapType = typeof(string), StringLength = -1)]
    public string[] Tags { get; set; } = Array.Empty<string>();

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

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

    /// <summary>
    /// 排序值（时间戳）
    /// </summary>
    [Column(IsNullable = false)]
    public long Order { get; set; }

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

    /// <summary>
    /// 允许查看的设备ID列表（JSON存储，可见性为Specified时使用）
    /// </summary>
    [Column(MapType = typeof(string), StringLength = -1)]
    public Guid[]? AllowedDevices { 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; }
}

