import { DataTypes } from 'sequelize';
import sequelize from '../config/config.js';
import Client from './client.js';

const Form = sequelize.define('Form', {
  form_id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  form_data: {
    type: DataTypes.JSON,
    allowNull: false
  },
  created_by: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  treelogy_id: {
    type: DataTypes.INTEGER,
    allowNull: true
  },
  created_date: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW
  },
  updated_date: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW
  }
}, {
  tableName: 'forms',
  schema: 'public',
  timestamps: false,
  hooks: {
    beforeUpdate: (form) => {
      form.updated_date = new Date();
    }
  },
  indexes: [
    { fields: ['created_by'] },
    { fields: ['created_date'] },
    { fields: ['treelogy_id'] }
  ]
});

Form.belongsTo(Client, { foreignKey: 'created_by', as: 'creator' });

export default Form;





