import { defineStore } from 'pinia'; import axios from 'axios'; interface PlayerInfo { nickname: string; avatar_image: string; stove_lv: number; } interface RedeemResult { name: string; code: string; status: 'success' | 'error' | 'pending'; message: string; } interface HistoryItem { id: number; fid: string; coupon_code: string; result_msg: string; executed_at: string; } export interface Coupon { id: number; name: string; code: string; is_active: boolean; is_expired: boolean; created_at: string; } export interface SavedUser { fid: string; nickname: string; avatar_url: string; created_at: string; } export const useWosStore = defineStore('wos', { state: () => ({ fid: '', player: null as PlayerInfo | null, results: [] as RedeemResult[], history: [] as HistoryItem[], coupons: [] as Coupon[], savedUsers: [] as SavedUser[], 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; await Promise.all([this.loadHistory(), this.redeemAll()]); this.status = 'idle'; } catch (err: any) { this.status = 'error'; this.errorMsg = err.response?.data?.message || '유저를 찾을 수 없습니다.'; } }, async redeemAll() { if (!this.fid) return; try { const { data } = await axios.post('/api/redeem-all', { fid: this.fid }); this.results = data; await this.loadHistory(); } catch (err: any) { 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 loadSavedUsers() { try { const { data } = await axios.get('/api/users'); this.savedUsers = data; } catch {} }, async loadCoupons() { try { const { data } = await axios.get('/api/coupons'); this.coupons = data; } catch {} }, async addCoupon(name: string, code: string) { await axios.post('/api/coupons', { name, code }); await this.loadCoupons(); }, async toggleCoupon(id: number, is_active: boolean) { await axios.patch(`/api/coupons/${id}`, { is_active }); await this.loadCoupons(); }, async deleteCoupon(id: number) { await axios.delete(`/api/coupons/${id}`); await this.loadCoupons(); }, }, });