CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    username VARCHAR(200) NOT NULL,
    email VARCHAR(250) NOT NULL UNIQUE,
    password TEXT NOT NULL,
    roles VARCHAR(250) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS status (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    nom VARCHAR(200) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS mentions (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    nom VARCHAR(250) NOT NULL,
    uniter VARCHAR(50) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS niveaus (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    nom VARCHAR(50) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS etudiants (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    nom VARCHAR(250) NOT NULL,
    prenom VARCHAR(250) NOT NULL,
    photos TEXT NOT NULL,
    date_de_naissances DATE NOT NULL,
    niveau VARCHAR(250) NOT NULL,
    annee_scolaire VARCHAR(20) NOT NULL,
    status INTEGER NOT NULL,
    mention_id INTEGER NOT NULL,
    num_inscription TEXT NOT NULL UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (status) REFERENCES status(id),
    FOREIGN KEY (mention_id) REFERENCES mentions(id)
);

CREATE TABLE IF NOT EXISTS matieres (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    nom VARCHAR(250) UNIQUE NOT NULL,
    unite_enseignement VARCHAR(250) NOT NULL,
    credit INTEGER NOT NULL,
    heure INTEGER NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS semestres (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    nom VARCHAR(30) UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS matiere_mention (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    matiere_id INTEGER NOT NULL,
    mention_id INTEGER NOT NULL,
    FOREIGN KEY (matiere_id) REFERENCES matieres(id),
    FOREIGN KEY (mention_id) REFERENCES mentions(id)
);

CREATE TABLE IF NOT EXISTS matiere_semestre (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    matiere_id INTEGER NOT NULL,
    semestre_id INTEGER NOT NULL,
    mention_id INTEGER NOT NULL,
    FOREIGN KEY (matiere_id) REFERENCES matieres(id),
    FOREIGN KEY (semestre_id) REFERENCES semestres(id),
    FOREIGN KEY (mention_id) REFERENCES mentions(id)
);

CREATE TABLE IF NOT EXISTS notes (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    etudiant_id INTEGER NOT NULL,
    matiere_id INTEGER NOT NULL,
    etudiant_niveau VARCHAR(50) NOT NULL,
    mention_id INTEGER NOT NULL,
    note FLOAT DEFAULT NULL,
    annee_scolaire VARCHAR(50) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (etudiant_id) REFERENCES etudiants(id),
    FOREIGN KEY (matiere_id) REFERENCES matieres(id),
    FOREIGN KEY (mention_id) REFERENCES mentions(id)
);

CREATE TABLE IF NOT EXISTS notesrepech (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    etudiant_id INTEGER NOT NULL,
    matiere_id INTEGER NOT NULL,
    etudiant_niveau VARCHAR(50) NOT NULL,
    mention_id INTEGER NOT NULL,
    note FLOAT DEFAULT NULL,
    annee_scolaire VARCHAR(50) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (etudiant_id) REFERENCES etudiants(id),
    FOREIGN KEY (matiere_id) REFERENCES matieres(id),
    FOREIGN KEY (mention_id) REFERENCES mentions(id)
);

CREATE TABLE IF NOT EXISTS notesystems (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    admis FLOAT NOT NULL DEFAULT 10,
    redouble FLOAT NOT NULL DEFAULT 9.99,
    renvoyer FLOAT NOT NULL DEFAULT 7.99,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS anneescolaire (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    code VARCHAR(30) NOT NULL,
    debut DATE NOT NULL,
    fin DATE NOT NULL,
    is_current INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS traitmentsystem (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    code VARCHAR(30) NOT NULL,
    debut DATE NOT NULL,
    fin DATE NOT NULL,
    is_finished INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
