/* SPDX-License-Identifier: WTFPL */

/* MariaDB Scheme 
   Version: 1 */

CREATE TABLE user (
    userid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    login VARCHAR(20) NOT NULL,
    pass BINARY(60) NOT NULL,
    displayname VARCHAR(100) NOT NULL,
    role SET('vessel', 'captain','helmsman','sailor') NOT NULL DEFAULT 'sailor',
    flags SET('deleted','locked'),
    language CHAR(2) NOT NULL DEFAULT 'en',
    PRIMARY KEY (userid),
    UNIQUE INDEX ix_login (login)
);

/* Settings
   E.g. the currently selected boat. Users logged in via the web are allowed
   to write to this table. Therefore a separate table and not additional
   fields in the user data record.
   
   userid=0: Global program settings
   sno=0: db_scheme_version
   sno=1: doc_base_path

   userid = -1: description of user settings for development and admin use

   userid >=1: setings per user
   sno=1 current vessel

   */
CREATE TABLE settings (
    userid SMALLINT(6) NOT NULL,
    sno SMALLINT(6) NOT NULL,
    valstr VARCHAR(200) DEFAULT NULL,
    valint INT(10) DEFAULT NULL,
    PRIMARY KEY (userid, sno)
);

/* ddid=0 is list of lists, not editable
   flags: fixed=not deletable, value not changeable
          locked=not editable */
CREATE TABLE dropdown (
    ddid TINYINT(3) UNSIGNED NOT NULL,
    ddval TINYINT(3) NOT NULL,
    sort TINYINT(3) DEFAULT NULL,
    ddtext VARCHAR(50) NOT NULL,
    ddshort VARCHAR(4) DEFAULT NULL,
    color CHAR(6) DEFAULT NULL,
    flags ENUM('locked', 'fixed') DEFAULT NULL,
    PRIMARY KEY (ddid, ddval)
);

CREATE TABLE vessel (
    vid SMALLINT(10) NOT NULL AUTO_INCREMENT,
    vtype ENUM('mono','cat','tri') NOT NULL DEFAULT 'mono',
    vesselname VARCHAR(40) NOT NULL,
    model VARCHAR(20) DEFAULT NULL,
    loa FLOAT(4,2) UNSIGNED DEFAULT NULL,
    lwl FLOAT(4,2) UNSIGNED DEFAULT NULL,
    beam FLOAT(4,2) UNSIGNED DEFAULT NULL,
    draught FLOAT(3,2) UNSIGNED DEFAULT NULL,
    draught_min FLOAT(3,2) UNSIGNED DEFAULT NULL,
    displacement SMALLINT(6) UNSIGNED DEFAULT NULL,
    ballast SMALLINT(6) UNSIGNED DEFAULT NULL,
    remarks VARCHAR(150) DEFAULT NULL,
    PRIMARY KEY (vid)
);

CREATE TABLE storage (
    sid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    vid SMALLINT(6) NOT NULL DEFAULT 1,
    sname VARCHAR(40) NOT NULL,
    stype TINYINT(3) NOT NULL DEFAULT 0,
    capacity SMALLINT(6) DEFAULT NULL,
    capaunit ENUM('kg', 'l') DEFAULT NULL,
    x SMALLINT(6) NOT NULL DEFAULT 0,
    y SMALLINT(6) NOT NULL DEFAULT 0,
    z SMALLINT(6) NOT NULL DEFAULT 0,
    remarks VARCHAR(150) DEFAULT NULL,
    PRIMARY KEY (sid)
);

/* TODO define cabins? CREATE TABLE cabin ... */


CREATE TABLE tag (
    tagid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    tagname VARCHAR(20) NOT NULL,
    color CHAR(6) DEFAULT NULL,
    PRIMARY KEY (tagid),
    UNIQUE INDEX ix_tagname (tagname)
);

