-- ===========
--  Users & Plans
-- ===========

CREATE TABLE users
(
    id              INTEGER PRIMARY KEY,
    email           TEXT NOT NULL UNIQUE,
    username        TEXT,
    hashed_password TEXT NOT NULL,
    created_at      TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at      TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE plans
(
    id              INTEGER PRIMARY KEY,
    name            TEXT NOT NULL UNIQUE, -- 'Free', 'Pro', 'Enterprise'
    -- Features des Plans als JSON. Flexibel für zukünftige Änderungen.
    features        TEXT NOT NULL,
    stripe_price_id TEXT UNIQUE,
    created_at      TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at      TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- ===========
--  Tenancy (Workspaces & Members)
-- ===========

CREATE TABLE workspaces
(
    id                  INTEGER PRIMARY KEY,
    name                TEXT    NOT NULL,
    owner_id            INTEGER NOT NULL REFERENCES users (id),
    plan_id             INTEGER NOT NULL REFERENCES plans (id),
    subscription_status TEXT    NOT NULL, -- z.B. 'active', 'trialing'
    created_at          TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at          TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE workspace_members
(
    user_id      INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
    workspace_id INTEGER NOT NULL REFERENCES workspaces (id) ON DELETE CASCADE,
    role_id      INTEGER NOT NULL REFERENCES roles (id),
    joined_at    TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (user_id, workspace_id)
);

-- ===========
--  RBAC (Roles & Permissions)
-- ===========

CREATE TABLE roles
(
    id   INTEGER PRIMARY KEY,
    name TEXT NOT NULL UNIQUE -- 'Workspace Owner', 'Admin', 'Developer', 'Viewer'
);

CREATE TABLE permissions
(
    id          INTEGER PRIMARY KEY,
    action      TEXT NOT NULL UNIQUE, -- 'project:create', 'route:delete', etc.
    description TEXT
);

CREATE TABLE role_permissions
(
    role_id       INTEGER NOT NULL REFERENCES roles (id) ON DELETE CASCADE,
    permission_id INTEGER NOT NULL REFERENCES permissions (id) ON DELETE CASCADE,
    PRIMARY KEY (role_id, permission_id)
);

-- ===========
--  Product Logic (Projects & Routes)
-- ===========

CREATE TABLE projects
(
    id           INTEGER PRIMARY KEY,
    workspace_id INTEGER NOT NULL REFERENCES workspaces (id) ON DELETE CASCADE,
    name         TEXT    NOT NULL,
    description  TEXT,
    created_at   TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at   TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE routes
(
    id                    INTEGER PRIMARY KEY,
    project_id            INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
    -- Matching Rules
    method                TEXT    NOT NULL,
    path                  TEXT    NOT NULL,
    request_body_contains TEXT,
    -- Protocol Dispatcher (kompatibel mit SQLite & Postgres)
    protocol              TEXT    NOT NULL CHECK (protocol IN ('REST_JSON', 'GRAPHQL', 'RAW_BYTESTREAM', 'JSON_RPC')),
    schema_definition     TEXT,
    -- Response Definition
    response_mode         TEXT    NOT NULL CHECK (response_mode IN ('STATIC', 'DYNAMIC')) DEFAULT 'STATIC',
    response_script       TEXT,
    status_code           INTEGER NOT NULL                                                DEFAULT 200,
    -- Für SQLite: TEXT-Spalte, die JSON enthält.
    response_headers      TEXT, -- In Postgres: JSONB
    response_body         TEXT,
    created_at            TIMESTAMP WITH TIME ZONE                                        DEFAULT CURRENT_TIMESTAMP,
    updated_at            TIMESTAMP WITH TIME ZONE                                        DEFAULT CURRENT_TIMESTAMP
);

-- ===========
--  Indizes für schnelle Abfragen
-- ===========

CREATE INDEX idx_workspaces_owner_id ON workspaces (owner_id);
CREATE INDEX idx_projects_workspace_id ON projects (workspace_id);
CREATE INDEX idx_routes_project_id ON routes (project_id);
