/* 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,
    beltpos_aft float(4,2) UNSIGNED DEFAULT NULL,
    beltpos_bow float(4,2) UNSIGNED DEFAULT NULL,
    shapefile varchar(40) 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) UNSIGNED NOT NULL DEFAULT 0,
    y smallint(6) NOT NULL DEFAULT 0,
    z smallint(6) NOT NULL DEFAULT 0,
    wx smallint(6) UNSIGNED NOT NULL DEFAULT 0,
    wy smallint(6) UNSIGNED NOT NULL DEFAULT 0,
    wz smallint(6) UNSIGNED 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,
    shortname varchar(20) DEFAULT NULL,
    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',
    remarks varchar(150) DEFAULT NULL,
    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),
    UNIQUE INDEX ix_hash (hash)
) ENGINE=InnoDB;

CREATE TABLE docref (
    drid int(10) NOT NULL AUTO_INCREMENT,
    reftype enum('vessel', 'box', 'equipment', 'project', 'task', 'user') NOT NULL DEFAULT 'vessel',
    docid smallint(6) NOT NULL,
    refid smallint(6) NOT NULL,
    PRIMARY KEY (drid),
    UNIQUE INDEX ix_docref (docid, refid),
    FOREIGN KEY fx_docref_doc (docid)
        REFERENCES document (docid)
        ON DELETE RESTRICT
        ON UPDATE CASCADE
) ENGINE=InnoDB;

CREATE TABLE company (
    compid smallint(6) NOT NULL AUTO_INCREMENT,
    compname varchar(60) NOT NULL,
    comptype tinyint(3) NOT NULL DEFAULT 1,
    shortname varchar(20) DEFAULT NULL,
    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)
);
