-- ============================================================
-- Migration: 크레딧 충전 요청 / 거래 이력 테이블 추가
-- Date: 2026-04-29
-- Description: 백오피스 크레딧 워크플로우 도입
-- 선행 조건: migration_2026_04_29_add_admin_table.sql 먼저 실행
-- ============================================================

CREATE TABLE IF NOT EXISTS credit_charge_request (
    id               BIGINT       NOT NULL AUTO_INCREMENT COMMENT '고유 식별자',
    user_uuid        VARCHAR(36)  NOT NULL               COMMENT '사용자 UUID (user.user_uuid 참조)',
    requested_amount INT          NOT NULL               COMMENT '요청 크레딧 수량 (양수)',
    message          VARCHAR(500)     NULL               COMMENT '사용자 요청 메시지',
    status           VARCHAR(20)  NOT NULL DEFAULT 'pending' COMMENT '처리 상태 (pending/approved/rejected/cancelled)',
    admin_id         BIGINT           NULL               COMMENT '처리한 백오피스 관리자 ID',
    admin_note       VARCHAR(1000)    NULL               COMMENT '관리자 메모',
    processed_at     DATETIME         NULL               COMMENT '처리 일시',
    created_at       DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '요청 일시',
    updated_at       DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '수정 일시',
    PRIMARY KEY (id),
    CONSTRAINT fk_charge_request_user     FOREIGN KEY (user_uuid) REFERENCES `user`(user_uuid) ON DELETE CASCADE,
    CONSTRAINT fk_charge_request_admin    FOREIGN KEY (admin_id)  REFERENCES `admin`(id)       ON DELETE SET NULL,
    INDEX idx_credit_request_user_uuid     (user_uuid),
    INDEX idx_credit_request_status        (status),
    INDEX idx_credit_request_created_at    (created_at),
    INDEX idx_credit_request_status_created (status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='사용자 크레딧 충전 요청';


CREATE TABLE IF NOT EXISTS credit_transaction (
    id                 BIGINT      NOT NULL AUTO_INCREMENT COMMENT '고유 식별자',
    user_uuid          VARCHAR(36) NOT NULL               COMMENT '사용자 UUID',
    amount             INT         NOT NULL               COMMENT '변경 크레딧 수량 (충전 양수, 차감 음수)',
    balance_after      INT         NOT NULL               COMMENT '변경 후 잔액',
    type               VARCHAR(20) NOT NULL               COMMENT '변경 유형 (charge/consume/refund/admin_adjust)',
    reason             VARCHAR(255)    NULL               COMMENT '변경 사유',
    admin_id           BIGINT          NULL               COMMENT '처리 관리자 ID',
    related_request_id BIGINT          NULL               COMMENT '연관 충전 요청 ID',
    created_at         DATETIME    NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '변경 일시',
    PRIMARY KEY (id),
    CONSTRAINT fk_credit_tx_user    FOREIGN KEY (user_uuid)          REFERENCES `user`(user_uuid)              ON DELETE CASCADE,
    CONSTRAINT fk_credit_tx_admin   FOREIGN KEY (admin_id)           REFERENCES `admin`(id)                   ON DELETE SET NULL,
    CONSTRAINT fk_credit_tx_request FOREIGN KEY (related_request_id) REFERENCES credit_charge_request(id)     ON DELETE SET NULL,
    INDEX idx_credit_tx_user_uuid         (user_uuid),
    INDEX idx_credit_tx_user_uuid_created (user_uuid, created_at),
    INDEX idx_credit_tx_type              (type),
    INDEX idx_credit_tx_related_request   (related_request_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='크레딧 변경 이력';
