-- Create the database
CREATE DATABASE TestDUTASJobSchedule;
GO

USE TestDUTASJobSchedule;
GO

-- Create the main control table
CREATE TABLE dbo.JobControl
(
    JobID              INT IDENTITY(1,1) NOT NULL,
    JobName            VARCHAR(50) NOT NULL,
    ScheduledStartTime TIME(7) NOT NULL,
    SchedulerAction    VARCHAR(10) NOT NULL,       -- STOP or CONTINUE
    IsActive           BIT NOT NULL CONSTRAINT DF_JobControl_IsActive DEFAULT (1),
    CreatedDate        DATETIME NOT NULL CONSTRAINT DF_JobControl_CreatedDate DEFAULT (GETDATE()),
    Frequency          CHAR(20) NOT NULL CONSTRAINT DF_JobControl_Frequency DEFAULT ('DAILY'),
    FrequencyPattern   NVARCHAR(500) NULL,         -- JSON scheduling rule

    CONSTRAINT PK_JobControl PRIMARY KEY CLUSTERED (JobID ASC),
    CONSTRAINT UQ_JobControl_JobName UNIQUE NONCLUSTERED (JobName ASC),
    CONSTRAINT CK_JobControl_SchedulerAction CHECK (SchedulerAction IN ('STOP', 'CONTINUE'))
);
GO

-- Create job dependencies table
CREATE TABLE dbo.JobDependencies (
    DependencyID INT IDENTITY(1,1) PRIMARY KEY,
    JobName VARCHAR(50) NOT NULL,
    PredecessorJobName VARCHAR(50) NULL,
    FOREIGN KEY (JobName) REFERENCES dbo.JobControl(JobName)
);
GO

-- Create execution history table
CREATE TABLE dbo.JobExecutionHistory (
    ExecutionID INT IDENTITY(1,1) PRIMARY KEY,
    JobName VARCHAR(50) NOT NULL,
    RunDate DATE NOT NULL,
    ScheduledStartTime TIME NOT NULL,
    ActualStartTime DATETIME NULL,
    ActualEndTime DATETIME NULL,
    Status VARCHAR(20) DEFAULT 'Pending' CHECK (Status IN ('Pending', 'Running', 'Success', 'Failed', 'ForceComplete')),
    ErrorMessage VARCHAR(MAX) NULL,
    OverrideFlag BIT DEFAULT 0,
    OverrideBy VARCHAR(100) NULL,
    OverrideDate DATETIME NULL
);
GO

-- Create holidays table
CREATE TABLE dbo.FederalHolidays (
    HolidayID INT IDENTITY(1,1) PRIMARY KEY,
    HolidayDate DATE NOT NULL UNIQUE,
    HolidayName VARCHAR(100) NOT NULL,
    Year INT NOT NULL
);
GO

-- Create Exit Code table for PowerShell Script
CREATE TABLE dbo.JobExitCodes
(
    JobExecutionID INT IDENTITY(1,1) PRIMARY KEY,  -- unique ID per execution
    JobName NVARCHAR(128) NOT NULL,               -- job name
    RunDate DATE NOT NULL,                         -- date of execution
    ExitCode INT NOT NULL,                         -- exit code from job
    RecordedTime DATETIME NOT NULL DEFAULT GETDATE() -- timestamp of execution
);
GO
-- Create JobSchedule table
CREATE TABLE dbo.JobSchedule (
    JobScheduleID      INT IDENTITY(1,1) PRIMARY KEY,
    JobName            VARCHAR(100) NOT NULL,
    Frequency          VARCHAR(20) CHECK (Frequency IN ('Monthly','Quarterly')),
    ScheduledRule      NVARCHAR(500) NULL,       -- Copy of JSON or rule text from JobControl
    ScheduledDate      DATE NOT NULL,            -- Planned date (before adjustment)
    AdjustedRunDate    DATE NOT NULL,            -- Final working-day adjusted date
    MonthOfSchedule    CHAR(7) NOT NULL,         -- 'YYYY-MM'
    YearOfSchedule     INT NOT NULL,
    CreatedOn          DATETIME DEFAULT GETDATE(),
    CreatedBy          VARCHAR(50) DEFAULT SUSER_SNAME()
);
GO