CREATE OR ALTER PROCEDURE dbo.StoryIntelligenceRun_QueueAdminText
    @UserID int,
    @ProjectID int = NULL,
    @BookID int = NULL,
    @ChapterID int = NULL,
    @ChapterNumber decimal(9, 2) = NULL,
    @SourceType nvarchar(50),
    @SourceLabel nvarchar(300),
    @SourceText nvarchar(max),
    @SourceFileName nvarchar(260) = NULL,
    @SourceFileSizeBytes bigint = NULL,
    @SourceWordCount int = NULL,
    @SourceCharacterCount int = NULL,
    @SourceChapterCount int = NULL,
    @Model nvarchar(100),
    @PromptVersionsSummary nvarchar(500)
AS
BEGIN
    SET NOCOUNT ON;

    INSERT dbo.StoryIntelligenceRuns
    (
        UserID, ProjectID, BookID, ChapterID, ChapterNumber, Status, SourceType, SourceLabel, SourceText,
        SourceFileName, SourceFileSizeBytes, SourceWordCount, SourceCharacterCount, SourceChapterCount, Model,
        PromptVersion, PromptVersionsSummary, StartedUtc, CurrentStage, CurrentMessage,
        CompletedScenes, FailedScenes
    )
    VALUES
    (
        @UserID, @ProjectID, @BookID, @ChapterID, @ChapterNumber, N'Pending', @SourceType, @SourceLabel, @SourceText,
        @SourceFileName, @SourceFileSizeBytes, @SourceWordCount, @SourceCharacterCount, @SourceChapterCount, @Model,
        @PromptVersionsSummary, @PromptVersionsSummary, SYSUTCDATETIME(), N'Pending',
        N'Queued for Story Intelligence processing.', 0, 0
    );

    SELECT CAST(SCOPE_IDENTITY() AS int) AS StoryIntelligenceRunID;
END;
GO
