import { DataTypes, Model } from 'sequelize';
import { sequelize } from '../config/database';
import TipoTarea from './tipo_tarea.modelo';


class Tarea extends Model {
  public id!: number;
  public nombre!: string | null;
  public descripcion!: string | null;
  public orden!: number | null;
  public id_tipo_tarea!: number;
  public maquina!: number | null;
  public active!: number | null;
  public date_add!: Date | null;
  public date_upd!: Date | null;
  public color!: string;
}

Tarea.init(
  {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
    },
    nombre: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    descripcion: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    orden: {
      type: DataTypes.INTEGER,
      allowNull: true,
    },
    id_tipo_tarea: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'tipo_tarea',
        key: 'id',
      },
    },
    maquina: {
      type: DataTypes.INTEGER,
      allowNull: true,
    },
    active: {
      type: DataTypes.INTEGER,
      allowNull: true,
    },
    date_add: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    date_upd: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    color: {
      type: DataTypes.STRING(11),
      allowNull: true,
    },
  },
  {
    sequelize,
    tableName: 'tarea',
    timestamps: false,
  }
);

// Define la asociación
Tarea.belongsTo(TipoTarea, {
  foreignKey: 'id_tipo_tarea', // Llave foránea en Tarea
  as: 'tipo_tarea', // Alias para la relación
});

export default Tarea;
