import { create } from 'zustand';
import { ImagePickerAsset } from 'expo-image-picker';

interface User {
  user_id: string;
  first_name: string;
  last_name: string;
  avatar?: string;
  flag1?: string;
}

interface GroupChatStore {
  selectedUsers: User[] | [];
  image: ImagePickerAsset | null;
  groupName: string;
  description: string;
  canEdit: boolean;
  canSend: boolean;
  canAdd: boolean;
  addUser: (user: User) => void;
  removeUser: (userId: string) => void;
  setImage: (image: ImagePickerAsset | null) => void;
  setGroupName: (name: string) => void;
  setDescription: (desc: string) => void;
  setCanEdit: (canEdit: boolean) => void;
  setCanSend: (canSend: boolean) => void;
  setCanAdd: (canAdd: boolean) => void;
  clearStore: () => void;
}

export const useGroupChatStore = create<GroupChatStore>((set) => ({
  selectedUsers: [],
  image: null,
  groupName: '',
  description: '',
  canEdit: true,
  canSend: true,
  canAdd: true,
  addUser: (user) => set((state) => ({ selectedUsers: [user, ...state.selectedUsers] })),
  removeUser: (userId) =>
    set((state) => ({
      selectedUsers: state.selectedUsers.filter((user) => user.user_id !== userId)
    })),
  setImage: (image) => set({ image }),
  setGroupName: (name) => set({ groupName: name }),
  setDescription: (desc) => set({ description: desc }),
  setCanEdit: (canEdit) => set({ canEdit }),
  setCanSend: (canSend) => set({ canSend }),
  setCanAdd: (canAdd) => set({ canAdd }),
  clearStore: () =>
    set({
      selectedUsers: [],
      image: null,
      groupName: '',
      description: '',
      canEdit: true,
      canSend: true,
      canAdd: true
    })
}));
