CREATE TABLE IF NOT EXISTS stl_files (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    filename VARCHAR(255) NOT NULL,
    original_name VARCHAR(255) NOT NULL,
    file_path VARCHAR(500) NOT NULL UNIQUE,
    file_size INTEGER NOT NULL,
    upload_date DATETIME DEFAULT CURRENT_TIMESTAMP,
    last_modified DATETIME DEFAULT CURRENT_TIMESTAMP,
    description TEXT,
    tags VARCHAR(500),
    print_settings JSON,
    dimensions JSON,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_filename ON stl_files(filename);
CREATE INDEX IF NOT EXISTS idx_upload_date ON stl_files(upload_date);
CREATE INDEX IF NOT EXISTS idx_tags ON stl_files(tags);

CREATE TRIGGER IF NOT EXISTS update_stl_files_timestamp 
    AFTER UPDATE ON stl_files
BEGIN
    UPDATE stl_files SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;