
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 (
    id        TEXT NOT NULL CHECK(LENGTH(id) = 4),
    parent_id TEXT,
    name      TEXT NOT NULL,

    land_id   INTEGER,

    CONSTRAINT pk_origin PRIMARY KEY (id),
    CONSTRAINT sk_origin_name UNIQUE (name),
    CONSTRAINT fk_origin_origin FOREIGN KEY (parent_id) REFERENCES origin (id)
        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;


INSERT INTO country VALUES
('AT', 'AUT',  40, 'Österreich',  TRUE),
('DE', 'DEU', 276, 'Deutschland', FALSE),
('CZ', 'CZE', 203, 'Tschechien',  FALSE),
('HU', 'HUN', 348, 'Ungarn',      FALSE),
('SK', 'SVK', 703, 'Slowakei',    FALSE);

INSERT INTO currency VALUES
('EUR', 'Euro', '€', 10000),
('ATS', 'Schilling', 'S', 137603);

INSERT INTO AT_bundesland VALUES
(1, 'B',  'Bgld.', 'Burgenland'),
(2, 'K',  'Ktn.',  'Kärnten'),
(3, 'N',  'NÖ',    'Niederösterreich'),
(4, 'O',  'OÖ',    'Oberösterreich'),
(5, 'S',  'Sbg.',  'Salzburg'),
(6, 'St', 'Stmk.', 'Steiermark'),
(7, 'T',   NULL,   'Tirol'),
(8, 'V',  'Vbg.',  'Vorarlberg'),
(9, 'W',   NULL,   'Wien');

INSERT INTO origin VALUES
('AUSL',  NULL , 'Ausland', NULL),
('EUXX',  NULL , 'Verschnitt von mehreren Weinen der EU', NULL),
('OEST',  NULL , 'Österreich', NULL),
('BLXX', 'OEST', 'Bergland', NULL),
('SLXX', 'OEST', 'Steirerland', NULL),
('WLXX', 'OEST', 'Weinland', NULL),
('BLKA', 'BLXX', 'Kärnten', 2),
('BLOO', 'BLXX', 'Oberösterreich', 4),
('BLSB', 'BLXX', 'Salzburg', 5),
('BLTI', 'BLXX', 'Tirol', 7),
('BLVO', 'BLXX', 'Vorarlberg', 8),
('SLST', 'SLXX', 'Steiermark', 6),
('SLSS', 'SLST', 'Südsteiermark', NULL),
('SLVL', 'SLST', 'Vulkanland Steiermark', NULL),
('SLWS', 'SLST', 'Weststeiermark', NULL),
('WIEN', 'WLXX', 'Wien', 9),
('WLNO', 'WLXX', 'Niederösterreich', 3),
('WLBL', 'WLXX', 'Burgenland', 1),
('WLCA', 'WLNO', 'Carnuntum', NULL),
('WLKA', 'WLNO', 'Kamptal', NULL),
('WLKT', 'WLNO', 'Kremstal', NULL),
('WLTH', 'WLNO', 'Thermenregion', NULL),
('WLTT', 'WLNO', 'Traisental', NULL),
('WLWA', 'WLNO', 'Wachau', NULL),
('WLWG', 'WLNO', 'Wagram', NULL),
('WLWV', 'WLNO', 'Weinviertel', NULL),
('WLNS', 'WLBL', 'Neusiedlersee', NULL),
('WLLB', 'WLBL', 'Leithaberg', NULL),
('WLRO', 'WLBL', 'Rosalia', NULL),
('WLMB', 'WLBL', 'Mittelburgenland', NULL),
('WLEB', 'WLBL', 'Eisenberg', NULL);

INSERT INTO wine_variety VALUES
('BL', 'R', 'Blauburger', NULL),
('BB', 'R', 'Blauer Burgunder', 'Blauer Spätburgunder, Blauburgunder, Pinot Noir'),
('BP', 'R', 'Blauer Portugieser', NULL),
('BW', 'R', 'Blauer Wildbacher', NULL),
('BF', 'R', 'Blaufränkisch', 'Frankovka'),
('BM', 'W', 'Blütenmuskateller', NULL),
('BO', 'W', 'Bouvier', NULL),
('CF', 'R', 'Cabernet Franc', NULL),
('CS', 'R', 'Cabernet Sauvignon', NULL),
('CH', 'W', 'Chardonnay', 'Morillon'),
('FV', 'W', 'Frühroter Veltliner', 'Malvasier'),
('FU', 'W', 'Furmint', NULL),
('GW', 'W', 'Gemischter Satz weiß', NULL),
('GB', 'W', 'Goldburger', NULL),
('GL', 'W', 'Goldmuskateller', NULL),
('GR', 'W', 'Grauer Burgunder', 'Grauburgunder, Pinot Gris, Ruländer'),
('GV', 'W', 'Grüner Veltliner', 'Weißgipfler'),
('JU', 'W', 'Jubiläumsrebe', NULL),
('ME', 'R', 'Merlot', NULL),
('MT', 'W', 'Müller Thurgau', 'Rivaner'),
('MC', 'W', 'Muscaris', NULL),
('MO', 'W', 'Muskat Ottonel', NULL),
('MU', 'W', 'Muskateller', 'Gelber Muskateller, Roter Muskateller'),
('NB', 'W', 'Neuburger', NULL),
('RH', 'R', 'Rathay', NULL),
('RR', 'W', 'Riesling', 'Weißer Riesling, Rheinriesling, Roter Riesling'),
('RL', 'R', 'Roesler', NULL),
('RM', 'R', 'Rosenmuskateller', NULL),
('RV', 'W', 'Roter Veltliner', NULL),
('RG', 'W', 'Rotgipfler', NULL),
('SB', 'W', 'Sauvignon Blanc', NULL),
('SA', 'W', 'Scheurebe', 'Sämling 88'),
-- ('SP', '', 'Sortenverschnitt gleichgepresst', NULL),
('SR', 'R', 'Sortenverschnitt Rosé', NULL),
('SO', 'R', 'Sortenverschnitt rot', NULL),
('SW', 'W', 'Sortenverschnitt weiß', NULL),
('SG', 'W', 'Souvignier Gris', NULL),
('SL', 'R', 'St. Laurent', NULL),
('SY', 'W', 'Sylvaner', 'Grüner Sylvaner'),
('SH', 'R', 'Syrah', 'Shiraz'),
('TR', 'W', 'Traminer', 'Gewürztraminer, Roter Traminer, Gelber Traminer'),
('WB', 'W', 'Weißer Burgunder', 'Weißburgunder, Pinot Blanc, Klevner'),
('WR', 'W', 'Welschriesling', NULL),
('ZF', 'W', 'Zierfandler', 'Spätrot'),
('ZW', 'R', 'Zweigelt', 'Blauer Zweigelt, Rotburger');
