-- WBX Spring Framework — MSSQL 스키마

CREATE TABLE wbx_users (
    id                    BIGINT IDENTITY(1,1) PRIMARY KEY,
    email                 NVARCHAR(255) NOT NULL UNIQUE,
    username              NVARCHAR(100) NOT NULL UNIQUE,
    hashed_password       NVARCHAR(500),
    full_name             NVARCHAR(255),
    phone                 NVARCHAR(50),
    department_id         BIGINT,
    position_title        NVARCHAR(100),
    employee_number       NVARCHAR(50) UNIQUE,
    is_active             BIT DEFAULT 1,
    is_admin              BIT DEFAULT 0,
    mfa_enabled           BIT DEFAULT 0,
    azure_oid             NVARCHAR(255),
    sso_provider          NVARCHAR(50),
    failed_login_attempts INT DEFAULT 0,
    last_failed_login     DATETIME2,
    locked_until          DATETIME2,
    password_changed_at   DATETIME2,
    must_change_password  BIT DEFAULT 0,
    last_login_at         DATETIME2,
    created_at            DATETIME2 DEFAULT GETDATE(),
    updated_at            DATETIME2 DEFAULT GETDATE(),
    created_by            BIGINT,
    updated_by            BIGINT
);

CREATE TABLE wbx_roles (
    id          BIGINT IDENTITY(1,1) PRIMARY KEY,
    code        NVARCHAR(50) NOT NULL UNIQUE,
    name        NVARCHAR(100) NOT NULL,
    description NVARCHAR(500),
    is_system   BIT DEFAULT 0,
    created_at  DATETIME2 DEFAULT GETDATE(),
    updated_at  DATETIME2 DEFAULT GETDATE()
);

CREATE TABLE wbx_user_roles (
    id      BIGINT IDENTITY(1,1) PRIMARY KEY,
    user_id BIGINT NOT NULL FOREIGN KEY REFERENCES wbx_users(id),
    role_id BIGINT NOT NULL FOREIGN KEY REFERENCES wbx_roles(id),
    CONSTRAINT uk_wbx_user_role UNIQUE (user_id, role_id)
);

CREATE TABLE wbx_role_permissions (
    id         BIGINT IDENTITY(1,1) PRIMARY KEY,
    role_id    BIGINT NOT NULL FOREIGN KEY REFERENCES wbx_roles(id),
    module     NVARCHAR(100) NOT NULL,
    action     NVARCHAR(50) NOT NULL,
    dept_scope NVARCHAR(20) DEFAULT 'OWN',
    CONSTRAINT uk_wbx_role_perm UNIQUE (role_id, module, action)
);

CREATE TABLE wbx_refresh_tokens (
    id          BIGINT IDENTITY(1,1) PRIMARY KEY,
    user_id     BIGINT NOT NULL FOREIGN KEY REFERENCES wbx_users(id),
    token_hash  NVARCHAR(500) NOT NULL,
    device_info NVARCHAR(500),
    ip_address  NVARCHAR(50),
    expires_at  DATETIME2 NOT NULL,
    created_at  DATETIME2 DEFAULT GETDATE()
);

CREATE TABLE wbx_totp_secrets (
    id               BIGINT IDENTITY(1,1) PRIMARY KEY,
    user_id          BIGINT NOT NULL UNIQUE FOREIGN KEY REFERENCES wbx_users(id),
    encrypted_secret NVARCHAR(500) NOT NULL,
    verified         BIT DEFAULT 0,
    backup_codes     NVARCHAR(2000),
    created_at       DATETIME2 DEFAULT GETDATE(),
    updated_at       DATETIME2 DEFAULT GETDATE(),
    created_by       BIGINT,
    updated_by       BIGINT
);

CREATE TABLE wbx_login_history (
    id             BIGINT IDENTITY(1,1) PRIMARY KEY,
    user_id        BIGINT,
    email          NVARCHAR(255),
    action         NVARCHAR(50),
    auth_method    NVARCHAR(50),
    ip_address     NVARCHAR(50),
    user_agent     NVARCHAR(500),
    failure_reason NVARCHAR(500),
    created_at     DATETIME2 DEFAULT GETDATE()
);

CREATE TABLE wbx_notifications (
    id         BIGINT IDENTITY(1,1) PRIMARY KEY,
    user_id    BIGINT NOT NULL FOREIGN KEY REFERENCES wbx_users(id),
    title      NVARCHAR(500),
    message    NVARCHAR(MAX),
    type       NVARCHAR(50),
    is_read    BIT DEFAULT 0,
    link       NVARCHAR(1000),
    created_at DATETIME2 DEFAULT GETDATE()
);

CREATE TABLE wbx_audit_logs (
    id          BIGINT IDENTITY(1,1) PRIMARY KEY,
    user_id     BIGINT,
    username    NVARCHAR(255),
    action      NVARCHAR(50) NOT NULL,
    resource    NVARCHAR(100) NOT NULL,
    resource_id BIGINT,
    detail      NVARCHAR(4000),
    ip_address  NVARCHAR(50),
    created_at  DATETIME2 DEFAULT GETDATE()
);

CREATE TABLE wbx_file_uploads (
    id            BIGINT IDENTITY(1,1) PRIMARY KEY,
    user_id       BIGINT,
    original_name NVARCHAR(500),
    stored_name   NVARCHAR(500),
    file_key      NVARCHAR(500),
    content_type  NVARCHAR(200),
    file_size     BIGINT,
    category      NVARCHAR(100),
    created_at    DATETIME2 DEFAULT GETDATE()
);

CREATE TABLE wbx_system_config (
    id           BIGINT IDENTITY(1,1) PRIMARY KEY,
    config_key   NVARCHAR(100) NOT NULL UNIQUE,
    config_value NVARCHAR(4000),
    value_type   NVARCHAR(20) DEFAULT 'STRING',
    description  NVARCHAR(500),
    is_editable  BIT DEFAULT 1,
    updated_at   DATETIME2 DEFAULT GETDATE(),
    updated_by   BIGINT
);
