-- Ruleta Nexora V1.4.0 - migración manual de respaldo.
-- NO ejecutar si el plugin pudo abrir AdminCP normalmente: ensureSchema() aplica estos cambios automáticamente.
IF COL_LENGTH('dbo.NEXORA_ROULETTE_SPINS','fragments_earned') IS NULL
BEGIN
    ALTER TABLE [dbo].[NEXORA_ROULETTE_SPINS] ADD [fragments_earned] int NOT NULL CONSTRAINT [DF_NRX_fragments] DEFAULT(0) WITH VALUES;
END;
IF OBJECT_ID(N'[dbo].[NEXORA_ROULETTE_PROGRESS]', N'U') IS NULL
BEGIN
    CREATE TABLE [dbo].[NEXORA_ROULETTE_PROGRESS](
        [realm] varchar(20) NOT NULL,[account_username] varchar(50) NOT NULL,
        [fragments] bigint NOT NULL CONSTRAINT [DF_NRG_fragments] DEFAULT(0),
        [lifetime_fragments] bigint NOT NULL CONSTRAINT [DF_NRG_lifetime] DEFAULT(0),
        [updated_at] datetime2 NOT NULL CONSTRAINT [DF_NRG_updated] DEFAULT(SYSUTCDATETIME()),
        CONSTRAINT [PK_NRG] PRIMARY KEY([realm],[account_username])
    );
END;
IF OBJECT_ID(N'[dbo].[NEXORA_ROULETTE_MILESTONES]', N'U') IS NULL
BEGIN
    CREATE TABLE [dbo].[NEXORA_ROULETTE_MILESTONES](
        [id] bigint IDENTITY(1,1) NOT NULL PRIMARY KEY,[realm] varchar(20) NOT NULL,[account_username] varchar(50) NOT NULL,
        [milestone_no] tinyint NOT NULL,[spin_threshold] int NOT NULL,
        [fragments_reward] int NOT NULL CONSTRAINT [DF_NRM_fragments] DEFAULT(0),
        [free_spins_reward] int NOT NULL CONSTRAINT [DF_NRM_spins] DEFAULT(0),
        [created_at] datetime2 NOT NULL CONSTRAINT [DF_NRM_created] DEFAULT(SYSUTCDATETIME()),
        CONSTRAINT [UX_NRM_account_milestone] UNIQUE([realm],[account_username],[milestone_no])
    );
    CREATE INDEX [IX_NRM_realm_created] ON [dbo].[NEXORA_ROULETTE_MILESTONES]([realm],[created_at] DESC);
END;
