import * as SecureStore from 'expo-secure-store';

type UserData = {
  id: number;
  appleId: string | null;
  appleEmail: string | null;
  fullName: string;
  pfpURL: string | null;
  pushToken: string;
  createdAt: Date;
};

type Relationship = {
  id: number;
  title: string;
  status: 'pending' | 'accepted' | 'rejected';
  relationshipStartDate: Date;
  createdAt: Date;
};

type RelationshipData = {
  relationship: Relationship;
  partner: UserData;
};

export const saveRelationshipData = async (RelationshipData: RelationshipData) => {
  try {
    await SecureStore.setItemAsync('RelationshipData', JSON.stringify(RelationshipData));
  } catch (error) {
    console.error('Error saving user data:', error);
  }
};

export const getRelationshipData = async () => {
  try { 
    const RelationshipData = await SecureStore.getItemAsync('RelationshipData');
    return RelationshipData ? JSON.parse(RelationshipData) : null;
  } catch (error) {
    console.error('Error getting user data:', error);
    return null;
  }
};

export const updateRelationshipData = async (updatedFields: Partial<any>) => {
  try {
    const currentUserData: RelationshipData = await getRelationshipData() as RelationshipData;
    if (currentUserData) {
      const updatedUserData: RelationshipData = { ...currentUserData, ...updatedFields };
      await saveRelationshipData(updatedUserData);
      return updatedUserData as RelationshipData;
    }
    return null;
  } catch (error) {
    console.error('Error updating user data:', error);
    return null;
  }
};
