-- SyncQueue: staging table for the Access -> SQL Server one-way sync.
-- Lives under the ProductionDataBaseSync schema (run 00_schema.sql first).
-- Idempotent: safe to re-run (table created only if absent; indexes only if absent).

IF OBJECT_ID('ProductionDataBaseSync.SyncQueue', 'U') IS NULL
BEGIN
    CREATE TABLE ProductionDataBaseSync.SyncQueue (
        QueueID      bigint IDENTITY(1,1) NOT NULL,
        SourceFile   nvarchar(255)        NOT NULL,
        SourceTable  nvarchar(255)        NOT NULL,
        SourceLogID  bigint               NOT NULL,
        TargetSchema nvarchar(128)        NOT NULL,
        TargetTable  nvarchar(255)        NOT NULL,
        RecordID     nvarchar(50)         NOT NULL,
        OperateType  varchar(10)          NOT NULL,
        RowData      nvarchar(max)        NULL,
        Status       varchar(10)          NOT NULL CONSTRAINT DF_SyncQueue_Status  DEFAULT 'pending',
        RetryCount   int                  NOT NULL CONSTRAINT DF_SyncQueue_Retry   DEFAULT 0,
        ErrorMsg     nvarchar(max)        NULL,
        CapturedAt   datetime2            NOT NULL CONSTRAINT DF_SyncQueue_Captured DEFAULT sysdatetime(),
        AppliedAt    datetime2            NULL,
        CleanedAt    datetime2            NULL,
        CONSTRAINT PK_SyncQueue PRIMARY KEY CLUSTERED (QueueID)
    );
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes
               WHERE name = 'UX_SyncQueue_Dedup'
                 AND object_id = OBJECT_ID('ProductionDataBaseSync.SyncQueue'))
BEGIN
    CREATE UNIQUE INDEX UX_SyncQueue_Dedup
        ON ProductionDataBaseSync.SyncQueue(SourceFile, SourceTable, SourceLogID);
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes
               WHERE name = 'IX_SyncQueue_Pending'
                 AND object_id = OBJECT_ID('ProductionDataBaseSync.SyncQueue'))
BEGIN
    CREATE INDEX IX_SyncQueue_Pending
        ON ProductionDataBaseSync.SyncQueue(Status, TargetSchema, TargetTable);
END
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes
               WHERE name = 'IX_SyncQueue_Cleaned'
                 AND object_id = OBJECT_ID('ProductionDataBaseSync.SyncQueue'))
BEGIN
    CREATE INDEX IX_SyncQueue_Cleaned
        ON ProductionDataBaseSync.SyncQueue(Status, CleanedAt);
END
GO
