PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;

CREATE TABLE IF NOT EXISTS users (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    google_id  TEXT UNIQUE,
    email      TEXT NOT NULL UNIQUE,
    name       TEXT NOT NULL,
    avatar_url TEXT,
    farbe      TEXT DEFAULT '#2563eb',
    created_at INTEGER DEFAULT (strftime('%s','now'))
);

CREATE TABLE IF NOT EXISTS reisen (
    id           TEXT PRIMARY KEY,
    name         TEXT NOT NULL,
    ziel         TEXT,
    start_datum  TEXT,
    end_datum    TEXT,
    cover_emoji  TEXT DEFAULT '✈️',
    beschreibung TEXT,
    created_by   INTEGER REFERENCES users(id),
    created_at   INTEGER DEFAULT (strftime('%s','now'))
);

CREATE TABLE IF NOT EXISTS reise_zugriff (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    trip_id    TEXT NOT NULL REFERENCES reisen(id) ON DELETE CASCADE,
    user_id    INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    access     TEXT NOT NULL DEFAULT 'see',
    invited_by INTEGER REFERENCES users(id),
    joined_at  INTEGER DEFAULT (strftime('%s','now')),
    UNIQUE(trip_id, user_id)
);

CREATE TABLE IF NOT EXISTS reise_einladungen (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    trip_id    TEXT NOT NULL REFERENCES reisen(id) ON DELETE CASCADE,
    email      TEXT NOT NULL,
    token      TEXT NOT NULL UNIQUE,
    access     TEXT DEFAULT 'see',
    invited_by INTEGER REFERENCES users(id),
    created_at INTEGER DEFAULT (strftime('%s','now')),
    used_at    INTEGER
);

CREATE TABLE IF NOT EXISTS ablauf (
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    trip_id      TEXT NOT NULL REFERENCES reisen(id) ON DELETE CASCADE,
    datum        TEXT,
    zeit         TEXT,
    titel        TEXT NOT NULL,
    beschreibung TEXT,
    ort          TEXT,
    kategorie    TEXT DEFAULT 'allgemein',
    sort_order   INTEGER DEFAULT 0,
    created_by   INTEGER REFERENCES users(id),
    created_at   INTEGER DEFAULT (strftime('%s','now'))
);

CREATE TABLE IF NOT EXISTS packliste (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    trip_id     TEXT NOT NULL REFERENCES reisen(id) ON DELETE CASCADE,
    name        TEXT NOT NULL,
    menge       TEXT,
    kat         TEXT DEFAULT 'sonstiges',
    packed      INTEGER DEFAULT 0,
    assigned_to INTEGER REFERENCES users(id),
    created_by  INTEGER REFERENCES users(id),
    created_at  INTEGER DEFAULT (strftime('%s','now'))
);

CREATE TABLE IF NOT EXISTS ideen (
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    trip_id      TEXT NOT NULL REFERENCES reisen(id) ON DELETE CASCADE,
    titel        TEXT NOT NULL,
    beschreibung TEXT,
    link         TEXT,
    status       TEXT DEFAULT 'offen',
    created_by   INTEGER REFERENCES users(id),
    created_at   INTEGER DEFAULT (strftime('%s','now'))
);

CREATE TABLE IF NOT EXISTS notizen (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    trip_id    TEXT NOT NULL REFERENCES reisen(id) ON DELETE CASCADE,
    titel      TEXT NOT NULL,
    inhalt     TEXT,
    created_by INTEGER REFERENCES users(id),
    updated_at INTEGER DEFAULT (strftime('%s','now')),
    created_at INTEGER DEFAULT (strftime('%s','now'))
);

CREATE TABLE IF NOT EXISTS dokumente (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    trip_id    TEXT NOT NULL REFERENCES reisen(id) ON DELETE CASCADE,
    name       TEXT NOT NULL,
    dateiname  TEXT NOT NULL,
    mime_type  TEXT,
    groesse    INTEGER,
    created_by INTEGER REFERENCES users(id),
    created_at INTEGER DEFAULT (strftime('%s','now'))
);
