--//TODO: Review reservered words

-- Auth table
CREATE TABLE AuthLevels (
    AuthLevelID TINYINT IDENTITY(1,1) PRIMARY KEY,
    AuthLevel VARCHAR(50),
    Description VARCHAR(MAX)
);


-- LEAFHeaders table
CREATE TABLE LEAFHeaders (
    LEAFHeaderID INT IDENTITY(1,1) PRIMARY KEY,
    LEAFHeader VARCHAR(50) NOT NULL,
    HeaderRegex VARCHAR(120) NOT NULL DEFAULT '*',
    Required BIT NOT NULL DEFAULT 0,
    Nullable BIT NOT NULL DEFAULT 1,
    ValueRegex VARCHAR(120) NOT NULL DEFAULT '*',
    Active BIT NOT NULL DEFAULT 1,
    Version TINYINT NOT NULL,
    Created DATETIMEOFFSET NOT NULL DEFAULT GETDATE(),
);

CREATE TABLE Company (
    CompanyID SMALLINT IDENTITY(1,1) PRIMARY KEY,
    CompanyName VARCHAR(40) NOT NULL,
    MainContact SMALLINT,
    State CHAR(2),
    Created DATETIMEOFFSET NOT NULL DEFAULT GETDATE(),
    Updated DATETIMEOFFSET,
    LastLogin DATETIMEOFFSET
);
GO

-- Users table
CREATE TABLE Users (
    UserID SMALLINT IDENTITY(1,1) PRIMARY KEY,
    Username VARCHAR(40),
    Email VARCHAR(50) NOT NULL,
    PasswordHash VARCHAR(100) NOT NULL,
    Created DATETIMEOFFSET NOT NULL DEFAULT GETDATE(),
    Updated DATETIMEOFFSET,
    PasswordUpdated DATETIMEOFFSET,
    CompanyID SMALLINT FOREIGN KEY REFERENCES Company(CompanyID) NOT NULL,
    LastLogin DATETIMEOFFSET,
    FailedAttempts INT NOT NULL DEFAULT 0,
    IsEnabled BIT NOT NULL DEFAULT 0 
);
-- Link the company and users tables
ALTER TABLE Company 
ADD CONSTRAINT FK_Company_MainContact FOREIGN KEY (MainContact) REFERENCES Users(UserID);

-- Store the authorizations of a user
CREATE TABLE Auths (
    AuthID SMALLINT IDENTITY(1,1) PRIMARY KEY,
    UserID SMALLINT FOREIGN KEY REFERENCES Users(UserID) NOT NULL, 
    AuthLevelID TINYINT FOREIGN KEY REFERENCES AuthLevels(AuthLevelID) NOT NULL,
);

-- PartnerHeaders table
CREATE TABLE PartnerHeaders (
    PartnerHeaderID INT IDENTITY(1,1) PRIMARY KEY,
    CompanyID SMALLINT FOREIGN KEY REFERENCES Company(CompanyID) NOT NULL,
    PartnerHeader VARCHAR(80) NOT NULL,
    LEAFHeaderID INT FOREIGN KEY  REFERENCES LEAFHeaders(LEAFHeaderID) NOT NULL,
    Created DATETIMEOFFSET NOT NULL DEFAULT GETDATE()
);
GO

-- Portfolios table
CREATE TABLE Portfolios (
    PortfolioID INT IDENTITY(1,1) PRIMARY KEY,
    CompanyID SMALLINT FOREIGN KEY REFERENCES Company(CompanyID) NOT NULL ,
    Uploaded DATETIMEOFFSET NOT NULL DEFAULT GETDATE(),
    NumContracts SMALLINT,
    Value MONEY,
    UploadUser SMALLINT FOREIGN KEY REFERENCES Users(UserID) NOT NULL
);
GO

CREATE TABLE PortfolioData (
    PortfolioDataID INT IDENTITY(1,1) PRIMARY KEY,
    PortfolioID INT FOREIGN KEY REFERENCES Portfolios(PortfolioID) NOT NULL,
    ContractNumber VARCHAR(25),
    CustomerName VARCHAR(80),
    CustomerPhoneNumber VARCHAR(15),
    CustomerTaxID VARCHAR(10),
    CustomerPhysicalAddress1 VARCHAR(50),
    CustomerPhysicalAddress2 VARCHAR(50),
    CustomerPhysicalCity VARCHAR(25),
    CustomerPhysicalState VARCHAR(25),
    CustomerPhysicalZip VARCHAR(10),
    DateBooked DATE,
    Term TINYINT,
    PaymentAmount MONEY,
    FinancedAmount MONEY,
    Receivablebalance MONEY,
    AssetDescription VARCHAR(120),
    SerialNumberVIN VARCHAR(50),
    BusinessType VARCHAR(80),
    PGName VARCHAR(80),
    PGAddress1 VARCHAR(50),
    PGAddress2 VARCHAR(50),
    PGCity VARCHAR(25),
    PGState VARCHAR(25),
    PGZip VARCHAR(10),
    PGSSN VARCHAR(12),
    DOB DATE,
    PG1FICO SMALLINT,
    ScheduleNumber VARCHAR(50),
    CustomerNumber VARCHAR(50),
    CustomerBillingStreet VARCHAR(50),
    CustomerBillingCity VARCHAR(25),
    CustomerBillingState VARCHAR(25),
    CustomerBillingZip VARCHAR(10),
    DirectInDirectChannel VARCHAR(50),
    DaysDelinquent SMALLINT,
    Times30DaysDelinq SMALLINT,
    Times60DaysDelinq SMALLINT,
    Times90PlusDaysDelinq SMALLINT,
    DBAName VARCHAR(120),
    AssetEquipmentAddress1 VARCHAR(50),
    AssetEquipmentAddress2 VARCHAR(50),
    AssetEquipmentCity VARCHAR(25),
    AssetEquipmentState VARCHAR(25),
    AssetEquipmentZip VARCHAR(10),
    Assumption VARCHAR(50),
    Resturcture VARCHAR(50),
    Bankruptcy VARCHAR(25),
    ResidualTypeTo VARCHAR(50),
    CSC VARCHAR(50),
    CommencementDate DATE,
    MaturityDate DATE,
    PaidThruDate DATE,
    NextDueDate DATE,
    LastPaymentDate DATE,
    Dateincomesuspended DATE,
    TerminationDate DATE,
    RemainingMonthsCash SMALLINT,
    PaymentType VARCHAR(50),
    ResidualAmount FLOAT(5),
    SecurityDeposit MONEY,
    SICCode SMALLINT,
    AssetType VARCHAR(80),
    YearsinBusiness SMALLINT,
    Vendorname VARCHAR(120),
    PG2Name VARCHAR(50),
    PG2Address1 VARCHAR(50),
    PG2Address2 VARCHAR(25),
    PG2City VARCHAR(25),
    PG2State VARCHAR(10),
    PG2Zip VARCHAR(12),
    PG2SSN VARCHAR(10),
    PG2DOB DATE,
    PG2FICO SMALLINT,
    CGName VARCHAR(120),
    CGAddress1 VARCHAR(50),
    CGAddress2 VARCHAR(25),
    CGCity VARCHAR(25),
    CGState VARCHAR(10),
    CGZip VARCHAR(12),
    NextDue DATE,
    NumDays SMALLINT,
    PerDiem VARCHAR(40),
    TotalNetFunding MONEY,
    NPV MONEY,
    AdditionalData VARCHAR(MAX)
);