rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
  
  match /Users/{userId}{
    allow create: if true;
    allow read, update:if isAuthenticated();
      match /doctorId 
      {
        allow read;
        allow write: if false;
      }
    }
    
    match /Doctors/{doctorId}{
    allow read:if true;
    //alllow create doctor if user is doctor and doctor id not set
    allow create: if isAuthenticated() && get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.role == 'doctor';
    	match /balance {
      allow read:if isAuthenticated() && get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.role == 'doctor';
      }
      match /accountStatus {
        allow update: if false;
      }

    allow update:if get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.doctorId == resource.id || isAdmin();
    }
  
  	match /DoctorCategory/{categoryId}{
    allow read: if isAuthenticated();
    allow write: if isAdmin();
    }
    
    match /DoctorTimeslot/{timeslotId}{
    // allow create timeslot only role doctor
    allow create: if isAuthenticated() && get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.role == 'doctor';
    allow read: if isAuthenticated() || isAdmin();
    allow update,delete: if get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.doctorId == resource.data.doctorId;
    }
  
    match /Order/{orderId}{
      allow read: if isAuthenticated() && (request.auth.uid == resource.data.userId || isAdmin());
      allow update: if isAdmin();
    }
      match /WithdrawMethod/{withdrawId}{
      allow read,create: if isAuthenticated() && get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.role == 'doctor';
      allow delete: if resource.data.userId == request.auth.uid;
    }
    match /WithdrawRequest/{withdrawRequestId}{
      allow read,create: if isAuthenticated() && get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.role == 'doctor' || isAdmin();
      allow update: if isAdmin();
    }
    match /Transaction/{transactionId}{
      allow read,create: if isAuthenticated();
      allow update: if isAdmin();
    }
    match /Review/{reviewId}{
    allow read,create,update: if isAuthenticated();
    }
    match /Problem/{problemId}{
    allow read,create,update: if isAuthenticated();
    }
    match /TopRatedDoctor/{topRatedDoctorsId}{
    allow read : if isAuthenticated();
    allow create, delete : if isAdmin();
    }
    match /Prescription/{prescriptionId}{
      allow read: if isAuthenticated();
      allow read, write: if isAuthenticated() && isDoctor();
    }

    function isDoctor() {
      return get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.role == 'doctor';
    }

    function isAuthenticated() {
    	return request.auth != null;
    }

    function isAdmin() {
      return request.auth.token.isAdmin == true;
    }
    match /Rooms/{roomId} {
     allow read,write: if isAuthenticated();
    }
    match /RoomVideoCall/{roomIdVideoCall} {
     allow read,write: if isAuthenticated();
    }
    
    match /Rooms/{roomid}/messages/{messageId} {
    allow read,write: if isAuthenticated();
    }
    
    match /users/{userId} {
      allow read,write : if true;
  	}
    match /Settings/{settingsId} {
      allow read: if isAuthenticated();
      allow write: if isAdmin();
  	}
    match /Settings/{name=**} {
      allow read: if isAuthenticated();
      allow write: if isAdmin();
  	}
    match /OnlineDoctor/{doctorId} {
      allow read, write: if isAuthenticated();
  	}
    match /UserWallet/{userId} {
      allow read: if userId == request.auth.uid;
  	}
    match /UserWallet/{userId}/History/{historyId} {
      allow read: if userId == request.auth.uid;
  	}
    match /help_center/{userId} {
      allow write: if isAuthenticated();
      allow read: if isAdmin();
  	}
  }
}