-- WBX Spring Framework — MySQL 스키마 (utf8mb4)
-- Hibernate ddl-auto와 병행 사용 가능 (Flyway 활성화 시 이 파일 사용)

CREATE TABLE IF NOT EXISTS wbx_users (
    id                    BIGINT AUTO_INCREMENT PRIMARY KEY,
    email                 VARCHAR(255) NOT NULL UNIQUE,
    username              VARCHAR(100) NOT NULL UNIQUE,
    hashed_password       VARCHAR(500),
    full_name             VARCHAR(255),
    phone                 VARCHAR(50),
    department_id         BIGINT,
    position_title        VARCHAR(100),
    employee_number       VARCHAR(50) UNIQUE,
    is_active             TINYINT(1) DEFAULT 1,
    is_admin              TINYINT(1) DEFAULT 0,
    mfa_enabled           TINYINT(1) DEFAULT 0,
    azure_oid             VARCHAR(255),
    sso_provider          VARCHAR(50),
    failed_login_attempts INT DEFAULT 0,
    last_failed_login     DATETIME,
    locked_until          DATETIME,
    password_changed_at   DATETIME,
    must_change_password  TINYINT(1) DEFAULT 0,
    last_login_at         DATETIME,
    created_at            DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at            DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    created_by            BIGINT,
    updated_by            BIGINT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wbx_roles (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    code        VARCHAR(50) NOT NULL UNIQUE,
    name        VARCHAR(100) NOT NULL,
    description VARCHAR(500),
    is_system   TINYINT(1) DEFAULT 0,
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at  DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS wbx_user_roles (
    id      BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT NOT NULL,
    role_id BIGINT NOT NULL,
    UNIQUE KEY uk_user_role (user_id, role_id),
    FOREIGN KEY (user_id) REFERENCES wbx_users(id),
    FOREIGN KEY (role_id) REFERENCES wbx_roles(id)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS wbx_role_permissions (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    role_id     BIGINT NOT NULL,
    module      VARCHAR(100) NOT NULL,
    action      VARCHAR(50) NOT NULL,
    dept_scope  VARCHAR(20) DEFAULT 'OWN',
    UNIQUE KEY uk_role_perm (role_id, module, action),
    FOREIGN KEY (role_id) REFERENCES wbx_roles(id)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS wbx_refresh_tokens (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id      BIGINT NOT NULL,
    token_hash   VARCHAR(500) NOT NULL,
    device_info  VARCHAR(500),
    ip_address   VARCHAR(50),
    expires_at   DATETIME NOT NULL,
    created_at   DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES wbx_users(id)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS wbx_totp_secrets (
    id               BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id          BIGINT NOT NULL UNIQUE,
    encrypted_secret VARCHAR(500) NOT NULL,
    verified         TINYINT(1) DEFAULT 0,
    backup_codes     VARCHAR(2000),
    created_at       DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at       DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    created_by       BIGINT,
    updated_by       BIGINT,
    FOREIGN KEY (user_id) REFERENCES wbx_users(id)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS wbx_login_history (
    id             BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id        BIGINT,
    email          VARCHAR(255),
    action         VARCHAR(50),
    auth_method    VARCHAR(50),
    ip_address     VARCHAR(50),
    user_agent     VARCHAR(500),
    failure_reason VARCHAR(500),
    created_at     DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS wbx_notifications (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id     BIGINT NOT NULL,
    title       VARCHAR(500),
    message     TEXT,
    type        VARCHAR(50),
    is_read     TINYINT(1) DEFAULT 0,
    link        VARCHAR(1000),
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES wbx_users(id)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS wbx_audit_logs (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id     BIGINT,
    username    VARCHAR(255),
    action      VARCHAR(50) NOT NULL,
    resource    VARCHAR(100) NOT NULL,
    resource_id BIGINT,
    detail      VARCHAR(4000),
    ip_address  VARCHAR(50),
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS wbx_file_uploads (
    id            BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id       BIGINT,
    original_name VARCHAR(500),
    stored_name   VARCHAR(500),
    file_key      VARCHAR(500),
    content_type  VARCHAR(200),
    file_size     BIGINT,
    category      VARCHAR(100),
    created_at    DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS wbx_system_config (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    config_key   VARCHAR(100) NOT NULL UNIQUE,
    config_value VARCHAR(4000),
    value_type   VARCHAR(20) DEFAULT 'STRING',
    description  VARCHAR(500),
    is_editable  TINYINT(1) DEFAULT 1,
    updated_at   DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    updated_by   BIGINT
) ENGINE=InnoDB;
