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

CREATE TABLE users (
                       id INT AUTO_INCREMENT PRIMARY KEY,
                       email VARCHAR(255) NOT NULL UNIQUE,
                       username VARCHAR(255),
                       hashed_password TEXT NOT NULL,
                       created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                       updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE plans (
                       id INT AUTO_INCREMENT PRIMARY KEY,
                       name VARCHAR(255) NOT NULL UNIQUE, -- 'Free', 'Pro', 'Enterprise'
    -- Features des Plans als JSON.
                       features JSON NOT NULL,
                       stripe_price_id VARCHAR(255) UNIQUE,
                       created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                       updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

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

CREATE TABLE workspaces (
                            id INT AUTO_INCREMENT PRIMARY KEY,
                            name VARCHAR(255) NOT NULL,
                            owner_id INT NOT NULL,
                            plan_id INT NOT NULL,
                            subscription_status VARCHAR(255) NOT NULL, -- z.B. 'active', 'trialing'
                            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                            FOREIGN KEY (owner_id) REFERENCES users(id),
                            FOREIGN KEY (plan_id) REFERENCES plans(id)
);

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

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

CREATE TABLE roles (
                       id INT AUTO_INCREMENT PRIMARY KEY,
                       name VARCHAR(255) NOT NULL UNIQUE -- 'Workspace Owner', 'Admin', 'Developer', 'Viewer'
);

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

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

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

CREATE TABLE projects (
                          id INT AUTO_INCREMENT PRIMARY KEY,
                          workspace_id INT NOT NULL,
                          name VARCHAR(255) NOT NULL,
                          description TEXT,
                          created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                          updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                          FOREIGN KEY (workspace_id) REFERENCES workspaces(id) ON DELETE CASCADE
);

CREATE TABLE routes (
                        id INT AUTO_INCREMENT PRIMARY KEY,
                        project_id INT NOT NULL,
    -- Matching Rules
                        method VARCHAR(10) NOT NULL,
                        path TEXT NOT NULL,
                        request_body_contains TEXT,
    -- Protocol Dispatcher
                        protocol VARCHAR(20) NOT NULL CHECK(protocol IN ('REST_JSON', 'GRAPHQL', 'RAW_BYTESTREAM', 'JSON_RPC')),
                        schema_definition TEXT,
    -- Response Definition
                        response_mode VARCHAR(10) NOT NULL CHECK(response_mode IN ('STATIC', 'DYNAMIC')) DEFAULT 'STATIC',
                        response_script TEXT,
                        status_code INT NOT NULL DEFAULT 200,
    -- JSON für Header und TEXT für Body
                        response_headers JSON,
                        response_body TEXT,
                        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                        FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

-- ===========
--  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);
