import { defineStore } from 'pinia';
import axios from 'axios';

export const useAdminStore = defineStore('admin', {
  state: () => ({
    users: [],
    clubs: [],
    subscriptions: [],
    loading: false,
    error: null
  }),

  actions: {
    async fetchUsers() {
      try {
        this.loading = true;
        const response = await axios.get('/api/admin/users', {
          headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
          }
        });
        this.users = response.data;
      } catch (error) {
        this.error = error.response?.data?.message || '사용자 목록을 불러오는데 실패했습니다.';
        throw error;
      } finally {
        this.loading = false;
      }
    },

    async fetchClubs() {
      try {
        this.loading = true;
        const response = await axios.get('/api/admin/clubs', {
          headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
          }
        });
        this.clubs = response.data;
      } catch (error) {
        this.error = error.response?.data?.message || '클럽 목록을 불러오는데 실패했습니다.';
        throw error;
      } finally {
        this.loading = false;
      }
    },

    async fetchSubscriptions() {
      try {
        this.loading = true;
        const response = await axios.get('/api/admin/subscriptions', {
          headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
          }
        });
        this.subscriptions = response.data;
      } catch (error) {
        this.error = error.response?.data?.message || '구독 정보를 불러오는데 실패했습니다.';
        throw error;
      } finally {
        this.loading = false;
      }
    }
  }
});
