SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

IF OBJECT_ID(N'dbo.ContinuityWarningAcknowledgements', N'U') IS NULL
BEGIN
    CREATE TABLE dbo.ContinuityWarningAcknowledgements
    (
        ContinuityWarningAcknowledgementID int IDENTITY(1,1) NOT NULL CONSTRAINT PK_ContinuityWarningAcknowledgements PRIMARY KEY,
        ProjectID int NOT NULL,
        WarningType nvarchar(100) NOT NULL,
        SceneID int NULL,
        CharacterID int NULL,
        AssetID int NULL,
        AcknowledgedDate datetime2 NOT NULL CONSTRAINT DF_ContinuityWarningAcknowledgements_AcknowledgedDate DEFAULT SYSUTCDATETIME(),
        AcknowledgedByUserID int NULL,
        Notes nvarchar(500) NULL,
        SceneIDKey AS ISNULL(SceneID, 0) PERSISTED,
        CharacterIDKey AS ISNULL(CharacterID, 0) PERSISTED,
        AssetIDKey AS ISNULL(AssetID, 0) PERSISTED,
        CONSTRAINT FK_ContinuityWarningAcknowledgements_Projects FOREIGN KEY (ProjectID) REFERENCES dbo.Projects(ProjectID),
        CONSTRAINT FK_ContinuityWarningAcknowledgements_Scenes FOREIGN KEY (SceneID) REFERENCES dbo.Scenes(SceneID),
        CONSTRAINT FK_ContinuityWarningAcknowledgements_Characters FOREIGN KEY (CharacterID) REFERENCES dbo.Characters(CharacterID),
        CONSTRAINT FK_ContinuityWarningAcknowledgements_Assets FOREIGN KEY (AssetID) REFERENCES dbo.StoryAssets(StoryAssetID),
        CONSTRAINT FK_ContinuityWarningAcknowledgements_AppUser FOREIGN KEY (AcknowledgedByUserID) REFERENCES dbo.AppUser(UserID)
    );
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'UX_ContinuityWarningAcknowledgements_Identity' AND object_id = OBJECT_ID(N'dbo.ContinuityWarningAcknowledgements'))
BEGIN
    CREATE UNIQUE INDEX UX_ContinuityWarningAcknowledgements_Identity
    ON dbo.ContinuityWarningAcknowledgements(ProjectID, WarningType, SceneIDKey, CharacterIDKey, AssetIDKey);
END;
GO

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

    SELECT ContinuityWarningAcknowledgementID,
        ProjectID,
        WarningType,
        SceneID,
        CharacterID,
        AssetID,
        AcknowledgedDate,
        AcknowledgedByUserID,
        Notes
    FROM dbo.ContinuityWarningAcknowledgements
    WHERE ProjectID = @ProjectID;
END;
GO

CREATE OR ALTER PROCEDURE dbo.ContinuityWarningAcknowledgement_Get
    @ProjectID int,
    @WarningType nvarchar(100),
    @SceneID int = NULL,
    @CharacterID int = NULL,
    @AssetID int = NULL
AS
BEGIN
    SET NOCOUNT ON;

    SELECT ContinuityWarningAcknowledgementID,
        ProjectID,
        WarningType,
        SceneID,
        CharacterID,
        AssetID,
        AcknowledgedDate,
        AcknowledgedByUserID,
        Notes
    FROM dbo.ContinuityWarningAcknowledgements
    WHERE ProjectID = @ProjectID
        AND WarningType = @WarningType
        AND ISNULL(SceneID, 0) = ISNULL(@SceneID, 0)
        AND ISNULL(CharacterID, 0) = ISNULL(@CharacterID, 0)
        AND ISNULL(AssetID, 0) = ISNULL(@AssetID, 0);
END;
GO

CREATE OR ALTER PROCEDURE dbo.ContinuityWarningAcknowledgement_Save
    @ProjectID int,
    @WarningType nvarchar(100),
    @SceneID int = NULL,
    @CharacterID int = NULL,
    @AssetID int = NULL,
    @AcknowledgedByUserID int = NULL,
    @Notes nvarchar(500) = NULL
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @TrimmedWarningType nvarchar(100) = NULLIF(LTRIM(RTRIM(@WarningType)), N'');
    DECLARE @TrimmedNotes nvarchar(500) = NULLIF(LTRIM(RTRIM(@Notes)), N'');

    IF @ProjectID <= 0 OR @TrimmedWarningType IS NULL
        RETURN;

    IF EXISTS
    (
        SELECT 1
        FROM dbo.ContinuityWarningAcknowledgements
        WHERE ProjectID = @ProjectID
            AND WarningType = @TrimmedWarningType
            AND ISNULL(SceneID, 0) = ISNULL(@SceneID, 0)
            AND ISNULL(CharacterID, 0) = ISNULL(@CharacterID, 0)
            AND ISNULL(AssetID, 0) = ISNULL(@AssetID, 0)
    )
    BEGIN
        UPDATE dbo.ContinuityWarningAcknowledgements
        SET AcknowledgedDate = SYSUTCDATETIME(),
            AcknowledgedByUserID = @AcknowledgedByUserID,
            Notes = @TrimmedNotes
        WHERE ProjectID = @ProjectID
            AND WarningType = @TrimmedWarningType
            AND ISNULL(SceneID, 0) = ISNULL(@SceneID, 0)
            AND ISNULL(CharacterID, 0) = ISNULL(@CharacterID, 0)
            AND ISNULL(AssetID, 0) = ISNULL(@AssetID, 0);
    END
    ELSE
    BEGIN
        INSERT dbo.ContinuityWarningAcknowledgements
            (ProjectID, WarningType, SceneID, CharacterID, AssetID, AcknowledgedByUserID, Notes)
        VALUES
            (@ProjectID, @TrimmedWarningType, @SceneID, @CharacterID, @AssetID, @AcknowledgedByUserID, @TrimmedNotes);
    END;
END;
GO

CREATE OR ALTER PROCEDURE dbo.ContinuityWarningAcknowledgement_Delete
    @ProjectID int,
    @WarningType nvarchar(100),
    @SceneID int = NULL,
    @CharacterID int = NULL,
    @AssetID int = NULL
AS
BEGIN
    SET NOCOUNT ON;

    DELETE FROM dbo.ContinuityWarningAcknowledgements
    WHERE ProjectID = @ProjectID
        AND WarningType = @WarningType
        AND ISNULL(SceneID, 0) = ISNULL(@SceneID, 0)
        AND ISNULL(CharacterID, 0) = ISNULL(@CharacterID, 0)
        AND ISNULL(AssetID, 0) = ISNULL(@AssetID, 0);
END;
GO
