import { DataTypes, Model, Optional } from "sequelize";
import sequelize from "@/lib/sequelize";

// 1️⃣ Define attributes
interface Model3DAttributes {
  id: number;
  title: string;
  description: string;
  speachtext: string;
  image: string;
  modelSrc: string;
  category: string;
  program: string;
  size: string;
  date: string;
  stackCount: number;
  status?: number;
  tag?: string | null;
  createdAt?: Date;
  updatedAt?: Date;
}

// Define creation attributes (id is optional on create)
type Model3DCreationAttributes = Optional<Model3DAttributes, "id" | "stackCount" | "tag" | "createdAt" | "updatedAt" | "status">;

//  Define the Model class
class Model3D
  extends Model<Model3DAttributes, Model3DCreationAttributes>
  implements Model3DAttributes
{
  public id!: number;
  public title!: string;
  public description!: string;
  public speachtext!: string;
  public image!: string;
  public modelSrc!: string;
  public category!: string;
  public program!: string;
  public size!: string;
  public date!: string;
  public stackCount!: number;
  public status?: number;
  public tag?: string | null;

  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

// Initialize the model
Model3D.init(
  {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      autoIncrement: true,
      primaryKey: true,
    },
    title: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: false,
    },
    speachtext: {
      type: DataTypes.TEXT,
      allowNull: false,
    },
    image: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    modelSrc: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    category: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    program: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    size: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    date: {
      type: DataTypes.STRING(191),
      allowNull: false,
    },
    stackCount: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    tag: {
      type: DataTypes.STRING(191),
      allowNull: true,
      defaultValue: null,
    },
    createdAt: {
      type: DataTypes.DATE(3),
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updatedAt: {
      type: DataTypes.DATE(3),
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1, // 1 for active, 0 for inactive
    },
  },
  {
    sequelize,
    tableName: "model3d",
    timestamps: true, // Sequelize will manage createdAt & updatedAt
    updatedAt: "updatedAt",
    createdAt: "createdAt",
  }
);

export default Model3D;