CREATE TABLE [dbo].[ATScheduledTask] (
    [TaskId]           INT IDENTITY(1,1),
    [ScheduleId]       INT NOT NULL,
    [TaskName]         NVARCHAR(255) NOT NULL,
    [TaskTypeId]       INT NOT NULL, 
    [CommandText]      NVARCHAR(MAX) NULL,      -- e.g. SQL script or shell command
    [ExecutablePath]   NVARCHAR(1024) NULL,     -- e.g. path to EXE, PowerShell, or script file
    [OnSuccessActionId]  INT NULL,
    [OnFailureActionId]  INT NULL,
    [Enabled]          BIT NOT NULL CONSTRAINT [DF_ATScheduledTask_Enabled] DEFAULT(1),
    [SortOrder]        INT NOT NULL CONSTRAINT [DF_ATScheduledTask_SortOrder] DEFAULT(0),
    CONSTRAINT [PK_ATScheduledTask] PRIMARY KEY ([TaskId]),

    CONSTRAINT [FK_ATScheduledTask_ATScheduledItem]
        FOREIGN KEY ([ScheduleId])
        REFERENCES [dbo].[ATScheduledItem]([ScheduleId])
        ON DELETE CASCADE,

    CONSTRAINT [FK_ATScheduledTask_ATTaskType]
        FOREIGN KEY ([TaskTypeId])
        REFERENCES [dbo].[ATTaskType]([TaskTypeId]),

    CONSTRAINT FK_TaskAction_Success
        FOREIGN KEY ([OnSuccessActionId]) 
        REFERENCES ATTaskActionType(ActionId),

    CONSTRAINT FK_TaskAction_Failure
        FOREIGN KEY ([OnFailureActionId]) 
        REFERENCES ATTaskActionType(ActionId)
);