CREATE TABLE tagref (
    tagid SMALLINT(6) NOT NULL,
    objid SMALLINT(6) NOT NULL,
    objtype ENUM('equip','inv','prov','doc') NOT NULL,
    PRIMARY KEY (tagid, objid, objtype)
);

/* TODO Boxes can be nested via parent
   TODO boxsize: (LxWxH) in cm? */
CREATE TABLE box (
    boxid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    sid SMALLINT(6) NOT NULL,
    parent INT(10) DEFAULT NULL,
    label VARCHAR(20) DEFAULT NULL,
    color CHAR(6) DEFAULT NULL,
    content VARCHAR(60) DEFAULT NULL,
    weight FLOAT(5,2) UNSIGNED DEFAULT NULL,
    remarks VARCHAR(150) DEFAULT NULL,
    PRIMARY KEY (boxid),
    INDEX ix_label (label)
);

CREATE TABLE equipment (
    eid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    vid SMALLINT(6) NOT NULL DEFAULT 1,
    ename VARCHAR(80) NOT NULL,
    make VARCHAR(40),
    model VARCHAR(40),
    ecat TINYINT(3) DEFAULT NULL,
    serial VARCHAR(30),
    supplier SMALLINT(6) DEFAULT NULL,
    manufacturer SMALLINT(6) DEFAULT NULL,
    purchdate DATE DEFAULT NULL,
    price DECIMAL(12,2) DEFAULT NULL,
    weight FLOAT(5,2) UNSIGNED DEFAULT NULL,
    remarks VARCHAR(150) DEFAULT NULL,
    PRIMARY KEY(eid)
);

CREATE TABLE inventory (
    invid INT(10) NOT NULL AUTO_INCREMENT,
    invname VARCHAR(80) NOT NULL,
    boxtype ENUM('storage','box') NOT NULL DEFAULT 'storage',
    sid SMALLINT(6) NOT NULL DEFAULT 1,
    boxid SMALLINT(6) NOT NULL DEFAULT 1,
    number SMALLINT(6) UNSIGNED NOT NULL DEFAULT 1,
    weight FLOAT(5,2) UNSIGNED DEFAULT NULL,
    price DECIMAL(12,2) DEFAULT NULL,
    purchdate DATE DEFAULT NULL,
    invcond ENUM('unknown','good','normal','bad','repairable','defect') NOT NULL default 'unknown',
    PRIMARY KEY (invid),
    INDEX ix_invname (invname)
);

CREATE TABLE measurement (
    mid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    mname VARCHAR(80) NOT NULL,
    nval TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,
    val1 INT(10) NOT NULL,
    val2 INT(10) NOT NULL,
    val3 INT(10) NOT NULL,
    unit TINYINT(3) NOT NULL DEFAULT 1,
    accuracy ENUM('unknown', 'precise', 'normal', 'rough', 'estimated') NOT NULL DEFAULT 'unknown',
    mdate DATE DEFAULT NULL,
    note VARCHAR(80) DEFAULT NULL,
    eid SMALLINT(6) DEFAULT NULL,
    PRIMARY KEY (mid)
);

CREATE TABLE provisions (
    provid INT(10) NOT NULL AUTO_INCREMENT,
    provname VARCHAR(40) NOT NULL,
    sid SMALLINT(6) DEFAULT NULL,
    number SMALLINT(6) UNSIGNED NOT NULL DEFAULT 1,
    unit TINYINT(3) DEFAULT NULL,
    storedate DATE DEFAULT CURRENT_DATE,
    shelflife DATE DEFAULT NULL,
    PRIMARY KEY (provid)
);

