SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

IF OBJECT_ID(N'dbo.ProjectRestorePoints', N'U') IS NULL
BEGIN
    CREATE TABLE dbo.ProjectRestorePoints
    (
        ProjectRestorePointID int IDENTITY(1,1) NOT NULL CONSTRAINT PK_ProjectRestorePoints PRIMARY KEY,
        ProjectID int NOT NULL,
        RestorePointName nvarchar(255) NOT NULL,
        RestorePointType nvarchar(50) NOT NULL,
        CreatedDateUtc datetime2 NOT NULL CONSTRAINT DF_ProjectRestorePoints_CreatedDateUtc DEFAULT SYSUTCDATETIME(),
        JsonData nvarchar(max) NOT NULL,
        JsonSizeBytes bigint NOT NULL,
        Notes nvarchar(max) NULL,
        CONSTRAINT FK_ProjectRestorePoints_Projects FOREIGN KEY (ProjectID) REFERENCES dbo.Projects(ProjectID)
    );
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'IX_ProjectRestorePoints_ProjectCreated' AND object_id = OBJECT_ID(N'dbo.ProjectRestorePoints'))
    CREATE INDEX IX_ProjectRestorePoints_ProjectCreated ON dbo.ProjectRestorePoints(ProjectID, CreatedDateUtc DESC);
GO

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

    SELECT rp.ProjectRestorePointID, rp.ProjectID, p.ProjectName, rp.RestorePointName, rp.RestorePointType,
        rp.CreatedDateUtc, CAST(NULL AS nvarchar(max)) AS JsonData, rp.JsonSizeBytes, rp.Notes
    FROM dbo.ProjectRestorePoints rp
    INNER JOIN dbo.Projects p ON p.ProjectID = rp.ProjectID
    WHERE rp.ProjectID = @ProjectID
    ORDER BY rp.CreatedDateUtc DESC, rp.ProjectRestorePointID DESC;
END;
GO

CREATE OR ALTER PROCEDURE dbo.ProjectRestorePoint_Get
    @ProjectRestorePointID int,
    @ProjectID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT rp.ProjectRestorePointID, rp.ProjectID, p.ProjectName, rp.RestorePointName, rp.RestorePointType,
        rp.CreatedDateUtc, rp.JsonData, rp.JsonSizeBytes, rp.Notes
    FROM dbo.ProjectRestorePoints rp
    INNER JOIN dbo.Projects p ON p.ProjectID = rp.ProjectID
    WHERE rp.ProjectRestorePointID = @ProjectRestorePointID
        AND rp.ProjectID = @ProjectID;
END;
GO

CREATE OR ALTER PROCEDURE dbo.ProjectRestorePoint_Create
    @ProjectID int,
    @RestorePointName nvarchar(255),
    @RestorePointType nvarchar(50),
    @JsonData nvarchar(max),
    @Notes nvarchar(max) = NULL
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO dbo.ProjectRestorePoints
    (
        ProjectID,
        RestorePointName,
        RestorePointType,
        CreatedDateUtc,
        JsonData,
        JsonSizeBytes,
        Notes
    )
    VALUES
    (
        @ProjectID,
        @RestorePointName,
        @RestorePointType,
        SYSUTCDATETIME(),
        @JsonData,
        DATALENGTH(@JsonData),
        @Notes
    );

    DECLARE @ProjectRestorePointID int = SCOPE_IDENTITY();

    SELECT rp.ProjectRestorePointID, rp.ProjectID, p.ProjectName, rp.RestorePointName, rp.RestorePointType,
        rp.CreatedDateUtc, CAST(NULL AS nvarchar(max)) AS JsonData, rp.JsonSizeBytes, rp.Notes
    FROM dbo.ProjectRestorePoints rp
    INNER JOIN dbo.Projects p ON p.ProjectID = rp.ProjectID
    WHERE rp.ProjectRestorePointID = @ProjectRestorePointID;
END;
GO

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

    SELECT COUNT(1)
    FROM dbo.ProjectRestorePoints
    WHERE ProjectID = @ProjectID;
END;
GO

CREATE OR ALTER PROCEDURE dbo.ProjectRestorePoint_Delete
    @ProjectRestorePointID int,
    @ProjectID int
AS
BEGIN
    SET NOCOUNT ON;

    IF OBJECT_ID(N'dbo.ProjectRestoreHistory', N'U') IS NOT NULL
    BEGIN
        UPDATE dbo.ProjectRestoreHistory
        SET SourceRestorePointID = NULL
        WHERE SourceRestorePointID = @ProjectRestorePointID;

        UPDATE dbo.ProjectRestoreHistory
        SET SafetyRestorePointID = NULL
        WHERE SafetyRestorePointID = @ProjectRestorePointID;
    END;

    DELETE FROM dbo.ProjectRestorePoints
    WHERE ProjectRestorePointID = @ProjectRestorePointID
        AND ProjectID = @ProjectID;

    SELECT CAST(CASE WHEN @@ROWCOUNT = 1 THEN 1 ELSE 0 END AS bit);
END;
GO
