const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    const HandoverRequest = sequelize.define('handoverRequest', {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        creation: { type: DataTypes.DATE, allowNull: false },
        modified: { type: DataTypes.DATE, allowNull: false },
        modified_by: { type: DataTypes.INTEGER, allowNull: false },
        owner: { type: DataTypes.INTEGER, allowNull: false },
        enabled: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
        
        request_number: { type: DataTypes.STRING(50), allowNull: false, unique: true },
        subject: { type: DataTypes.STRING(255), allowNull: true },
        applicant_id: { type: DataTypes.INTEGER, allowNull: false },
        department_id: { type: DataTypes.INTEGER, allowNull: false },
        system_description: { type: DataTypes.TEXT, allowNull: true },
        renewal_reason: { type: DataTypes.TEXT, allowNull: true },
        renewal_objective: { type: DataTypes.TEXT, allowNull: true },
        expected_impact: { type: DataTypes.TEXT, allowNull: true },
        additional_notes: { type: DataTypes.TEXT, allowNull: true },
        supervisor_id: { type: DataTypes.INTEGER, allowNull: true },
        status: { type: DataTypes.STRING(20), allowNull: false, defaultValue: 'Draft' },

        doc_no: { type: DataTypes.STRING(50), allowNull: true },
        issue_date: { type: DataTypes.DATEONLY, allowNull: true },
        rev_no: { type: DataTypes.STRING(10), allowNull: true, defaultValue: '0' },
        rev_date: { type: DataTypes.DATEONLY, allowNull: true },
    }, {
        tableName: 'handoverrequests',
        freezeTableName: true
    });

    HandoverRequest.associate = (models) => {
        HandoverRequest.belongsTo(models.Employee, { foreignKey: "applicant_id", as: "applicant" });
        HandoverRequest.belongsTo(models.Department, { foreignKey: "department_id", as: "department" });
        HandoverRequest.belongsTo(models.Employee, { foreignKey: "supervisor_id", as: "supervisor" });
        HandoverRequest.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
        
        HandoverRequest.hasOne(models.HandoverCompletion, { foreignKey: "request_id", as: "completion" });
    };

    return HandoverRequest;
};
