-- schema.sql

-- Roles table
CREATE TABLE IF NOT EXISTS roles (
    role_id SERIAL PRIMARY KEY,
    role_name VARCHAR(50) UNIQUE NOT NULL
);

-- Users table
CREATE TABLE IF NOT EXISTS users (
    user_id SERIAL PRIMARY KEY,
    user_name VARCHAR(100) NOT NULL,
    password VARCHAR(255) NOT NULL
);

-- User-Role Mapping table (many-to-many relationship)
CREATE TABLE IF NOT EXISTS user_role_mapping (
    mapping_id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
    role_id INTEGER NOT NULL REFERENCES roles(role_id) ON DELETE CASCADE
);

-- Tunnels table
CREATE TABLE IF NOT EXISTS tunnels (
    tunnel_id SERIAL PRIMARY KEY,
    tunnel_name VARCHAR(100) NOT NULL,
    tunnel_type VARCHAR(50) NOT NULL
);

-- Mapping Requests table
CREATE TABLE IF NOT EXISTS mapping_requests (
    request_id SERIAL PRIMARY KEY,
    request_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    status VARCHAR(20) NOT NULL,
    user_id INTEGER REFERENCES users(user_id) ON DELETE SET NULL,
    tunnel_id INTEGER REFERENCES tunnels(tunnel_id) ON DELETE SET NULL
);
