SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

IF OBJECT_ID(N'dbo.TR_Chapters_CreateDefaultScene', N'TR') IS NOT NULL
    DROP TRIGGER dbo.TR_Chapters_CreateDefaultScene;
GO

CREATE OR ALTER PROCEDURE dbo.StoryIntelligenceImportCommit_GetChapterReadiness
    @ChapterID int
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @ActiveSceneCount int;

    SELECT @ActiveSceneCount = COUNT(1)
    FROM dbo.Scenes
    WHERE ChapterID = @ChapterID
        AND IsArchived = 0;

    SELECT
        @ActiveSceneCount AS ActiveSceneCount,
        CAST(CASE WHEN @ActiveSceneCount = 0 THEN 1 ELSE 0 END AS bit) AS CanCommit,
        CAST(CASE WHEN @ActiveSceneCount = 0 THEN N'EmptyChapter' ELSE N'Blocked' END AS nvarchar(80)) AS CommitMode,
        CAST(CASE WHEN @ActiveSceneCount = 0 THEN NULL ELSE N'This chapter already contains scenes.' END AS nvarchar(300)) AS BlockReason;
END;
GO
