if not exists (select top 1 1 from sys.objects where name = 'PowerAction' and type = 'U')
begin
	create table PowerAction
	(
		PowerActionId int identity(1, 1) constraint PK_PowerAction primary key,
		PowerActionCode varchar(30),
		PowerActionName varchar(130)
	)
end

if not exists (select top 1 1 from PowerAction)
begin
	insert into PowerAction (PowerActionCode, PowerActionName)
	select 'WAKE', 'Wake machine'
	insert into PowerAction (PowerActionCode, PowerActionName)
	select 'PING', 'Ping machine'
	insert into PowerAction (PowerActionCode, PowerActionName)
	select 'SHUTDOWN', 'Shutdown machine'
	insert into PowerAction (PowerActionCode, PowerActionName)
	select 'RESTART', 'Restart machine'
	insert into PowerAction (PowerActionCode, PowerActionName)
	select 'SLEEP', 'Sleep machine'
	insert into PowerAction (PowerActionCode, PowerActionName)
	select 'LOGOUT', 'Logout machine user'
	insert into PowerAction (PowerActionCode, PowerActionName)
	select 'LOCK', 'Lock machine user screen'
end

if not exists (select top 1 1 from sys.objects where name = 'PowerActionPerformer' and type = 'U')
begin
	create table PowerActionPerformer
	(
		PerformerId int identity(1, 1) constraint PK_PowerActionPerformer primary key,
		PerformerCode varchar(50),
		PerformerName varchar(130)
	)
end

if not exists (select top 1 1 from PowerActionPerformer)
begin
	insert into PowerActionPerformer (PerformerCode, PerformerName)
	select 'NETWORK_RESURRECTOR_SERVER', 'Network resurrector server' union
	select 'NETWORK_RESURRECTOR_AGENT', 'Network resurrector agent'
end

if not exists (select top 1 1 from sys.objects where name = 'MachineAgent' and type = 'U')
begin
	create table MachineAgent
	(
		MachineId int constraint PK_MachineAgent primary key,
		AgentPort int not null,
		constraint FK_MachineAgent_Machine foreign key (MachineId) references Machine(MachineId)
	)
end

if not exists (select top 1 1 from sys.objects where name = 'PowerActionConfiguration' and type = 'U')
begin
	create table PowerActionConfiguration
	(
		ConfigurationId int identity(1, 1) constraint PK_PowerActionConfiguration primary key,
		MachineId int constraint FK_PowerActionConfiguration_Machine foreign key references Machine(MachineId),
		PowerActionId int constraint FK_PowerActionConfigurationn_PowerAction foreign key references PowerAction(PowerActionId),
		PerformerId int constraint FK_PowerActionConfiguration_PowerActionPerformer foreign key references PowerActionPerformer(PerformerId)
	)
end