
CREATE TABLE country (
    alpha2 TEXT    NOT NULL CHECK(LENGTH(alpha2) = 2),
    alpha3 TEXT    NOT NULL CHECK(LENGTH(alpha3) = 3),
    num    INTEGER NOT NULL CHECK(num >= 0),
    name   TEXT    NOT NULL,

    is_visible INTEGER NOT NULL CHECK(is_visible = TRUE OR is_visible = FALSE) DEFAULT TRUE,

    CONSTRAINT pk_country PRIMARY KEY (alpha2),
    CONSTRAINT sk_country_alpha3 UNIQUE (alpha3),
    CONSTRAINT sk_country_numeric UNIQUE (num),
    CONSTRAINT sk_country_name UNIQUE (name)
) STRICT;

CREATE TABLE currency (
    code     TEXT NOT NULL CHECK(LENGTH(code) = 3),
    name     TEXT NOT NULL,
    symbol   TEXT,

    one_euro INTEGER,

    CONSTRAINT pk_currency PRIMARY KEY (code)
) STRICT;

CREATE TABLE wine_variety (
    code    TEXT NOT NULL CHECK(LENGTH(code) = 2),
    type    TEXT NOT NULL CHECK(type IN ('R', 'W')),
    name    TEXT NOT NULL,
    comment TEXT,

    CONSTRAINT pk_wine_variety PRIMARY KEY (code)
) STRICT;

CREATE TABLE AT_bundesland (
    id     INTEGER NOT NULL CHECK(id >= 0),
    short1 TEXT    NOT NULL,
    short2 TEXT,
    name   TEXT    NOT NULL,

    CONSTRAINT pk_AT_bundesland PRIMARY KEY (id),
    CONSTRAINT sk_AT_bundesland_short1 UNIQUE (short1),
    CONSTRAINT sk_AT_bundesland_short2 UNIQUE (short2),
    CONSTRAINT sk_AT_bundesland_name UNIQUE (name)
) STRICT;

CREATE TABLE AT_gemeinde (
    gkz     INTEGER NOT NULL CHECK(gkz >= 10000 AND gkz <= 99999),
    land_id INTEGER NOT NULL CHECK(land_id = gkz / 10000),
    name    TEXT    NOT NULL,

    CONSTRAINT pk_AT_gemeinde PRIMARY KEY (gkz),
    CONSTRAINT fk_AT_gemeinde_AT_bundesland FOREIGN KEY (land_id) REFERENCES AT_bundesland (id)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) STRICT;

CREATE TABLE AT_ort (
    okz  INTEGER NOT NULL CHECK(okz > 0),
    gkz  INTEGER NOT NULL,
    name TEXT    NOT NULL,

    CONSTRAINT pk_AT_ort PRIMARY KEY (okz),
    CONSTRAINT fk_AT_ort_AT_gemeinde FOREIGN KEY (gkz) REFERENCES AT_gemeinde (gkz)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) STRICT;

CREATE TABLE AT_plz (
    plz  INTEGER NOT NULL CHECK(plz >= 1000 AND plz <= 9999),
    okz  INTEGER NOT NULL,
    id   INTEGER NOT NULL CHECK(id = plz * 100000 + okz),
    name TEXT    NOT NULL,

    CONSTRAINT pk_AT_plz PRIMARY KEY (plz, okz),
    CONSTRAINT sk_AT_plz_id UNIQUE (id),
    CONSTRAINT fk_AT_plz_AT_ort FOREIGN KEY (okz) REFERENCES AT_ort (okz)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) STRICT;

CREATE TABLE origin (
    code        TEXT NOT NULL CHECK(LENGTH(code) = 4),
    parent_code TEXT,
    name        TEXT NOT NULL,

    land_id     INTEGER,

    CONSTRAINT pk_origin PRIMARY KEY (code),
    CONSTRAINT sk_origin_name UNIQUE (name),
    CONSTRAINT fk_origin_origin FOREIGN KEY (parent_code) REFERENCES origin (code)
        ON UPDATE CASCADE
        ON DELETE RESTRICT,
    CONSTRAINT fk_origin_AT_bundesland FOREIGN KEY (land_id) REFERENCES AT_bundesland (id)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) STRICT;

-- Weinbauland
--CREATE TABLE wbl() STRICT;

-- Weinbauregion
--CREATE TABLE wbr() STRICT;

-- Generisches Weinbaugebiet
--CREATE TABLE wbgg() STRICT;

-- Spezifisches Weinbaugebeit
--CREATE TABLE wbgs() STRICT;

-- Großlage
CREATE TABLE wbgl (
    id INTEGER NOT NULL,

    CONSTRAINT pk_wbgl PRIMARY KEY (id)
) STRICT;

-- Weinbaugemeinde
CREATE TABLE wbgem (
    id INTEGER NOT NULL,

    CONSTRAINT pk_wbgem PRIMARY KEY (id)
) STRICT;

-- Weinbaukatastralgemeinde
CREATE TABLE wbkg (
    id INTEGER NOT NULL,

    CONSTRAINT pk_wbkg PRIMARY KEY (id)
) STRICT;

-- Ried
CREATE TABLE wbrd (
    id INTEGER NOT NULL,

    CONSTRAINT pk_wbrd PRIMARY KEY (id)
) STRICT;
