-- SQLite 마일리지 데이터베이스 스키마
-- pharmacy-pos-qr-system/backend/db/mileage_schema.sql

-- 1. 사용자 테이블 (카카오 로그인 계정)
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    nickname VARCHAR(100),
    profile_image_url VARCHAR(500),
    email VARCHAR(200),
    is_email_verified BOOLEAN DEFAULT FALSE,
    phone VARCHAR(20),
    birthday VARCHAR(10),
    mileage_balance INTEGER DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- 2. 외부 로그인 매핑 테이블
CREATE TABLE IF NOT EXISTS customer_identities (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    provider VARCHAR(20) NOT NULL,
    provider_user_id VARCHAR(100) NOT NULL,
    provider_data TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id),
    UNIQUE(provider, provider_user_id)
);

CREATE INDEX IF NOT EXISTS idx_identities_user ON customer_identities(user_id);

-- 3. 영수증 QR 토큰 테이블
CREATE TABLE IF NOT EXISTS claim_tokens (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    transaction_id VARCHAR(20) NOT NULL,
    pharmacy_id VARCHAR(20),
    token_hash VARCHAR(64) NOT NULL,
    total_amount INTEGER NOT NULL,
    claimable_points INTEGER NOT NULL,
    expires_at DATETIME NOT NULL,
    claimed_at DATETIME,
    claimed_by_user_id INTEGER,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (claimed_by_user_id) REFERENCES users(id),
    UNIQUE(transaction_id),
    UNIQUE(token_hash)
);

CREATE INDEX IF NOT EXISTS idx_tokens_hash ON claim_tokens(token_hash);
CREATE INDEX IF NOT EXISTS idx_tokens_expires ON claim_tokens(expires_at);

-- 4. 마일리지 원장 테이블
CREATE TABLE IF NOT EXISTS mileage_ledger (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    transaction_id VARCHAR(20),
    points INTEGER NOT NULL,
    balance_after INTEGER NOT NULL,
    reason VARCHAR(50) NOT NULL,
    description TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id),
    UNIQUE(transaction_id)
);

CREATE INDEX IF NOT EXISTS idx_ledger_user ON mileage_ledger(user_id);
CREATE INDEX IF NOT EXISTS idx_ledger_transaction ON mileage_ledger(transaction_id);

-- 5. POS 고객 연결 테이블
CREATE TABLE IF NOT EXISTS pos_customer_links (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    pharmacy_id VARCHAR(20),
    cuscode VARCHAR(10),
    customer_name VARCHAR(50),
    linked_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN DEFAULT TRUE,
    FOREIGN KEY (user_id) REFERENCES users(id),
    UNIQUE(user_id, pharmacy_id)
);

CREATE INDEX IF NOT EXISTS idx_links_cuscode ON pos_customer_links(cuscode);

-- 6. 알림톡 발송 로그 테이블
CREATE TABLE IF NOT EXISTS alimtalk_logs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    template_code VARCHAR(50) NOT NULL,
    recipient_no VARCHAR(20) NOT NULL,
    user_id INTEGER,
    trigger_source VARCHAR(20) NOT NULL,       -- 'kiosk', 'admin', 'manual' 등
    template_params TEXT,                       -- JSON 문자열
    success BOOLEAN NOT NULL,
    result_message TEXT,
    transaction_id VARCHAR(20),
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE INDEX IF NOT EXISTS idx_alimtalk_created ON alimtalk_logs(created_at);
CREATE INDEX IF NOT EXISTS idx_alimtalk_recipient ON alimtalk_logs(recipient_no);
