{
  "rules": {
    ".read": false,
    ".write": false,
    "profiles": {
      ".read": "auth != null && root.child('profiles').child(auth.uid).child('role').val() === 'admin'",
      "$uid": {
        ".read": "auth != null && (auth.uid === $uid || root.child('profiles').child(auth.uid).child('role').val() === 'admin')",
        ".write": "auth != null && auth.uid === $uid && (!data.exists() || newData.child('role').val() === data.child('role').val())",
        ".validate": "newData.hasChildren(['name','email','role','createdAt']) && newData.child('name').isString() && newData.child('name').val().length <= 100 && newData.child('email').isString() && newData.child('role').val() === 'client'"
      }
    },
    "requests": {
      ".indexOn": ["clientId", "createdAt"],
      ".read": "auth != null && (root.child('profiles').child(auth.uid).child('role').val() === 'admin' || (query.orderByChild === 'clientId' && query.equalTo === auth.uid))",
      "$requestId": {
        ".read": "auth != null && (data.child('clientId').val() === auth.uid || root.child('profiles').child(auth.uid).child('role').val() === 'admin')",
        ".write": "auth != null && (( !data.exists() && newData.child('clientId').val() === auth.uid && newData.child('status').val() === 'recibida') || root.child('profiles').child(auth.uid).child('role').val() === 'admin')",
        ".validate": "newData.hasChildren(['clientId','service','status','createdAt']) && newData.child('clientId').isString() && newData.child('service').isString() && newData.child('service').val().length <= 80 && newData.child('notes').val().length <= 500 && newData.child('status').isString()"
      }
    },
    "leads": {
      ".read": "auth != null && root.child('profiles').child(auth.uid).child('role').val() === 'admin'",
      "$leadId": {
        ".read": "auth != null && root.child('profiles').child(auth.uid).child('role').val() === 'admin'",
        ".write": "(!data.exists() && newData.hasChildren(['name','phone','service','createdAt','status']) && newData.child('name').isString() && newData.child('name').val().length <= 100 && newData.child('phone').isString() && newData.child('phone').val().length <= 30 && newData.child('notes').val().length <= 500) || (auth != null && root.child('profiles').child(auth.uid).child('role').val() === 'admin')"
      }
    }
  }
}
