-- Migration initiale MatosBox (SQLite)

CREATE TABLE IF NOT EXISTS objet (
    id TEXT PRIMARY KEY,
    nom TEXT NOT NULL,
    description TEXT,
    quantite INTEGER NOT NULL DEFAULT 0,
    prix_achat REAL,
    date_achat DATETIME,
    boutique TEXT,
    numero_serie TEXT,
    numero_modele TEXT,
    fabricant TEXT,
    statut TEXT NOT NULL DEFAULT 'en_stock' CHECK (statut IN ('en_stock','pret','hors_service','archive')),
    caracteristiques TEXT,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS categorie (
    id TEXT PRIMARY KEY,
    nom TEXT NOT NULL,
    parent_id TEXT,
    slug TEXT,
    icone TEXT,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (parent_id) REFERENCES categorie(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS emplacement (
    id TEXT PRIMARY KEY,
    nom TEXT NOT NULL,
    parent_id TEXT,
    slug TEXT,
    piece TEXT,
    meuble TEXT,
    numero_boite TEXT,
    icone TEXT,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (parent_id) REFERENCES emplacement(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS lien_objet_emplacement (
    id TEXT PRIMARY KEY,
    objet_id TEXT NOT NULL,
    emplacement_id TEXT NOT NULL,
    type TEXT NOT NULL DEFAULT 'stocke' CHECK (type IN ('stocke','utilise_dans')),
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (objet_id) REFERENCES objet(id) ON DELETE CASCADE,
    FOREIGN KEY (emplacement_id) REFERENCES emplacement(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS champ_personnalise (
    id TEXT PRIMARY KEY,
    objet_id TEXT NOT NULL,
    nom_champ TEXT NOT NULL,
    type_champ TEXT NOT NULL DEFAULT 'string' CHECK (type_champ IN ('string','int','bool','date')),
    valeur TEXT,
    unite TEXT,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (objet_id) REFERENCES objet(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS piece_jointe (
    id TEXT PRIMARY KEY,
    objet_id TEXT NOT NULL,
    nom_fichier TEXT NOT NULL,
    chemin TEXT NOT NULL,
    type_mime TEXT NOT NULL,
    est_principale INTEGER NOT NULL DEFAULT 0,
    categorie TEXT NOT NULL DEFAULT 'image' CHECK (categorie IN ('image','pdf_notice','markdown_tuto')),
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (objet_id) REFERENCES objet(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_categorie_parent_id ON categorie(parent_id);
CREATE INDEX IF NOT EXISTS idx_emplacement_parent_id ON emplacement(parent_id);
CREATE INDEX IF NOT EXISTS idx_lien_objet_emplacement_objet_id ON lien_objet_emplacement(objet_id);
CREATE INDEX IF NOT EXISTS idx_lien_objet_emplacement_emplacement_id ON lien_objet_emplacement(emplacement_id);
CREATE INDEX IF NOT EXISTS idx_champ_personnalise_objet_id ON champ_personnalise(objet_id);
CREATE INDEX IF NOT EXISTS idx_piece_jointe_objet_id ON piece_jointe(objet_id);
