SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

CREATE OR ALTER PROCEDURE dbo.StoryBible_RelationshipSummaries
    @ProjectID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT cr.CharacterRelationshipID,
        cr.CharacterAID,
        ca.CharacterName AS CharacterAName,
        cr.CharacterBID,
        cb.CharacterName AS CharacterBName,
        rt.TypeName AS RelationshipTypeName,
        CASE WHEN cr.IsInitialRelationship = 1 THEN rt.TypeName ELSE NULL END AS InitialRelationshipName,
        initialState.StateName AS InitialStateName,
        cr.InitialIntensity,
        COALESCE(currentEvent.StateName, initialState.StateName, rt.TypeName) AS CurrentStateName,
        COALESCE(currentEvent.Intensity, cr.InitialIntensity) AS CurrentIntensity,
        ISNULL(events.EventCount, 0) AS EventCount,
        firstEvent.SceneLabel AS FirstEventLabel,
        events.EventHistory
    FROM dbo.CharacterRelationships cr
    INNER JOIN dbo.Characters ca ON ca.CharacterID = cr.CharacterAID
    INNER JOIN dbo.Characters cb ON cb.CharacterID = cr.CharacterBID
    INNER JOIN dbo.RelationshipTypes rt ON rt.RelationshipTypeID = cr.RelationshipTypeID
    LEFT JOIN dbo.RelationshipStates initialState ON initialState.RelationshipStateID = cr.InitialRelationshipStateID
    OUTER APPLY
    (
        SELECT TOP (1) rs.StateName, re.Intensity
        FROM dbo.RelationshipEvents re
        INNER JOIN dbo.RelationshipStates rs ON rs.RelationshipStateID = re.RelationshipStateID
        INNER JOIN dbo.Scenes s ON s.SceneID = re.SceneID AND s.IsArchived = 0
        INNER JOIN dbo.Chapters c ON c.ChapterID = s.ChapterID AND c.IsArchived = 0
        INNER JOIN dbo.Books b ON b.BookID = c.BookID AND b.IsArchived = 0
        WHERE re.CharacterRelationshipID = cr.CharacterRelationshipID
        ORDER BY b.SortOrder DESC, c.SortOrder DESC, s.SortOrder DESC, re.CreatedDate DESC, re.RelationshipEventID DESC
    ) currentEvent
    OUTER APPLY
    (
        SELECT COUNT(*) AS EventCount,
            STRING_AGG(
                CONVERT(nvarchar(max), CONCAT(b.BookTitle, N' / Ch ', c.ChapterNumber, N' / Scene ', s.SceneNumber, N': ', rs.StateName, ISNULL(N' (' + CONVERT(nvarchar(20), re.Intensity) + N')', N''), ISNULL(N' - ' + re.Description, N''))),
                N' -> '
            ) WITHIN GROUP (ORDER BY b.SortOrder, c.SortOrder, s.SortOrder, re.CreatedDate, re.RelationshipEventID) AS EventHistory
        FROM dbo.RelationshipEvents re
        INNER JOIN dbo.RelationshipStates rs ON rs.RelationshipStateID = re.RelationshipStateID
        INNER JOIN dbo.Scenes s ON s.SceneID = re.SceneID AND s.IsArchived = 0
        INNER JOIN dbo.Chapters c ON c.ChapterID = s.ChapterID AND c.IsArchived = 0
        INNER JOIN dbo.Books b ON b.BookID = c.BookID AND b.IsArchived = 0
        WHERE re.CharacterRelationshipID = cr.CharacterRelationshipID
    ) events
    OUTER APPLY
    (
        SELECT TOP (1) CONCAT(b.BookTitle, N' / Ch ', c.ChapterNumber, N' / Scene ', s.SceneNumber, N': ', s.SceneTitle) AS SceneLabel
        FROM dbo.RelationshipEvents re
        INNER JOIN dbo.Scenes s ON s.SceneID = re.SceneID AND s.IsArchived = 0
        INNER JOIN dbo.Chapters c ON c.ChapterID = s.ChapterID AND c.IsArchived = 0
        INNER JOIN dbo.Books b ON b.BookID = c.BookID AND b.IsArchived = 0
        WHERE re.CharacterRelationshipID = cr.CharacterRelationshipID
        ORDER BY b.SortOrder, c.SortOrder, s.SortOrder, re.CreatedDate, re.RelationshipEventID
    ) firstEvent
    WHERE cr.ProjectID = @ProjectID AND cr.IsArchived = 0
    ORDER BY ca.CharacterName, cb.CharacterName;
END;
GO
