SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

CREATE OR ALTER PROCEDURE dbo.Scene_ListByFloorPlan
    @FloorPlanID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        s.SceneID,
        s.ChapterID,
        c.BookID,
        b.BookNumber,
        b.SortOrder AS BookSortOrder,
        b.BookTitle,
        b.Subtitle AS BookSubtitle,
        c.ChapterNumber,
        c.SortOrder AS ChapterSortOrder,
        c.ChapterTitle,
        s.SceneNumber,
        s.SceneTitle,
        s.Summary,
        s.POVCharacterID,
        s.PrimaryLocationID,
        s.FloorPlanID,
        s.InitialFloorPlanFloorID,
        s.SortOrder,
        s.TimeModeID,
        tm.TimeModeName,
        s.StartDateTime,
        s.EndDateTime,
        s.DurationAmount,
        s.DurationUnitID,
        du.DurationUnitName,
        s.RelativeTimeText,
        s.TimeConfidenceID,
        tc.TimeConfidenceName,
        s.ScenePurposeNotes,
        s.SceneOutcomeNotes,
        s.RevisionStatusID,
        rs.StatusName AS RevisionStatusName,
        s.CreatedDate,
        s.UpdatedDate,
        s.IsArchived
    FROM dbo.Scenes s
    INNER JOIN dbo.Chapters c ON c.ChapterID = s.ChapterID
    INNER JOIN dbo.Books b ON b.BookID = c.BookID
    INNER JOIN dbo.TimeModes tm ON tm.TimeModeID = s.TimeModeID
    INNER JOIN dbo.TimeConfidences tc ON tc.TimeConfidenceID = s.TimeConfidenceID
    INNER JOIN dbo.RevisionStatuses rs ON rs.RevisionStatusID = s.RevisionStatusID
    LEFT JOIN dbo.DurationUnits du ON du.DurationUnitID = s.DurationUnitID
    WHERE s.FloorPlanID = @FloorPlanID
      AND s.IsArchived = 0
      AND c.IsArchived = 0
      AND b.IsArchived = 0
    ORDER BY
        b.SortOrder,
        b.BookNumber,
        b.BookTitle,
        c.SortOrder,
        c.ChapterNumber,
        s.SceneNumber,
        s.SortOrder,
        s.SceneTitle;
END;
GO
