-- Deployment
CREATE TABLE Deployment (
  DeploymentId SERIAL PRIMARY KEY,
  ProviderType VARCHAR(255) NOT NULL,
  TargetType VARCHAR(255) NOT NULL,
  TargetName VARCHAR(255) NOT NULL,
  FactoryCluster VARCHAR(255),
  FactoryNodeName VARCHAR(255),
  CPU INTEGER,
  VCPU INTEGER,
  Memory INTEGER,
  Labels VARCHAR(255) [] DEFAULT '{}',
  UNIQUE (TargetType, TargetName)
);
CREATE INDEX Deployment_FactoryNodeName_index ON Deployment(FactoryNodeName);

-- Cluster
CREATE TABLE FactoryCluster (
  ClusterId SERIAL PRIMARY KEY,
  ClusterName VARCHAR(255) NOT NULL UNIQUE,
  ClusterDescription VARCHAR(255) NOT NULL,
  VirtualIp VARCHAR(15) NOT NULL,
  ZoneName VARCHAR(255) NOT NULL
);

-- Node in Cluster
CREATE TABLE FactoryClusterNode (
  ClusterNodeId SERIAL PRIMARY KEY,
  ClusterId INTEGER NOT NULL,
  ServerName VARCHAR(255) NOT NULL UNIQUE,
  FOREIGN KEY (ClusterId) REFERENCES FactoryCluster(ClusterId)
);
CREATE INDEX FactoryClusterNode_ServerName_index ON FactoryClusterNode(ServerName);
-- Site
CREATE TABLE Site (
  SiteId SERIAL PRIMARY KEY,
  SiteName VARCHAR(255) NOT NULL UNIQUE,
  SiteDescription VARCHAR(255) NOT NULL
);

-- Zone
CREATE TABLE Zone (
  ZoneId SERIAL PRIMARY KEY,
  ZoneName VARCHAR(255) NOT NULL UNIQUE,
  ZoneDescription VARCHAR(255) NOT NULL,
  SitesName VARCHAR(255) [] NOT NULL
);

-- Server
CREATE TABLE Server (
  ServerId SERIAL PRIMARY KEY,
  ServerName VARCHAR(255) NOT NULL UNIQUE,
  ServerDescription VARCHAR(255) NOT NULL,
  ServerServermodelName VARCHAR(255) NOT NULL,
  ReleaseDistribution VARCHAR(255) NOT NULL,
  SiteName VARCHAR(255) NOT NULL,
  ZonesName VARCHAR(255) [] NOT NULL,
  ZonesIP VARCHAR(255) [] NOT NULL
);
-- Source
CREATE TABLE Source (
  SourceId SERIAL PRIMARY KEY,
  SourceName VARCHAR(255) NOT NULL UNIQUE,
  SourceURL TEXT
);

-- Release
CREATE TABLE Release (
  ReleaseId SERIAL PRIMARY KEY,
  ReleaseName VARCHAR(255) NOT NULL,
  ReleaseSourceId INTEGER NOT NULL,
  ReleaseDistribution VARCHAR(20) CONSTRAINT releasedistribution_choice CHECK (ReleaseDistribution IN ('last', 'n-1', 'n-2')),
  UNIQUE (ReleaseName, ReleaseSourceId),
  UNIQUE (ReleaseDistribution, ReleaseSourceId),
  FOREIGN KEY (ReleaseSourceId) REFERENCES Source(SourceId)
);

-- Servermodel
CREATE TABLE Servermodel (
  ServermodelId SERIAL PRIMARY KEY,
  ServermodelName VARCHAR(255) NOT NULL,
  ServermodelDescription VARCHAR(255) NOT NULL,
  Osname VARCHAR(255),
  Osversion VARCHAR(255),
  ISO VARCHAR(255),
  ServermodelParents VARCHAR(255) [] DEFAULT '{}',
  SourceName VARCHAR(255) NOT NULL,
  ReleaseDistribution VARCHAR(255) NOT NULL,
  ServermodelApplicationserviceId INTEGER NOT NULL,
  UNIQUE (ServermodelName, SourceName, ReleaseDistribution)
);
CREATE INDEX ServermodelApplicationserviceId_index ON Servermodel (ServermodelApplicationserviceId);

-- Applicationservice
CREATE TABLE Applicationservice (
  ApplicationserviceId SERIAL PRIMARY KEY,
  ApplicationserviceName VARCHAR(255) NOT NULL,
  ApplicationserviceDescription VARCHAR(255) NOT NULL,
  ApplicationserviceReleaseId INTEGER NOT NULL,
  --ApplicationserviceLVIS VARCHAR(255) NOT NULL,
  --ApplicationserviceLVIP VARCHAR(255) NOT NULL,
  --ApplicationserviceLVMP VARCHAR(255) NOT NULL,
  OS JSON,
  UNIQUE (ApplicationserviceName, ApplicationserviceReleaseId)
);

CREATE TABLE ApplicationserviceDependency (
  ApplicationserviceId INTEGER NOT NULL,
  ApplicationserviceDependencyId INTEGER NOT NULL,
  UNIQUE(ApplicationserviceId, ApplicationserviceDependencyId),
  FOREIGN KEY (ApplicationserviceId) REFERENCES Applicationservice(ApplicationserviceId),
  FOREIGN KEY (ApplicationserviceDependencyId) REFERENCES Applicationservice(ApplicationserviceId)
);

-- Log
CREATE TABLE log(
  Msg VARCHAR(255) NOT NULL,
  Level VARCHAR(10) NOT NULL,
  Path VARCHAR(255),
  Username VARCHAR(100) NOT NULL,
  Data JSON,
  Date timestamp DEFAULT current_timestamp
);
-- User, Role and ACL
CREATE TABLE RisottoUser (
  UserId SERIAL PRIMARY KEY,
  UserLogin VARCHAR(100) NOT NULL UNIQUE,
  UserName VARCHAR(100) NOT NULL,
  UserSurname VARCHAR(100) NOT NULL
);

CREATE TABLE UserRole (
  RoleId SERIAL PRIMARY KEY,
  RoleUserId INTEGER NOT NULL,
  RoleName VARCHAR(255) NOT NULL,
  RoleAttribute VARCHAR(255),
  RoleAttributeValue VARCHAR(255),
  FOREIGN KEY (RoleUserId) REFERENCES RisottoUser(UserId)
);

CREATE TABLE URI (
  URIId SERIAL PRIMARY KEY,
  URIName VARCHAR(255) NOT NULL UNIQUE
);

CREATE TABLE RoleURI (
  RoleName VARCHAR(255) NOT NULL,
  URIId INTEGER NOT NULL,
  FOREIGN KEY (URIId) REFERENCES URI(URIId),
  PRIMARY KEY (RoleName, URIId)
);
