To add to this thread, here's how Lichess generates chess puzzles - https://github.com/ornicar/lichess-puzzler