CREATE DATABASE IF NOT EXISTS portfolio;
USE portfolio;

-- Mot de passe à changer après exécution du script (voir Bitwarden)
CREATE USER IF NOT EXISTS 'srv_portfolio'@'%' IDENTIFIED BY 'mdp';
GRANT ALL ON portfolio.* TO 'srv_portfolio'@'%';
FLUSH PRIVILEGES;

CREATE TABLE IF NOT EXISTS users (
    id           INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    username     VARCHAR(50)     NOT NULL,
    password     VARCHAR(255)    NOT NULL,
    role         ENUM('admin', 'user') NOT NULL DEFAULT 'user',
    created_at   DATETIME        NULL DEFAULT NULL,
    last_login   DATETIME        NULL DEFAULT NULL,
    PRIMARY KEY (id),
    UNIQUE INDEX idx_username (username)
);

INSERT IGNORE INTO users (id, created_at, last_login, password, role, username)
VALUES (11, '2026-06-08 11:10:21', '2026-06-08 13:10:41', '$2y$12$x01fDqrHQEYIyj7MDHa72eRyl2j/n5cNq9sv3.FIiQCaRsB50Q4iK', 'admin', 'admin');

CREATE TABLE IF NOT EXISTS projects (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    title VARCHAR(100) NOT NULL,
    status VARCHAR(100) NULL,
    tags VARCHAR(255) NULL,
    description TEXT NOT NULL,
    image_url VARCHAR(255) NOT NULL,
    markdown LONGTEXT NULL,
    link_url VARCHAR(255) NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS experiences (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    title VARCHAR(100) NOT NULL,
    status VARCHAR(100) NULL,
    xptype VARCHAR(100) NOT NULL,
    tags VARCHAR(255) NULL,
    description TEXT NOT NULL,
    duration VARCHAR(100) NOT NULL,
    image_url VARCHAR(255) NOT NULL,
    markdown LONGTEXT NULL,
    link_url VARCHAR(255) NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS schools (
    id          INT UNSIGNED NOT NULL AUTO_INCREMENT,
    title       VARCHAR(100) NOT NULL,
    status      VARCHAR(100) NULL,
    tags        VARCHAR(255) NULL,
    description TEXT         NOT NULL,
    duration    VARCHAR(100) NOT NULL,
    image_url   VARCHAR(255) NOT NULL,
    markdown    LONGTEXT     NULL,
    link_url    VARCHAR(255) NULL,
    created_at  DATETIME     DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

-- Réglages du site (clé / valeur), utilisés pour la notification de la page d'accueil et le captcha
CREATE TABLE IF NOT EXISTS settings (
    name  VARCHAR(50)  NOT NULL,
    value TEXT         NULL,
    PRIMARY KEY (name)
);

INSERT INTO settings (name, value) VALUES
('site_notice_title', 'Des bugs peuvent encore se produire.'),
('site_notice_text', 'Si le CSS ou les images s''affichent mal, pensez à vider le cache de votre navigateur (Ctrl + F5 ou Cmd + Shift + R) pour voir les dernières modifications.'),
('site_notice_enabled', '1'),
('captcha_enabled', '1'),
('contact_email', 'esteban.moussu@ik.me'),
('contact_email_enabled', '1'),
('contact_address_line1', '20 Grande rue'),
('contact_address_line2', '52500 Broncourt'),
('contact_address_enabled', '1')
ON DUPLICATE KEY UPDATE value = VALUES(value);

-- items : un item par ligne, au format "classes-icone|Texte" (icône optionnelle)
CREATE TABLE IF NOT EXISTS skills (
    id         INT UNSIGNED NOT NULL AUTO_INCREMENT,
    title      VARCHAR(100) NOT NULL,
    icon       VARCHAR(100) NULL,
    items      TEXT         NOT NULL,
    position   INT          NOT NULL DEFAULT 0,
    created_at DATETIME     DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

INSERT INTO skills (title, icon, items, position) VALUES
('Systèmes d''exploitation', 'fa-solid fa-computer', 'fa-brands fa-microsoft|Windows (Serveur/XP/8/10/11)\nfa-brands fa-linux|Linux (Debian/Ubuntu)\nfa-brands fa-apple|macOS', 1),
('Réseaux', 'fa-solid fa-network-wired', 'fa-solid fa-location-dot|Adressage IP\nfa-solid fa-diagram-project|VLAN\nfa-solid fa-shield-halved|Pare-feu (iptables/ufw)\nfa-solid fa-globe|DNS (Bind9)\nfa-solid fa-server|DHCP (ISC DHCP)\nfa-solid fa-arrows-left-right|Proxy (Squid)', 2),
('Scripting', 'fa-solid fa-terminal', 'fa-solid fa-terminal|Bash\nfa-brands fa-python|Python\nfa-brands fa-java|Java\nfa-solid fa-gears|Ansible', 3),
('Applications', 'fa-solid fa-cubes', 'fa-solid fa-address-book|LDAP (Windows AD)\nfa-solid fa-headset|GLPI\nfa-solid fa-compact-disc|FOG\nfa-solid fa-server|Nginx / Apache\nfa-brands fa-docker|Docker\nfa-solid fa-display|Guacamole\nfa-solid fa-database|MySQL / MariaDB', 4),
('Développement web', 'fa-solid fa-code', 'fa-brands fa-html5|HTML\nfa-brands fa-css3-alt|CSS\nfa-brands fa-js|JavaScript\nfa-brands fa-php|PHP', 5),
('Virtualisation & Outils', 'fa-solid fa-layer-group', 'fa-solid fa-server|Proxmox\nfa-solid fa-box|VirtualBox\nfa-brands fa-git-alt|Git (GitHub, Gitea)', 6);

-- category : 'tool' (bandeau "Outils que je sais manipuler") ou 'partner' (bandeau "Ils m'ont fait confiance")
-- icon : classes CSS Devicon, prioritaire sur logo_url si les deux sont renseignés (au moins un des deux requis)
CREATE TABLE IF NOT EXISTS logo_banners (
    id         INT UNSIGNED NOT NULL AUTO_INCREMENT,
    category   ENUM('tool', 'partner') NOT NULL,
    name       VARCHAR(100) NOT NULL,
    icon       VARCHAR(100) NULL,
    logo_url   VARCHAR(255) NULL,
    link_url   VARCHAR(255) NULL,
    position   INT          NOT NULL DEFAULT 0,
    created_at DATETIME     DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

-- PDF de projects/experiences/schools : tables associées plutôt qu'une colonne sur la table
-- parente, pour que les pages qui LISTENT ces éléments n'aient jamais à charger le contenu
-- binaire des PDF en mémoire (seul le nom de fichier y est utilisé)
CREATE TABLE IF NOT EXISTS project_pdfs (
    project_id   INT UNSIGNED NOT NULL,
    pdf_filename VARCHAR(255) NOT NULL,
    pdf_data     LONGBLOB     NOT NULL,
    PRIMARY KEY (project_id),
    CONSTRAINT fk_project_pdfs_project FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS experience_pdfs (
    experience_id INT UNSIGNED NOT NULL,
    pdf_filename  VARCHAR(255) NOT NULL,
    pdf_data      LONGBLOB     NOT NULL,
    PRIMARY KEY (experience_id),
    CONSTRAINT fk_experience_pdfs_experience FOREIGN KEY (experience_id) REFERENCES experiences(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS school_pdfs (
    school_id    INT UNSIGNED NOT NULL,
    pdf_filename VARCHAR(255) NOT NULL,
    pdf_data     LONGBLOB     NOT NULL,
    PRIMARY KEY (school_id),
    CONSTRAINT fk_school_pdfs_school FOREIGN KEY (school_id) REFERENCES schools(id) ON DELETE CASCADE
);
