﻿CREATE PROCEDURE [session].[ReadSession]
	@Name [session].[SessionName]
AS
BEGIN
	SET NOCOUNT ON -- Performance boost
	SET XACT_ABORT ON -- Rollback transaction on error
	SET TRANSACTION ISOLATION LEVEL SNAPSHOT -- Ignores data changes that happen after the transaction begins.

	BEGIN TRANSACTION

		-- Session
		SELECT
			sess.[Name],
			p1.[Name] as Player1,
			p2.[Name] as Player2
		FROM [session].[Session] sess
			INNER JOIN [user].[User] p1 on sess.Player1Id = p1.Id
			LEFT JOIN [user].[User] p2 on sess.Player2Id = p2.Id
		WHERE sess.[Name] = @Name;

		-- Player moves
		SELECT
			mv.[From],
			mv.[To],
			mv.IsPromotion,
			piece.[Name] as PieceFromHand
		FROM [session].[Move] mv
			INNER JOIN [session].[Session] sess ON sess.Id = mv.SessionId
			LEFT JOIN [session].Piece piece on piece.Id = mv.PieceIdFromHand
		WHERE sess.[Name] = @Name;
		
	COMMIT
END
