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 orgs = useOrgsStore() const list = ref([]) const loading = ref(false) const error = ref(null) async function fetchAll() { loading.value = true error.value = null try { list.value = await $api('/groups/') } catch (e: any) { error.value = e?.message ?? 'Erreur chargement groupes' } finally { loading.value = false } } async function getGroup(id: string): Promise { try { return await $api(`/groups/${id}`) } catch { return null } } async function create(payload: GroupCreate): Promise { try { const group = await $api('/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 { 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 { try { const member = await $api(`/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 { 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 } })