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

export interface RequestParticipantAttributes {
    id: string;
    requestId: string;
    requestType: string;
    userId: string;
    participantType: string;
    joinedMethod: string;
    metadata: any | null;
}

export interface RequestParticipantInstance extends Model<RequestParticipantAttributes>, RequestParticipantAttributes { }

export default (sequelize: Sequelize) => {
    const RequestParticipant = sequelize.define<RequestParticipantInstance>('RequestParticipant', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        requestId: {
            type: DataTypes.UUID,
            allowNull: false
        },
        requestType: {
            type: DataTypes.STRING,
            allowNull: false
        },
        userId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        participantType: {
            type: DataTypes.ENUM('owner', 'assignee', 'reviewer', 'contributor', 'observer', 'architecture'),
            defaultValue: 'contributor'
        },
        joinedMethod: {
            type: DataTypes.ENUM('manual', 'auto', 'worknote', 'interview', 'mention'),
            defaultValue: 'auto'
        },
        metadata: {
            type: DataTypes.JSON,
            allowNull: true
        }
    }, {
        tableName: 'request_participants',
        timestamps: true,
        indexes: [
            { fields: ['requestId', 'requestType'] },
            { fields: ['userId'] },
            { fields: ['participantType'] }
        ]
    });

    (RequestParticipant as any).associate = (models: any) => {
        RequestParticipant.belongsTo(models.User, { foreignKey: 'userId', as: 'user' });
    };

    return RequestParticipant;
};
