import { Model, DataTypes, Sequelize } from 'sequelize';

export interface ExitFeedbackAttributes {
    id: string;
    resignationId: string | null;
    terminationRequestId: string | null;
    feedbackType: string;
    ratings: any; // JSON
    comments: string | null;
    submittedAt: Date;
    submittedBy: string | null;
}

export interface ExitFeedbackInstance extends Model<ExitFeedbackAttributes>, ExitFeedbackAttributes { }

export default (sequelize: Sequelize) => {
    const ExitFeedback = sequelize.define<ExitFeedbackInstance>('ExitFeedback', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        resignationId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'resignations',
                key: 'id'
            }
        },
        terminationRequestId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'termination_requests',
                key: 'id'
            }
        },
        feedbackType: {
            type: DataTypes.STRING,
            allowNull: false
        },
        ratings: {
            type: DataTypes.JSON,
            defaultValue: {}
        },
        comments: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        submittedAt: {
            type: DataTypes.DATE,
            defaultValue: DataTypes.NOW
        },
        submittedBy: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        }
    }, {
        tableName: 'exit_feedbacks',
        timestamps: true,
        updatedAt: false
    });

    (ExitFeedback as any).associate = (models: any) => {
        ExitFeedback.belongsTo(models.Resignation, { foreignKey: 'resignationId', as: 'resignation' });
        ExitFeedback.belongsTo(models.TerminationRequest, { foreignKey: 'terminationRequestId', as: 'terminationRequest' });
        ExitFeedback.belongsTo(models.User, { foreignKey: 'submittedBy', as: 'submitter' });
    };

    return ExitFeedback;
};
