import React from 'react';
import {
  normalizePermissionState,
  resolveStorageStatus,
  type PermissionStatus,
  type StorageStatus,
} from '../lib/devicePermissions';

type DevicePermissionsState = {
  notifications: PermissionStatus;
  camera: PermissionStatus;
  storage: StorageStatus;
};

type DevicePermissionsHook = DevicePermissionsState & {
  loading: boolean;
  refresh: () => Promise<void>;
  requestPersistentStorage: () => Promise<boolean>;
};

export function useDevicePermissions(): DevicePermissionsHook {
  const [permissions, setPermissions] = React.useState<DevicePermissionsState>({
    notifications: 'unsupported',
    camera: 'unsupported',
    storage: 'unsupported',
  });
  const [loading, setLoading] = React.useState(true);

  const refresh = React.useCallback(async () => {
    setLoading(true);

    try {
      if (typeof window === 'undefined' || typeof navigator === 'undefined') {
        return;
      }

      let notificationState: PermissionStatus = 'unsupported';
      if ('Notification' in window) {
        notificationState = normalizePermissionState(Notification.permission);
      }

      let cameraState: PermissionStatus = 'unsupported';
      if (navigator.permissions?.query) {
        try {
          const cameraPermission = await navigator.permissions.query({
            name: 'camera' as PermissionName,
          });
          cameraState = normalizePermissionState(cameraPermission.state);
        } catch {
          cameraState = 'unsupported';
        }
      }

      const storageSupported = Boolean(navigator.storage?.persisted);
      let persisted: boolean | null = null;
      if (storageSupported) {
        persisted = await navigator.storage.persisted();
      }

      setPermissions({
        notifications: notificationState,
        camera: cameraState,
        storage: resolveStorageStatus(persisted, storageSupported),
      });
    } finally {
      setLoading(false);
    }
  }, []);

  const requestPersistentStorage = React.useCallback(async () => {
    if (typeof navigator === 'undefined' || !navigator.storage?.persist) {
      return false;
    }

    try {
      const granted = await navigator.storage.persist();
      setPermissions((prev) => ({
        ...prev,
        storage: granted ? 'persisted' : 'available',
      }));
      return granted;
    } catch {
      return false;
    }
  }, []);

  React.useEffect(() => {
    void refresh();
  }, [refresh]);

  return {
    ...permissions,
    loading,
    refresh,
    requestPersistentStorage,
  };
}