CREATE TABLE document (
    docid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    doctype ENUM('generic', 'manual', 'invoice', 'drawing', 'picture') NOT NULL DEFAULT 'generic',
    mimetype VARCHAR(80) NOT NULL DEFAULT 'application/octet-stream',
    filename VARCHAR(200) NOT NULL,
    extension CHAR(3) NOT NULL,
    hash VARCHAR(32) NOT NULL,
    docsize INT(10) unsigned NOT NULL,
    doctime DATETIME DEFAULT NULL,
    title VARCHAR(40) DEFAULT NULL,
    pages SMALLINT(6) unsigned NOT NULL DEFAULT 1,
    remarks VARCHAR(150) DEFAULT NULL,
    PRIMARY KEY (docid)
);

CREATE TABLE docref (
    drid INT(10) NOT NULL AUTO_INCREMENT,
    reftype ENUM('vessel', 'box', 'equipment', 'project', 'task') NOT NULL DEFAULT 'vessel',
    docid SMALLINT(6) NOT NULL,
    refid SMALLINT(6) NOT NULL,
    PRIMARY KEY (drid),
    UNIQUE INDEX ix_docref (docid, refid)
);

CREATE TABLE company (
    compid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    compname VARCHAR(40) NOT NULL,
    comptype TINYINT(3) NOT NULL DEFAULT 1,
    street VARCHAR(30),
    zip VARCHAR(10),
    city VARCHAR(30),
    country VARCHAR(30),
    contact VARCHAR(30),
    phone VARCHAR(30),
    email VARCHAR(50),
    web VARCHAR(40),
    customerno VARCHAR(20),
    contractno VARCHAR(20),
    remarks VARCHAR(150) DEFAULT NULL,
    PRIMARY KEY (compid),
    UNIQUE INDEX ix_compname (compname)
);


/* TODO optional field: plan date, target date, completion date */
CREATE TABLE maintenance (
    maintid INT(10) NOT NULL AUTO_INCREMENT,
    vid SMALLINT(6) NOT NULL DEFAULT 1,
    eid SMALLINT(6) DEFAULT NULL,
    series SMALLINT(6) DEFAULT NULL,
    activities VARCHAR(40) NOT NULL,
    remarks VARCHAR(150) DEFAULT NULL,
    maintstate ENUM('new','planned','ongoing','done') NOT NULL DEFAULT 'new',
    PRIMARY KEY (maintid)
);

/* TODO? time recording */

CREATE TABLE project (
    projid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    projname VARCHAR(40) NOT NULL,
    vid SMALLINT(6) NOT NULL DEFAULT 1,
    responsible SMALLINT(6) NOT NULL,
    startdate date DEFAULT NULL,
    duration SMALLINT(6) UNSIGNED DEFAULT NULL,
    costs_plan DECIMAL(12,2) DEFAULT NULL,
    costs_final DECIMAL(12,2) DEFAULT NULL,
    remarks VARCHAR(150) DEFAULT NULL,
    projstate ENUM('new','plan','ongoing','finished') NOT NULL DEFAULT 'new',
    PRIMARY KEY (projid)
);

CREATE TABLE task (
   taskid INT(10) NOT NULL AUTO_INCREMENT,
   projid SMALLINT(6) DEFAULT NULL,
   taskname VARCHAR(60) NOT NULL,
   taskstate ENUM('new','finished') NOT NULL DEFAULT 'new',
   PRIMARY KEY (taskid)
);

CREATE TABLE checklist (
    clid SMALLINT(6) NOT NULL AUTO_INCREMENT,
    title VARCHAR(30) NOT NULL,
    clstate ENUM('new','open','closed') NOT NULL DEFAULT 'new',
    PRIMARY KEY (clid)
);

CREATE TABLE checkitem (
    checkid INT(10) NOT NULL AUTO_INCREMENT,
    checkname VARCHAR(60) NOT NULL,
    groupid TINYINT(3) DEFAULT NULL,
    PRIMARY KEY (checkid)
);

CREATE TABLE checkref (
    clid SMALLINT(6) NOT NULL,
    checkid INT(10) NOT NULL,
    sort SMALLINT(6) DEFAULT NULL,
    PRIMARY KEY (clid, checkid)
);
