import { defineStore } from 'pinia'
import axios from 'axios'

interface DashboardStats {
  volunteers: any
  events: any
  pqrs: any
  finance: any
  proposals: any
  territory: any
  activity: any
}

export const useDashboardStore = defineStore('dashboard', {
  state: () => ({
    stats: null as DashboardStats | null,
    trends: null as any,
    recentActivity: [] as any[],
    topVolunteers: [] as any[],
    geospatial: null as any,
    loading: false
  }),

  actions: {
    async fetchOverview(period: number = 30) {
      this.loading = true
      try {
        const response = await axios.get('/dashboard/overview', { params: { period } })
        this.stats = response.data
      } catch (error) {
        console.error('Error fetching overview:', error)
      } finally {
        this.loading = false
      }
    },

    async fetchTrends(days: number = 30) {
      try {
        const response = await axios.get('/dashboard/trends', { params: { days } })
        this.trends = response.data
      } catch (error) {
        console.error('Error fetching trends:', error)
      }
    },

    async fetchRecentActivity(limit: number = 20) {
      try {
        const response = await axios.get('/dashboard/recent-activity', { params: { limit } })
        this.recentActivity = response.data
      } catch (error) {
        console.error('Error fetching recent activity:', error)
      }
    },

    async fetchTopVolunteers(limit: number = 10, period: number = 30) {
      try {
        const response = await axios.get('/dashboard/top-volunteers', { params: { limit, period } })
        this.topVolunteers = response.data
      } catch (error) {
        console.error('Error fetching top volunteers:', error)
      }
    },

    async fetchGeospatial() {
      try {
        const response = await axios.get('/dashboard/geospatial')
        this.geospatial = response.data
      } catch (error) {
        console.error('Error fetching geospatial data:', error)
      }
    }
  }
})
