-- WBX Spring Framework — Oracle 스키마

CREATE TABLE wbx_users (
    id                    NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    email                 VARCHAR2(255) NOT NULL UNIQUE,
    username              VARCHAR2(100) NOT NULL UNIQUE,
    hashed_password       VARCHAR2(500),
    full_name             VARCHAR2(255),
    phone                 VARCHAR2(50),
    department_id         NUMBER(19),
    position_title        VARCHAR2(100),
    employee_number       VARCHAR2(50) UNIQUE,
    is_active             NUMBER(1) DEFAULT 1,
    is_admin              NUMBER(1) DEFAULT 0,
    mfa_enabled           NUMBER(1) DEFAULT 0,
    azure_oid             VARCHAR2(255),
    sso_provider          VARCHAR2(50),
    failed_login_attempts NUMBER(10) DEFAULT 0,
    last_failed_login     TIMESTAMP,
    locked_until          TIMESTAMP,
    password_changed_at   TIMESTAMP,
    must_change_password  NUMBER(1) DEFAULT 0,
    last_login_at         TIMESTAMP,
    created_at            TIMESTAMP DEFAULT SYSTIMESTAMP,
    updated_at            TIMESTAMP DEFAULT SYSTIMESTAMP,
    created_by            NUMBER(19),
    updated_by            NUMBER(19)
);

CREATE TABLE wbx_roles (
    id          NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    code        VARCHAR2(50) NOT NULL UNIQUE,
    name        VARCHAR2(100) NOT NULL,
    description VARCHAR2(500),
    is_system   NUMBER(1) DEFAULT 0,
    created_at  TIMESTAMP DEFAULT SYSTIMESTAMP,
    updated_at  TIMESTAMP DEFAULT SYSTIMESTAMP
);

CREATE TABLE wbx_user_roles (
    id      NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    user_id NUMBER(19) NOT NULL REFERENCES wbx_users(id),
    role_id NUMBER(19) NOT NULL REFERENCES wbx_roles(id),
    CONSTRAINT uk_wbx_user_role UNIQUE (user_id, role_id)
);

CREATE TABLE wbx_role_permissions (
    id         NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    role_id    NUMBER(19) NOT NULL REFERENCES wbx_roles(id),
    module     VARCHAR2(100) NOT NULL,
    action     VARCHAR2(50) NOT NULL,
    dept_scope VARCHAR2(20) DEFAULT 'OWN',
    CONSTRAINT uk_wbx_role_perm UNIQUE (role_id, module, action)
);

CREATE TABLE wbx_refresh_tokens (
    id          NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    user_id     NUMBER(19) NOT NULL REFERENCES wbx_users(id),
    token_hash  VARCHAR2(500) NOT NULL,
    device_info VARCHAR2(500),
    ip_address  VARCHAR2(50),
    expires_at  TIMESTAMP NOT NULL,
    created_at  TIMESTAMP DEFAULT SYSTIMESTAMP
);

CREATE TABLE wbx_totp_secrets (
    id               NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    user_id          NUMBER(19) NOT NULL UNIQUE REFERENCES wbx_users(id),
    encrypted_secret VARCHAR2(500) NOT NULL,
    verified         NUMBER(1) DEFAULT 0,
    backup_codes     VARCHAR2(2000),
    created_at       TIMESTAMP DEFAULT SYSTIMESTAMP,
    updated_at       TIMESTAMP DEFAULT SYSTIMESTAMP,
    created_by       NUMBER(19),
    updated_by       NUMBER(19)
);

CREATE TABLE wbx_login_history (
    id             NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    user_id        NUMBER(19),
    email          VARCHAR2(255),
    action         VARCHAR2(50),
    auth_method    VARCHAR2(50),
    ip_address     VARCHAR2(50),
    user_agent     VARCHAR2(500),
    failure_reason VARCHAR2(500),
    created_at     TIMESTAMP DEFAULT SYSTIMESTAMP
);

CREATE TABLE wbx_notifications (
    id         NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    user_id    NUMBER(19) NOT NULL REFERENCES wbx_users(id),
    title      VARCHAR2(500),
    message    CLOB,
    type       VARCHAR2(50),
    is_read    NUMBER(1) DEFAULT 0,
    link       VARCHAR2(1000),
    created_at TIMESTAMP DEFAULT SYSTIMESTAMP
);

CREATE TABLE wbx_audit_logs (
    id          NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    user_id     NUMBER(19),
    username    VARCHAR2(255),
    action      VARCHAR2(50) NOT NULL,
    resource    VARCHAR2(100) NOT NULL,
    resource_id NUMBER(19),
    detail      VARCHAR2(4000),
    ip_address  VARCHAR2(50),
    created_at  TIMESTAMP DEFAULT SYSTIMESTAMP
);

CREATE TABLE wbx_file_uploads (
    id            NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    user_id       NUMBER(19),
    original_name VARCHAR2(500),
    stored_name   VARCHAR2(500),
    file_key      VARCHAR2(500),
    content_type  VARCHAR2(200),
    file_size     NUMBER(19),
    category      VARCHAR2(100),
    created_at    TIMESTAMP DEFAULT SYSTIMESTAMP
);

CREATE TABLE wbx_system_config (
    id           NUMBER(19) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    config_key   VARCHAR2(100) NOT NULL UNIQUE,
    config_value VARCHAR2(4000),
    value_type   VARCHAR2(20) DEFAULT 'STRING',
    description  VARCHAR2(500),
    is_editable  NUMBER(1) DEFAULT 1,
    updated_at   TIMESTAMP DEFAULT SYSTIMESTAMP,
    updated_by   NUMBER(19)
);
