CREATE TABLE Issuer (
    code TEXT NOT NULL COLLATE NOCASE PRIMARY KEY,
    name TEXT NOT NULL COLLATE NOCASE
);

CREATE TABLE Currency (
    id INTEGER NOT NULL PRIMARY KEY,
    name TEXT NOT NULL COLLATE NOCASE,
    full_name TEXT NOT NULL COLLATE NOCASE
);

CREATE TABLE Value (
    id INTEGER NOT NULL PRIMARY KEY,
    text TEXT COLLATE NOCASE,
    numeric_value REAL,
    numerator INTEGER,
    denominator INTEGER,
    currency_id INTEGER,
    FOREIGN KEY (currency_id) REFERENCES Currency(id)
);

CREATE TABLE Demonetization (
    id INTEGER NOT NULL PRIMARY KEY,
    is_demonetized BOOLEAN NOT NULL,
    demonetization_date TEXT
);

CREATE TABLE Composition (
    id INTEGER NOT NULL PRIMARY KEY,
    text TEXT
);

CREATE TABLE Technique (
    id INTEGER NOT NULL PRIMARY KEY,
    text TEXT
);

CREATE TABLE Reverse (
    id INTEGER NOT NULL PRIMARY KEY,
    engravers TEXT,
    designers TEXT,
    description TEXT,
    lettering TEXT,
    lettering_scripts TEXT,
    unabridged_legend TEXT,
    lettering_translation TEXT,
    picture TEXT,
    thumbnail TEXT,
    picture_copyright TEXT,
    picture_copyright_url TEXT,
    picture_license_name TEXT,
    picture_license_url TEXT
);

CREATE TABLE Obverse (
    id INTEGER NOT NULL PRIMARY KEY,
    engravers TEXT,
    designers TEXT,
    description TEXT,
    lettering TEXT,
    lettering_scripts TEXT,
    unabridged_legend TEXT,
    lettering_translation TEXT,
    picture TEXT,
    thumbnail TEXT,
    picture_copyright TEXT,
    picture_copyright_url TEXT,
    picture_license_name TEXT,
    picture_license_url TEXT
);

CREATE TABLE Edge (
    id INTEGER NOT NULL PRIMARY KEY,
    engravers TEXT,
    designers TEXT,
    description TEXT,
    lettering TEXT,
    lettering_scripts TEXT,
    unabridged_legend TEXT,
    lettering_translation TEXT,
    picture TEXT,
    thumbnail TEXT,
    picture_copyright TEXT,
    picture_copyright_url TEXT,
    picture_license_name TEXT,
    picture_license_url TEXT
);

CREATE TABLE Watermark (
    id INTEGER NOT NULL PRIMARY KEY,
    engravers TEXT,
    designers TEXT,
    description TEXT,
    lettering TEXT,
    lettering_scripts TEXT,
    unabridged_legend TEXT,
    lettering_translation TEXT,
    picture TEXT,
    thumbnail TEXT,
    picture_copyright TEXT,
    picture_copyright_url TEXT,
    picture_license_name TEXT,
    picture_license_url TEXT
);

CREATE TABLE NumistaType (
    id INTEGER NOT NULL PRIMARY KEY,
    url TEXT,
    title TEXT NOT NULL COLLATE NOCASE,
    category TEXT NOT NULL CHECK (category IN ('coin', 'banknote', 'exonumia')),
    issuer_code TEXT,
    min_year INTEGER,
    max_year INTEGER,
    type TEXT COLLATE NOCASE,
    value_id INTEGER,
    demonetization_id INTEGER,
    shape TEXT,
    composition_id INTEGER,
    technique_id INTEGER,
    weight REAL,
    size REAL,
    thickness REAL,
    orientation TEXT CHECK (orientation IN ('coin', 'medal', 'variable', 'three', 'nine')),
    obverse_id INTEGER,
    reverse_id INTEGER,
    edge_id INTEGER,
    watermark_id INTEGER,
    series TEXT,
    commemorated_topic TEXT,
    comments TEXT,
    count INTEGER NOT NULL,
    color TEXT,
    FOREIGN KEY (issuer_code) REFERENCES Issuer(code),
    FOREIGN KEY (value_id) REFERENCES Value(id),
    FOREIGN KEY (demonetization_id) REFERENCES Demonetization(id),
    FOREIGN KEY (composition_id) REFERENCES Composition(id),
    FOREIGN KEY (technique_id) REFERENCES Technique(id),
    FOREIGN KEY (obverse_id) REFERENCES Obverse(id),
    FOREIGN KEY (reverse_id) REFERENCES Reverse(id),
    FOREIGN KEY (edge_id) REFERENCES Edge(id),
    FOREIGN KEY (watermark_id) REFERENCES Watermark(id)
);

CREATE TABLE RulerGroup (
    id INTEGER NOT NULL PRIMARY KEY,
    name TEXT NOT NULL 
);

CREATE TABLE Ruler (
    id INTEGER NOT NULL PRIMARY KEY,
    name TEXT NOT NULL,
    wikidata_id TEXT,
    group_id INTEGER,
    FOREIGN KEY (group_id) REFERENCES RulerGroup(id)
);

CREATE TABLE TypeRuler (
    type_id INTEGER NOT NULL,
    ruler_id INTEGER NOT NULL,
    FOREIGN KEY (type_id) REFERENCES NumistaType(id),
    FOREIGN KEY (ruler_id) REFERENCES Ruler(id),
    UNIQUE(type_id, ruler_id)
);

CREATE TABLE TypeRelated (
    type1_id INTEGER NOT NULL,
    type2_id INTEGER NOT NULL,
    FOREIGN KEY (type1_id) REFERENCES NumistaType(id),
    FOREIGN KEY (type2_id) REFERENCES NumistaType(id),
    UNIQUE(type1_id, type2_id)
);