CREATE TABLE ProviderDeployment (
  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 ProviderDeployment(FactoryNodeName);

CREATE TABLE ProviderFactoryCluster (
  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
);

CREATE TABLE ProviderFactoryClusterNode (
  ClusterNodeId SERIAL PRIMARY KEY,
  ClusterId INTEGER NOT NULL,
  ServerName VARCHAR(255) NOT NULL UNIQUE,
  FOREIGN KEY (ClusterId) REFERENCES ProviderFactoryCluster(ClusterId)
);
CREATE INDEX ProviderFactoryClusterNode_ServerName_index ON ProviderFactoryClusterNode(ServerName);

CREATE TABLE InfraSite (
  SiteId SERIAL PRIMARY KEY,
  SiteName VARCHAR(255) NOT NULL UNIQUE,
  SiteDescription VARCHAR(255) NOT NULL
);

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

CREATE TABLE InfraServer(
  ServerId SERIAL PRIMARY KEY,
  ServerName VARCHAR(255) NOT NULL UNIQUE,
  ServerDescription VARCHAR(255) NOT NULL,
  ServermodelName VARCHAR(255) NOT NULL,
  ServermodelReleaseDistribution VARCHAR(255) NOT NULL,
  SiteName VARCHAR(255) NOT NULL,
  ZonesName VARCHAR(255) [] NOT NULL,
  ZonesIP VARCHAR(255) [] NOT NULL
);

CREATE TABLE SettingSource (
  SourceId SERIAL PRIMARY KEY,
  SourceName VARCHAR(255) NOT NULL UNIQUE,
  SourceDirectory TEXT
);

CREATE TABLE SettingRelease (
  ReleaseId SERIAL PRIMARY KEY,
  ReleaseName VARCHAR(255) NOT NULL,
  ReleaseSourceName VARCHAR(255) NOT NULL,
  ReleaseDistribution VARCHAR(20) NOT NULL,
  UNIQUE (ReleaseName, ReleaseSourceName),
  UNIQUE (ReleaseDistribution, ReleaseSourceName)
);

CREATE TABLE SettingServer(
  ServerId SERIAL PRIMARY KEY,
  ServerName VARCHAR(255) NOT NULL UNIQUE,
  ServermodelName VARCHAR(255) NOT NULL,
  ReleaseName VARCHAR(255) NOT NULL
);

CREATE TABLE SettingServermodel (
  ServermodelId SERIAL PRIMARY KEY,
  ServermodelName VARCHAR(255) NOT NULL,
  ServermodelDescription VARCHAR(255) NOT NULL,
  ServermodelParents VARCHAR(255) [] DEFAULT '{}',
  ReleaseDistribution VARCHAR(255) NOT NULL,
  ReleaseName VARCHAR(255) NOT NULL,
  ServermodelApplicationserviceId INTEGER NOT NULL,
  OSName VARCHAR(255),
  OSVersion VARCHAR(255),
  UNIQUE (ServermodelName, ReleaseDistribution)
);
CREATE INDEX ServermodelApplicationserviceId_index ON SettingServermodel (ServermodelApplicationserviceId);

CREATE TABLE SettingApplicationservice (
  ApplicationserviceId SERIAL PRIMARY KEY,
  ApplicationserviceName VARCHAR(255) NOT NULL,
  ApplicationserviceDescription VARCHAR(255) NOT NULL,
  ReleaseDistribution VARCHAR(255) NOT NULL,
  ReleaseName VARCHAR(255) NOT NULL,
  SourceName VARCHAR(255) NOT NULL,
  Compatibilities JSON,
  OSName VARCHAR(255),
  OSVersion VARCHAR(255),
  UNIQUE (ApplicationserviceName, ReleaseDistribution)
);

CREATE TABLE SettingApplicationserviceDependency (
  ApplicationserviceId INTEGER NOT NULL,
  ApplicationserviceDependencyId INTEGER NOT NULL,
  UNIQUE(ApplicationserviceId, ApplicationserviceDependencyId),
  FOREIGN KEY (ApplicationserviceId) REFERENCES SettingApplicationservice(ApplicationserviceId),
  FOREIGN KEY (ApplicationserviceDependencyId) REFERENCES SettingApplicationservice(ApplicationserviceId)
);

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
);

CREATE TABLE UserUser (
  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 UserUser(UserId)
);

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

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