import { DataTypes, Model, Optional } from "sequelize";
import sequelize from "@/lib/sequelize";
import Model3D from "@/models/Model3D";

// 1️⃣ Define attributes
interface HotspotAttributes {
  id: number;
  modelId: number;
  position: string;
  normal: string;
  title: string;
  description?: string | null;
  createdAt?: Date;
}

// 2️⃣ Define creation attributes
type HotspotCreationAttributes = Optional<HotspotAttributes, "id" | "description" | "createdAt">;

//  Define the model class
class Hotspot
  extends Model<HotspotAttributes, HotspotCreationAttributes>
  implements HotspotAttributes
{
  public id!: number;
  public modelId!: number;
  public position!: string;
  public normal!: string;
  public title!: string;
  public description?: string | null;
  public readonly createdAt!: Date;
}

//  Initialize the model
Hotspot.init(
  {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      autoIncrement: true,
      primaryKey: true,
    },
    modelId: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
      references: {
        model: Model3D,
        key: "id",
      },
      onUpdate: "CASCADE",
      onDelete: "CASCADE",
    },
    position: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    normal: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    title: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      defaultValue: null,
    },
    createdAt: {
      type: DataTypes.DATE(3),
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  },
  {
    sequelize,
    tableName: "Hotspot",
    timestamps: false, // Only createdAt exists
  }
);


// Set up association
Model3D.hasMany(Hotspot, {
  foreignKey: "modelId",
  as: "hotspots",
});
Hotspot.belongsTo(Model3D, {
  foreignKey: "modelId",
  as: "model",
});

export default Hotspot;