DROP TABLE IF EXISTS assessmentanswer CASCADE;
DROP TABLE IF EXISTS assessment CASCADE;
DROP TABLE IF EXISTS themaansprechpartner CASCADE;
DROP TABLE IF EXISTS ansprechpartner CASCADE;
DROP TABLE IF EXISTS fragen CASCADE;
DROP TABLE IF EXISTS thema CASCADE;
DROP TABLE IF EXISTS accesslog CASCADE;
DROP TABLE IF EXISTS benutzer_gruppen CASCADE;
DROP TABLE IF EXISTS gruppen CASCADE;
DROP TABLE IF EXISTS benutzer CASCADE;

CREATE TABLE benutzer (
    id SERIAL PRIMARY KEY,
    name VARCHAR(150) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    passwort_hash VARCHAR(255) NOT NULL,
    last_login TIMESTAMP NULL,
    is_active BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE gruppen (
    id SERIAL PRIMARY KEY,
    gruppenname VARCHAR(100) NOT NULL UNIQUE,
    info TEXT
);

CREATE TABLE benutzer_gruppen (
    benutzer_id INTEGER NOT NULL REFERENCES benutzer(id) ON DELETE CASCADE,
    gruppen_id INTEGER NOT NULL REFERENCES gruppen(id) ON DELETE CASCADE,
    PRIMARY KEY (benutzer_id, gruppen_id)
);

CREATE TABLE accesslog (
    id SERIAL PRIMARY KEY,
    datetime TIMESTAMP NOT NULL DEFAULT NOW(),
    userid INTEGER NOT NULL REFERENCES benutzer(id) ON DELETE CASCADE
);

CREATE TABLE thema (
    id SERIAL PRIMARY KEY,
    kurztitel VARCHAR(50) NOT NULL,
    titel VARCHAR(255) NOT NULL,
    infotext TEXT,
    zusatztext TEXT
);

CREATE TABLE fragen (
    id SERIAL PRIMARY KEY,
    themaid INTEGER NOT NULL REFERENCES thema(id) ON DELETE CASCADE,
    text TEXT NOT NULL
);

CREATE TABLE ansprechpartner (
    id SERIAL PRIMARY KEY,
    name VARCHAR(150) NOT NULL,
    email VARCHAR(255) NOT NULL,
    infotext TEXT
);

CREATE TABLE themaansprechpartner (
    themaid INTEGER NOT NULL REFERENCES thema(id) ON DELETE CASCADE,
    ansprechpartnerid INTEGER NOT NULL REFERENCES ansprechpartner(id) ON DELETE CASCADE,
    PRIMARY KEY (themaid, ansprechpartnerid)
);

CREATE TABLE assessment (
    id SERIAL PRIMARY KEY,
    datetime TIMESTAMP NOT NULL DEFAULT NOW(),
    userid INTEGER NOT NULL REFERENCES benutzer(id) ON DELETE CASCADE
);

CREATE TABLE assessmentanswer (
    assessmentid INTEGER NOT NULL REFERENCES assessment(id) ON DELETE CASCADE,
    themaid INTEGER NOT NULL REFERENCES thema(id) ON DELETE CASCADE,
    frageid INTEGER NOT NULL REFERENCES fragen(id) ON DELETE CASCADE,
    antwort BOOLEAN NOT NULL,
    PRIMARY KEY (assessmentid, frageid)
);

INSERT INTO benutzer (name, email, passwort_hash, is_active)
VALUES ('AdminUser', 'admin@dasunternehmen.local', 'topsecret', TRUE);

INSERT INTO gruppen (gruppenname, info)
VALUES ('Admins', 'Administratoren der Anwendung');

INSERT INTO benutzer_gruppen (benutzer_id, gruppen_id)
VALUES (1, 1);

INSERT INTO thema (kurztitel, titel, infotext, zusatztext)
VALUES
('TH1', 'Thema 1', 'Infotext zu Thema 1', 'Zusatztext zu Thema 1'),
('TH2', 'Thema 2', 'Infotext zu Thema 2', 'Zusatztext zu Thema 2'),
('TH3', 'Thema 3', 'Infotext zu Thema 3', 'Zusatztext zu Thema 3');

INSERT INTO fragen (themaid, text)
VALUES
(1, 'TH1 Nr 1'), (1, 'TH1 Nr 2'), (1, 'TH1 Nr 3'), (1, 'TH1 Nr 4'),
(1, 'TH1 Nr 5'), (1, 'TH1 Nr 6'), (1, 'TH1 Nr 7'), (1, 'TH1 Nr 8'),
(2, 'TH2 Nr 1'), (2, 'TH2 Nr 2'), (2, 'TH2 Nr 3'), (2, 'TH2 Nr 4'),
(2, 'TH2 Nr 5'), (2, 'TH2 Nr 6'), (2, 'TH2 Nr 7'), (2, 'TH2 Nr 8'),
(3, 'TH3 Nr 1'), (3, 'TH3 Nr 2'), (3, 'TH3 Nr 3'), (3, 'TH3 Nr 4'),
(3, 'TH3 Nr 5'), (3, 'TH3 Nr 6'), (3, 'TH3 Nr 7'), (3, 'TH3 Nr 8');
