import {Platform} from 'react-native';
import {PERMISSIONS, request} from 'react-native-permissions';
import {getAPILevel, isIos} from '@shared/utils';

const sdkVersion = Platform.OS === 'android' ? getAPILevel() : 0;

const ATTACHMENT_PERMISSIONS = Platform.select({
  ios: {
    camera: PERMISSIONS.IOS.CAMERA,
    gallery: PERMISSIONS.IOS.PHOTO_LIBRARY,
    files: PERMISSIONS.IOS.PHOTO_LIBRARY,
  },
  android: {
    camera: PERMISSIONS.ANDROID.CAMERA,
    gallery:
      sdkVersion < 33
        ? PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE
        : PERMISSIONS.ANDROID.READ_MEDIA_IMAGES,
    files: PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE,
  },
  default: {
    camera: PERMISSIONS.IOS.CAMERA,
    gallery: PERMISSIONS.IOS.PHOTO_LIBRARY,
    files: PERMISSIONS.IOS.PHOTO_LIBRARY,
  },
});

export const useMediaPermissions = () => {
  const requestCameraPermission = async () => {
    try {
      const status = await request(ATTACHMENT_PERMISSIONS.camera);
      return {
        isAvailable: status === 'granted',
        shouldOpenSettings: status === 'blocked',
      };
    } catch (error) {
      return {
        isAvailable: false,
        shouldOpenSettings: false,
      };
    }
  };

  const requestGalleryPermission = async () => {
    try {
      // Android 13+ (API 33+): react-native-image-crop-picker uses the Android
      // Photo Picker which runs in a separate process and handles media access
      // internally — no runtime permission is required or grantable.
      // Requesting READ_MEDIA_IMAGES on these devices returns 'blocked' immediately
      // on OEM devices (Samsung etc.) that enforce Photo Picker usage.
      if (!isIos && sdkVersion >= 33) {
        return {isAvailable: true, shouldOpenSettings: false};
      }

      const status = await request(ATTACHMENT_PERMISSIONS.gallery);
      return {
        isAvailable: status === 'granted' || status === 'limited',
        shouldOpenSettings: status === 'blocked',
      };
    } catch (error) {
      return {
        isAvailable: false,
        shouldOpenSettings: false,
      };
    }
  };

  const requestFilesPermission = async () => {
    try {
      if (isIos) {
        // iOS DocumentPicker doesn't require specific permissions
        // The system handles permissions internally
        return {isAvailable: true, shouldOpenSettings: false};
      }

      // Get Android API level
      const apiLevel = await getAPILevel();

      // Old Android versions needs to request a permission
      if (apiLevel < 33) {
        const status = await request(ATTACHMENT_PERMISSIONS.files);
        return {
          isAvailable: status === 'granted',
          shouldOpenSettings: status === 'blocked',
        };
      }

      // New android versions not need permission, it should use the native picker
      return {isAvailable: true, shouldOpenSettings: false};
    } catch (error) {
      return {isAvailable: false, shouldOpenSettings: false};
    }
  };

  return {
    requestCameraPermission,
    requestGalleryPermission,
    requestFilesPermission,
  };
};
