export interface GroupMember {
  id: string
  display_name: string
  identity_id: string | null
  added_at: string
}

export interface Group {
  id: string
  name: string
  description: string | null
  organization_id: string | null
  created_at: string
  members: GroupMember[]
}

export interface GroupSummary {
  id: string
  name: string
  description: string | null
  organization_id: string | null
  member_count: number
}

export interface GroupCreate {
  name: string
  description?: string | null
}

export interface GroupMemberCreate {
  display_name: string
  identity_id?: string | null
}

export const useGroupsStore = defineStore('groups', () => {
  const { $api } = useApi()

  const list = ref<GroupSummary[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)

  async function fetchAll() {
    loading.value = true
    error.value = null
    try {
      list.value = await $api<GroupSummary[]>('/groups/')
    } catch (e: any) {
      error.value = e?.message ?? 'Erreur chargement groupes'
    } finally {
      loading.value = false
    }
  }

  async function getGroup(id: string): Promise<Group | null> {
    try {
      return await $api<Group>(`/groups/${id}`)
    } catch {
      return null
    }
  }

  async function create(payload: GroupCreate): Promise<Group | null> {
    try {
      const group = await $api<Group>('/groups/', { method: 'POST', body: payload })
      await fetchAll()
      return group
    } catch (e: any) {
      error.value = e?.message ?? 'Erreur création groupe'
      return null
    }
  }

  async function remove(id: string): Promise<boolean> {
    try {
      await $api(`/groups/${id}`, { method: 'DELETE' })
      list.value = list.value.filter(g => g.id !== id)
      return true
    } catch {
      return false
    }
  }

  async function addMember(groupId: string, payload: GroupMemberCreate): Promise<GroupMember | null> {
    try {
      const member = await $api<GroupMember>(`/groups/${groupId}/members`, {
        method: 'POST',
        body: payload,
      })
      const g = list.value.find(g => g.id === groupId)
      if (g) g.member_count++
      return member
    } catch {
      return null
    }
  }

  async function removeMember(groupId: string, memberId: string): Promise<boolean> {
    try {
      await $api(`/groups/${groupId}/members/${memberId}`, { method: 'DELETE' })
      const g = list.value.find(g => g.id === groupId)
      if (g) g.member_count--
      return true
    } catch {
      return false
    }
  }

  return { list, loading, error, fetchAll, getGroup, create, remove, addMember, removeMember }
})
