import { DataTypes, Model } from 'sequelize';
import { sequelize } from '../config/database';
import AreaCasillero from './area_casillero.model';

// Definimos el modelo Casillero
class Casillero extends Model {
  public id!: number;
  public nombre!: string;
  public descripcion!: string;
  public ocupado!: number;
  public date_upd!: Date;
  public active!: number | null;
  public id_areacasillero!: number;
  public pedido!: string | null;
}

// Inicializamos el modelo
Casillero.init(
  {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
    },
    nombre: {
      type: DataTypes.STRING(255),
      allowNull: false,
    },
    descripcion: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    ocupado: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    date_upd: {
      type: DataTypes.DATE,
      allowNull: false,
    },
    active: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
    },
    id_areacasillero: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: AreaCasillero, // Referencia al modelo AreaCasillero
        key: 'id',
      },
    },
    pedido: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
  },
  {
    sequelize, // Instancia de Sequelize
    modelName: 'Casillero', // Nombre del modelo
    tableName: 'casillero', // Nombre de la tabla en la base de datos
    timestamps: false, // Si no se necesita createdAt y updatedAt
    charset: 'utf8',
    collate: 'utf8_bin',
  }
);

// Definimos la relación con AreaCasillero
Casillero.belongsTo(AreaCasillero, {
  foreignKey: 'id_areacasillero',
  as: 'area',
});

export default Casillero;