package com.home4j.entity;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import org.hibernate.validator.constraints.URL;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 书签实体
 */
@Entity
@Table(name = "t_bookmark")
public class Bookmark extends PanacheEntity {

    /**
     * 所属分组
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "group_id")
    public BookmarkGroup group;

    /**
     * 书签名称
     */
    @NotBlank(message = "书签名称不能为空")
    @Size(max = 100, message = "书签名称长度不能超过100")
    @Column(nullable = false, length = 100)
    public String name;

    /**
     * 书签URL
     */
    @NotBlank(message = "URL不能为空")
    @URL(message = "URL格式不正确")
    @Size(max = 500, message = "URL长度不能超过500")
    @Column(nullable = false, length = 500)
    public String url;

    /**
     * 书签描述
     */
    @Size(max = 200, message = "描述长度不能超过200")
    @Column(length = 200)
    public String description;

    /**
     * 图标（Lucide图标名称或自定义图片URL）
     */
    @Size(max = 255, message = "图标长度不能超过255")
    @Column(length = 255)
    public String icon;

    /**
     * 排序顺序
     */
    @Column(name = "sort_order", nullable = false)
    public Integer sortOrder = 0;

    /**
     * 是否可见
     */
    @Column(nullable = false)
    public Boolean visible = true;

    /**
     * 是否在新窗口打开
     */
    @Column(name = "open_in_new_tab", nullable = false)
    public Boolean openInNewTab = true;

    /**
     * 卡片显示模式: icon (图标模式), image (背景图模式)
     */
    @Size(max = 20, message = "显示模式长度不能超过20")
    @Column(name = "display_mode", length = 20)
    public String displayMode = "icon";

    /**
     * 背景图片URL (当 displayMode = image 时使用)
     */
    @Size(max = 500, message = "背景图片URL长度不能超过500")
    @Column(name = "background_image", length = 500)
    public String backgroundImage;

    /**
     * 创建时间
     */
    @Column(name = "created_at", nullable = false, updatable = false)
    public LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @Column(name = "updated_at")
    public LocalDateTime updatedAt;

    @PrePersist
    public void prePersist() {
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }

    @PreUpdate
    public void preUpdate() {
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 根据分组查找所有可见书签
     */
    public static List<Bookmark> findByGroupOrdered(BookmarkGroup group) {
        return find("group = ?1 AND visible = true ORDER BY sortOrder ASC", group).list();
    }

    /**
     * 根据分组ID查找所有书签
     */
    public static List<Bookmark> findByGroupIdOrdered(Long groupId) {
        return find("group.id = ?1 ORDER BY sortOrder ASC", groupId).list();
    }

    /**
     * 查找未分组的书签
     */
    public static List<Bookmark> findUngrouped() {
        return find("group IS NULL ORDER BY sortOrder ASC").list();
    }

    /**
     * 搜索书签（名称或描述）
     */
    public static List<Bookmark> search(String keyword) {
        return find("visible = true AND (LOWER(name) LIKE ?1 OR LOWER(description) LIKE ?1) ORDER BY sortOrder ASC",
                "%" + keyword.toLowerCase() + "%").list();
    }

    /**
     * 获取分组内下一个排序序号
     */
    public static int getNextSortOrder(Long groupId) {
        Bookmark last;
        if (groupId != null) {
            last = find("group.id = ?1 ORDER BY sortOrder DESC", groupId).firstResult();
        } else {
            last = find("group IS NULL ORDER BY sortOrder DESC").firstResult();
        }
        return last == null ? 0 : last.sortOrder + 1;
    }
}
