import * as FileSystem from 'expo-file-system';
import type { User, RelationshipData } from '@/constants/Types';

export const getInitialDataByAppleId = async (appleId: string) => {
  try {
    const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/getInitialDataByAppleId`;
    const response = await fetch((apiUrl + `?appleId=${appleId}`), {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
      },
    });
    return response;
  } catch (error: unknown) {
    console.error('Error getting user by Apple ID:', error);
    throw error;
  }
};

export const createUser =
  async (appleId: string, email: string, fullName: string, pushToken: string) => {
  try {
    const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/createUser`;
    const response = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
      },
      body: JSON.stringify({
        appleId: appleId,
        email: email,
        fullName: fullName,
        pushToken: pushToken,
      }),
    });
    if (!response.ok) {
      throw new Error(
        `Error creating user: ${response.status} ${response.statusText}`
      );
    }
    const user: User = await response.json() as User;
    return user;
  } catch (error: unknown) {
    console.error('Error creating user:', error);
    throw error;
  }
};

export const updatePushToken = async (userId: number, pushToken: string) => {
  try {
    const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/updatePushToken`;
    const response = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
      },
      body: JSON.stringify({
        userId: userId,
        pushToken: pushToken,
      }),
    });
    if (!response.ok) {
      throw new Error(
        `Error updating push token: ${response.status} ${response.statusText}`
      );
    }
    return response;
  } catch (error: unknown) {
    console.error('Error updating push token:', error);
    throw error;
  }
};

export const updateProfilePicture = async (userId: number, pfpUrl: string) => {
  try {
    const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/updatePfp`;
    const response = await FileSystem.uploadAsync(apiUrl, pfpUrl, {
      fieldName: 'file',
      httpMethod: 'POST',
      uploadType: FileSystem.FileSystemUploadType.MULTIPART,
      parameters: { userId: userId.toString() },
      headers: {
        'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
      },
    });
    if (response.status !== 200)
      throw new Error(
        `Error: Server responded with status ${response.status}: ${response.body}`
      );
    const updatedUser: User = JSON.parse(response.body) as User;
    return updatedUser;
  } catch (error: unknown) {
    console.error('Error updating profile picture:', error);
    throw error;
  }
};

export const checkRelationshipStatus = async (userId: number) => {
  try {
    const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/checkRelationship`;
    const response = await fetch((apiUrl + `?userId=${userId}`), {
      headers: {
        'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
      },
    });
    if (!response.ok) {
      throw new Error(
        `Error checking relationship status: ${response.status} ${response.statusText}`
      );
    }
    const relationshipData = await response.json() as RelationshipData;
    return relationshipData;
  } catch (error: unknown) {
    console.log('Error checking relationship status:', error);
    throw error;
  }
};

export const updateRelationshipStatus = async (userId: number, status: 'accepted' | 'rejected') => {
  try {
    const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/relationships/updateStatus`;
    const response = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
      },
      body: JSON.stringify({
        userId: userId,
        status: status,
      }),
    });
    if (!response.ok) {
      throw new Error(
        `Error updating relationship status: ${response.status} ${response.statusText}`
      );
    }
    const updatedRelationshipData = await response.json() as RelationshipData;
    return updatedRelationshipData;
  } catch (error: unknown) {
    console.error('Error updating relationship status:', error);
    throw error;
  }
};

export const searchUsers = async (searchTerm: string) => {
  try {
    const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/search`;
    const response = await fetch((apiUrl + `?searchTerm=${searchTerm}`), {
      headers: {
        'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
      },
    });
    if (!response.ok) {
      throw new Error(
        `Error searching users: ${response.status} ${response.statusText}`
      );
    }
    const users: User[] = await response.json() as User[];
    return users as User[];
  } catch (error: unknown) {
    console.error('Error searching users:', error);
    throw error;
  }
};

export const sendRelationshipRequest = async (userId: number, targetUserId: number) => {
  if (!userId || !targetUserId || isNaN(userId) || isNaN(targetUserId)) return;
  try {
    const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/relationships/createRequest`;
    const response = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
      },
      body: JSON.stringify({ userId: userId, targetUserId: targetUserId }),
    });
    if (!response.ok) {
      throw new Error(
        `Error sending Relationship request: ${response.status} ${response.statusText}`
      );
    }
    const relationshipData: RelationshipData = await response.json() as RelationshipData;
    return relationshipData;
  } catch (error: unknown) {
    console.error('Error sending Relationship request:', error);
    throw error;
  }

};
