PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS users (
    userID             TEXT    NOT NULL PRIMARY KEY,
    username           TEXT    NOT NULL UNIQUE,
    passwordHash       TEXT    NOT NULL,
    role               TEXT    NOT NULL CHECK(role IN ('admin','supervisor','coach')),
    entityID           TEXT    NOT NULL,
    mustChangePassword INTEGER NOT NULL DEFAULT 1,
    createdAt          INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS admin (
    adminID  TEXT NOT NULL PRIMARY KEY,
    username TEXT NOT NULL,
    location TEXT NOT NULL DEFAULT ''
);

CREATE TABLE IF NOT EXISTS supervisor (
    supervisorID TEXT NOT NULL PRIMARY KEY,
    username     TEXT NOT NULL,
    location     TEXT NOT NULL DEFAULT ''
);

CREATE TABLE IF NOT EXISTS coach (
    coachID      TEXT NOT NULL PRIMARY KEY,
    supervisorID TEXT NOT NULL,
    username     TEXT NOT NULL,
    FOREIGN KEY(supervisorID) REFERENCES supervisor(supervisorID)
);

CREATE TABLE IF NOT EXISTS client (
    clientCode TEXT NOT NULL PRIMARY KEY,
    coachID    TEXT NOT NULL,
    FOREIGN KEY(coachID) REFERENCES coach(coachID)
);

CREATE TABLE IF NOT EXISTS questionnaire (
    questionnaireID TEXT    NOT NULL PRIMARY KEY,
    name            TEXT    NOT NULL DEFAULT '',
    version         TEXT    NOT NULL DEFAULT '',
    state           TEXT    NOT NULL DEFAULT '',
    orderIndex      INTEGER NOT NULL DEFAULT 0,
    showPoints      INTEGER NOT NULL DEFAULT 0,
    conditionJson   TEXT    NOT NULL DEFAULT '{}'
);

CREATE TABLE IF NOT EXISTS question (
    questionID      TEXT    NOT NULL PRIMARY KEY,
    questionnaireID TEXT    NOT NULL,
    defaultText     TEXT    NOT NULL DEFAULT '',
    type            TEXT    NOT NULL DEFAULT '',
    orderIndex      INTEGER NOT NULL DEFAULT 0,
    isRequired      INTEGER NOT NULL DEFAULT 0,
    configJson      TEXT    NOT NULL DEFAULT '{}',
    FOREIGN KEY(questionnaireID) REFERENCES questionnaire(questionnaireID)
);

CREATE TABLE IF NOT EXISTS answer_option (
    answerOptionID TEXT    NOT NULL PRIMARY KEY,
    questionID     TEXT    NOT NULL,
    defaultText    TEXT    NOT NULL DEFAULT '',
    points         INTEGER NOT NULL DEFAULT 0,
    orderIndex     INTEGER NOT NULL DEFAULT 0,
    nextQuestionId TEXT    NOT NULL DEFAULT '',
    FOREIGN KEY(questionID) REFERENCES question(questionID)
);

CREATE TABLE IF NOT EXISTS client_answer (
    clientCode     TEXT NOT NULL,
    questionID     TEXT NOT NULL,
    answerOptionID TEXT,
    freeTextValue  TEXT,
    numericValue   REAL,
    answeredAt     INTEGER,
    PRIMARY KEY (clientCode, questionID),
    FOREIGN KEY(clientCode)     REFERENCES client(clientCode),
    FOREIGN KEY(questionID)     REFERENCES question(questionID),
    FOREIGN KEY(answerOptionID) REFERENCES answer_option(answerOptionID)
);

CREATE TABLE IF NOT EXISTS completed_questionnaire (
    clientCode      TEXT    NOT NULL,
    questionnaireID TEXT    NOT NULL,
    assignedByCoach TEXT,
    status          TEXT    NOT NULL DEFAULT '',
    startedAt       INTEGER,
    completedAt     INTEGER,
    uploadedAt      INTEGER,
    sumPoints       INTEGER,
    PRIMARY KEY (clientCode, questionnaireID),
    FOREIGN KEY(clientCode)      REFERENCES client(clientCode),
    FOREIGN KEY(questionnaireID) REFERENCES questionnaire(questionnaireID),
    FOREIGN KEY(assignedByCoach) REFERENCES coach(coachID)
);

CREATE TABLE IF NOT EXISTS questionnaire_submission (
    submissionID      TEXT    NOT NULL PRIMARY KEY,
    clientCode        TEXT    NOT NULL,
    questionnaireID   TEXT    NOT NULL,
    assignedByCoach   TEXT,
    status            TEXT    NOT NULL DEFAULT 'completed',
    startedAt         INTEGER,
    completedAt       INTEGER,
    uploadedAt        INTEGER NOT NULL,
    sumPoints         INTEGER,
    submissionVersion INTEGER NOT NULL DEFAULT 1,
    submittedByUserID TEXT,
    submittedByRole   TEXT,
    FOREIGN KEY(clientCode)      REFERENCES client(clientCode),
    FOREIGN KEY(questionnaireID) REFERENCES questionnaire(questionnaireID),
    FOREIGN KEY(assignedByCoach) REFERENCES coach(coachID)
);

CREATE TABLE IF NOT EXISTS submission_answer (
    submissionID   TEXT NOT NULL,
    questionID     TEXT NOT NULL,
    answerOptionID TEXT,
    freeTextValue  TEXT,
    numericValue   REAL,
    answeredAt     INTEGER,
    PRIMARY KEY (submissionID, questionID),
    FOREIGN KEY(submissionID)   REFERENCES questionnaire_submission(submissionID),
    FOREIGN KEY(questionID)     REFERENCES question(questionID),
    FOREIGN KEY(answerOptionID) REFERENCES answer_option(answerOptionID)
);

CREATE TABLE IF NOT EXISTS activity_event (
    eventID         TEXT    NOT NULL PRIMARY KEY,
    createdAt       INTEGER NOT NULL,
    actorUserID     TEXT,
    actorUsername   TEXT,
    actorRole       TEXT,
    actorEntityID   TEXT,
    action          TEXT    NOT NULL,
    targetType      TEXT,
    targetID        TEXT,
    summary         TEXT    NOT NULL DEFAULT '',
    detailsJson     TEXT    NOT NULL DEFAULT '{}'
);

CREATE TABLE IF NOT EXISTS question_translation (
    questionID   TEXT NOT NULL,
    languageCode TEXT NOT NULL,
    text         TEXT NOT NULL DEFAULT '',
    PRIMARY KEY (questionID, languageCode),
    FOREIGN KEY(questionID) REFERENCES question(questionID)
);

CREATE TABLE IF NOT EXISTS answer_option_translation (
    answerOptionID TEXT NOT NULL,
    languageCode   TEXT NOT NULL,
    text           TEXT NOT NULL DEFAULT '',
    PRIMARY KEY (answerOptionID, languageCode),
    FOREIGN KEY(answerOptionID) REFERENCES answer_option(answerOptionID)
);

CREATE TABLE IF NOT EXISTS string_translation (
    stringKey    TEXT NOT NULL,
    languageCode TEXT NOT NULL,
    text         TEXT NOT NULL DEFAULT '',
    PRIMARY KEY (stringKey, languageCode)
);

CREATE TABLE IF NOT EXISTS language (
    languageCode TEXT NOT NULL PRIMARY KEY,
    name         TEXT NOT NULL DEFAULT ''
);

CREATE TABLE IF NOT EXISTS session (
    token     TEXT    NOT NULL PRIMARY KEY,
    userID    TEXT    NOT NULL,
    role      TEXT    NOT NULL,
    entityID  TEXT    NOT NULL DEFAULT '',
    createdAt INTEGER NOT NULL,
    expiresAt INTEGER NOT NULL,
    temp      INTEGER NOT NULL DEFAULT 0
);
