Base Building Corridor Mechanic Query

I’m trying to create a base-building mechanic like Dungeon Keeper or Evil Genius. I’ve managed to get tiles spawning to grid when the left mouse button is held down. So now I need to dynamically change the type of spawned tile based on the surrounding tiles, to create corners and such.

Example of the mechanic I’m trying to emulate about 5min into this video: Evil Genius - How to be Evil - Chapter 1 - YouTube

As a bonus extra, I also need to create a ‘drag’ system to allow the player to drag out corridors and rooms like a selection box, rather than placing tile by tile.

I’m a bit stuck on what to search for, and any suggestions to get me headed in the right direction will be greatly appreciated.

Thanks
Kib