package com.home4j.entity;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.Size;

import java.time.LocalDateTime;

/**
 * 用户设置实体 - 存储用户个性化配置
 */
@Entity
@Table(name = "t_user_settings")
public class UserSettings extends PanacheEntity {

    /**
     * 关联用户
     */
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", unique = true)
    public User user;

    /**
     * 界面语言: zh-CN, en-US
     */
    @Size(max = 10)
    @Column(length = 10)
    public String language = "zh-CN";

    /**
     * 背景类型: theme (跟随主题), color (纯色), image (图片)
     */
    @Size(max = 20)
    @Column(name = "bg_type", length = 20)
    public String bgType = "theme";

    /**
     * 背景颜色 (当 bgType = color 时使用)
     */
    @Size(max = 30)
    @Column(name = "bg_color", length = 30)
    public String bgColor;

    /**
     * 背景图片URL (当 bgType = image 时使用)
     */
    @Size(max = 500)
    @Column(name = "bg_image", length = 500)
    public String bgImage;

    /**
     * 默认视图模式: list, card
     */
    @Size(max = 20)
    @Column(name = "default_view", length = 20)
    public String defaultView = "card";

    /**
     * 默认卡片显示模式: icon, image
     */
    @Size(max = 20)
    @Column(name = "card_display_mode", length = 20)
    public String cardDisplayMode = "icon";

    /**
     * 天气城市名称
     */
    @Size(max = 50)
    @Column(name = "weather_city", length = 50)
    public String weatherCity;

    /**
     * 创建时间
     */
    @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();
    }

    /**
     * 根据用户ID查找设置
     */
    public static UserSettings findByUserId(Long userId) {
        return find("user.id", userId).firstResult();
    }

    /**
     * 根据用户名查找设置
     */
    public static UserSettings findByUsername(String username) {
        return find("user.username", username).firstResult();
    }
}
