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

export interface FddAssignmentAttributes {
    id: string;
    applicationId: string;
    assignedToAgency: string | null;
    status: string;
}

export interface FddAssignmentInstance extends Model<FddAssignmentAttributes>, FddAssignmentAttributes { }

export default (sequelize: Sequelize) => {
    const FddAssignment = sequelize.define<FddAssignmentInstance>('FddAssignment', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        applicationId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'applications',
                key: 'id'
            }
        },
        assignedToAgency: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'pending'
        }
    }, {
        tableName: 'fdd_assignments',
        timestamps: true
    });

    (FddAssignment as any).associate = (models: any) => {
        FddAssignment.belongsTo(models.Application, { foreignKey: 'applicationId', as: 'application' });
        FddAssignment.belongsTo(models.User, { foreignKey: 'assignedToAgency', as: 'agency' });
        FddAssignment.hasMany(models.FddReport, { foreignKey: 'assignmentId', as: 'reports' });
    };

    return FddAssignment;
};
