rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write, create, update, delete: if request.auth != null && request.auth.token.admin == true;
    }
    
    match /address_data/address_uid_mapping/{address}/firebase {
      allow write: if request.auth != null && request.resource.data.keys().hasOnly([request.auth.uid]) && request.resource.data[request.auth.uid] == true;
      allow update, delete: if request.auth != null && request.resource.data.diff(resource.data).affectedKeys().hasOnly([request.auth.uid]);
    }
    match /address_data/address_push_token_mapping/{address}/{document=**} {
      allow write, update, delete: if request.auth != null && get(/databases/$(database)/documents/address_data/address_uid_mapping/$(address)/firebase).data[request.auth.uid] == true;
    }
    match /address_data/metadata/{address}/onesignal_uids/{uid}/{document=**} {
      allow write, update, delete: if request.auth != null && get(/databases/$(database)/documents/address_data/address_uid_mapping/$(address)/firebase).data[request.auth.uid] == true;
    }

    match /dev_address_data/address_uid_mapping/{address}/firebase {
      allow write: if request.auth != null && request.resource.data.keys().hasOnly([request.auth.uid]) && request.resource.data[request.auth.uid] == true;
      allow update, delete: if request.auth != null && request.resource.data.diff(resource.data).affectedKeys().hasOnly([request.auth.uid]);
    }
    match /dev_address_data/address_push_token_mapping/{address}/{document=**} {
      allow write, update, delete: if request.auth != null && get(/databases/$(database)/documents/dev_address_data/address_uid_mapping/$(address)/firebase).data[request.auth.uid] == true;
    }
    match /dev_address_data/metadata/{address}/onesignal_uids/{uid}/{document=**} {
      allow write, update, delete: if request.auth != null && get(/databases/$(database)/documents/dev_address_data/address_uid_mapping/$(address)/firebase).data[request.auth.uid] == true;
    }
    
    match /beta_address_data/address_uid_mapping/{address}/firebase {
      allow write: if request.auth != null && request.resource.data.keys().hasOnly([request.auth.uid]) && request.resource.data[request.auth.uid] == true;
      allow update, delete: if request.auth != null && request.resource.data.diff(resource.data).affectedKeys().hasOnly([request.auth.uid]);
    }
    match /beta_address_data/address_push_token_mapping/{address}/{document=**} {
      allow write, update, delete: if request.auth != null && get(/databases/$(database)/documents/beta_address_data/address_uid_mapping/$(address)/firebase).data[request.auth.uid] == true;
    }
    match /beta_address_data/metadata/{address}/onesignal_uids/{uid}/{document=**} {
      allow write, update, delete: if request.auth != null && get(/databases/$(database)/documents/beta_address_data/address_uid_mapping/$(address)/firebase).data[request.auth.uid] == true;
    }
    
  }
}
