import { defineStore } from 'pinia'; import axios from 'axios'; interface PlayerInfo { nickname: string; avatar_image: string; stove_lv: number; } interface RedeemResult { code: string; status: 'success' | 'error' | 'pending'; message: string; } interface HistoryItem { id: number; fid: string; coupon_code: string; result_msg: string; executed_at: string; } export const useWosStore = defineStore('wos', { state: () => ({ fid: '', player: null as PlayerInfo | null, couponCodes: '', results: [] as RedeemResult[], history: [] as HistoryItem[], status: 'idle' as 'idle' | 'loading' | 'success' | 'error', errorMsg: '', }), actions: { async searchPlayer(fid: string) { this.status = 'loading'; this.errorMsg = ''; this.player = null; this.results = []; try { const { data } = await axios.post('/api/player', { fid }); this.player = data; this.fid = fid; this.status = 'idle'; await this.loadHistory(); } catch (err: any) { this.status = 'error'; this.errorMsg = err.response?.data?.message || '유저를 찾을 수 없습니다.'; } }, async loadHistory() { if (!this.fid) return; try { const { data } = await axios.get(`/api/history/${this.fid}`); this.history = data; } catch {} }, async submitCoupons() { if (!this.fid) return; const codes = this.couponCodes .split('\n') .map((c) => c.trim()) .filter((c) => c.length > 0); if (codes.length === 0) { this.errorMsg = '쿠폰 코드를 입력해주세요.'; return; } this.results = codes.map((code) => ({ code, status: 'pending', message: '처리 중...' })); this.status = 'loading'; this.errorMsg = ''; try { const { data } = await axios.post('/api/redeem', { fid: this.fid, codes }); this.results = data; this.status = 'idle'; this.couponCodes = ''; await this.loadHistory(); } catch (err: any) { this.status = 'error'; this.errorMsg = err.response?.data?.message || '쿠폰 입력 중 오류가 발생했습니다.'; } }, }, });