import everything from 'http://ns.nuke24.net/Schema/'
import everything from 'http://ns.nuke24.net/Schema/DataTypeTranslation/'
import everything from 'http://ns.nuke24.net/Schema/Application/'
import 'http://ns.nuke24.net/Schema/RDB/Schema'
import 'http://ns.nuke24.net/Schema/RDB/Sequence'
import 'http://ns.nuke24.net/Schema/RDB/initialValue'
import 'http://ns.nuke24.net/Schema/RDB/defaultValueSequence'
import 'http://ns.nuke24.net/Schema/RDB/isAutoIncremented'
import 'http://ns.nuke24.net/Schema/RDB/isInNamespace'
import 'http://ns.nuke24.net/Schema/RDB/isSelfKeyed'
import 'http://www.w3.org/2000/01/rdf-schema#isSubclassOf' as 'extends'

class 'integer' :
        SQL type @ "INT" :
        PHP type @ "int" : JSON type @ "number"
class 'unsigned integer' : extends(integer) :
	SQL type @ "INT UNSIGNED" : regex @ "\\d+"
class 'boolean' :
        SQL type @ "BOOLEAN" :
        PHP type @ "bool" : JSON type @ "boolean"
class 'string' :
        SQL type @ "VARCHAR(126)" :
        PHP type @ "string" : JSON type @ "string"
class 'normal ID' : extends(unsigned integer)
class 'entity ID' : extends(unsigned integer) : PHP type @ "string" : SQL type @ "BIGINT"
class 'code' : extends(string) : SQL type @ "CHAR(4)" : regex @ "[A-Za-z0-9 _-]{1,4}"
class 'text' : extends(string) : SQL type @ "TEXT"
class 'hash' : extends(string) : regex @ "[A-Fa-f0-9]{40}" : comment @ "Hex-encoded SHA-1 of something (40 bytes)"
class 'e-mail address' : extends(string)
class 'URI' : extends(string)
class 'time' : extends(string) : SQL type @ "TIMESTAMP"
class 'date' : extends(string) : SQL type @ "DATE"

schema 'phptemplateprojectdatabasenamespace'

sequence 'new entity ID' : initial value @ 1001 : is in namespace @ 'phptemplateprojectdatabasenamespace'

field modifier 'AIPK' = normal ID : is auto-incremented : key(primary)
field modifier 'EIPK' = entity ID : default value sequence @ new entity ID : key(primary)
# SRC = 'standard resource class'
field modifier 'SRC' = has a database table : has a REST service : is in namespace @ 'phptemplateprojectdatabasenamespace'

class 'user' : SRC {
	ID : EIPK
	username : string
	passhash : string : nullable
	e-mail address : e-mail address : nullable
}

class 'organization' : SRC : members are public {
	ID : EIPK
	name : string
}

class 'user organization attachment' : SRC : self-keyed {
	# first 'user' is the name of the property of a 'user organization attachment'
	# reference(user) means that this property is a reference to a 'user' record
	# { ID = user ID } means the 'ID' of the 'user' record matches the 'user ID' field of this record
	user : reference(user) {
		ID = user ID
	}
	organization : reference(organization) {
		ID = organization ID
	}
}

# Actual enums are hard.
# Best practice according to Dan is avoid defining enum types and just always use a table.
class 'computation status' : SRC : members are public {
	status code : string : key(primary)
}

class 'computation' : SRC : members are public {
	expression : string : key(primary)
	status : reference(computation status) = status code
	result : string : nullable
}
