-- owrawww 데이터베이스 생성
CREATE DATABASE IF NOT EXISTS owrawww
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

CREATE USER IF NOT EXISTS 'owrawww'@'localhost' IDENTIFIED BY 'owrawww';
GRANT ALL PRIVILEGES ON owrawww.* TO 'owrawww'@'localhost';
FLUSH PRIVILEGES;

USE owrawww;

-- users 테이블
CREATE TABLE IF NOT EXISTS users (
    id       BIGINT       NOT NULL AUTO_INCREMENT,
    username VARCHAR(50)  NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role     VARCHAR(20)  NOT NULL DEFAULT 'ROLE_USER',
    email    VARCHAR(100),
    enabled  TINYINT(1)   NOT NULL DEFAULT 1,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- inquiry 테이블 (제휴문의)
CREATE TABLE IF NOT EXISTS inquiry (
    id         BIGINT        NOT NULL AUTO_INCREMENT,
    name       VARCHAR(100)  NOT NULL,
    email      VARCHAR(100)  NOT NULL,
    tel        VARCHAR(30)   NOT NULL,
    dept       VARCHAR(50)   NOT NULL,
    title      VARCHAR(200)  NOT NULL,
    content    TEXT          NOT NULL,
    created_at DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 테스트 계정 (비밀번호: Test1234!)
-- BCrypt 해시값 사전 생성 필요 시 아래 INSERT 사용
-- INSERT INTO users (username, password, role, email, enabled)
-- VALUES ('admin', '$2a$10$...', 'ROLE_ADMIN', 'admin@owrawww.com', 1);

-- =====================================================================
-- [마이그레이션] tel_hash / email_hash 컬럼 추가 (솔트 해시 검색용)
-- 운영 DB에 아래 ALTER TABLE 를 직접 실행해야 합니다.
-- =====================================================================
-- ALTER TABLE application_table ADD COLUMN tel_hash   VARCHAR(64) AFTER phone;
-- ALTER TABLE application_table ADD COLUMN email_hash VARCHAR(64) AFTER email;
-- CREATE INDEX idx_app_tel_hash   ON application_table(tel_hash);
-- CREATE INDEX idx_app_email_hash ON application_table(email_hash);
