﻿CREATE TABLE [session].[Session]
(
	Id			BIGINT NOT NULL PRIMARY KEY IDENTITY,
	Created		DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET(),
	DomainDocument	[session].[JsonDocument] NOT NULL,
	[Name]		AS JSON_VALUE(DomainDocument, '$.Name') UNIQUE,
	Player1Id	BIGINT NOT NULL,
	Player2Id	BIGINT NULL,

	CONSTRAINT [BoardState must be json] CHECK (isjson(DomainDocument)=1),
	CONSTRAINT FK_Player1_User FOREIGN KEY (Player1Id) REFERENCES [user].[User] (Id) 
		ON DELETE CASCADE 
		ON UPDATE CASCADE,
	CONSTRAINT FK_Player2_User FOREIGN KEY (Player2Id) REFERENCES [user].[User] (Id) 
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
)
