graph TD
    %% START - User Access
    Start([User Access Portal]) --> Login[Login via RE Bridge Centralised SSO]
    Login --> AuthCheck{Authentication Success?}
    AuthCheck -->|No| LoginError[Authentication Failed]
    AuthCheck -->|Yes| Dashboard[User Dashboard]
    
    %% DASHBOARD ROUTING
    Dashboard --> UserType{User Role}
    UserType -->|Dealer| DealerPortal[Dealer Portal]
    UserType -->|Internal Staff| StaffPortal[Staff Portal]
    UserType -->|Admin| AdminAuthCheck{Admin Permission Level}
    
    %% ADMIN ROLE HIERARCHY
    AdminAuthCheck -->|Super Admin| AdminPortal[Full Admin Portal]
    AdminAuthCheck -->|System Admin| SystemAdminView[System Admin View]
    AdminAuthCheck -->|Workflow Admin| WorkflowAdminView[Workflow Admin View]
    AdminAuthCheck -->|Reports Admin| ReportsAdminView[Reports Admin View]
    AdminAuthCheck -->|Regional Admin| RegionalAdminView[Regional Admin View]
    
    %% ROUTE TO ADMIN ACTIONS
    SystemAdminView --> AdminActions{Admin Actions}
    WorkflowAdminView --> AdminActions
    ReportsAdminView --> AdminActions
    RegionalAdminView --> AdminActions
    
    %% DEALER PORTAL - BECOME A DEALER FLOW
    DealerPortal --> DealerAction{Dealer Action}
    DealerAction -->|New Application| NewApp[Submit 'Become a Dealer' Form]
    DealerAction -->|Existing Application| TrackApp[Track Application Status]
    DealerAction -->|Claims| ClaimProcess[Submit Claim Request]
    
    %% NEW DEALER APPLICATION FLOW
    NewApp --> CaptureForm[Frontend: Capture Form Data]
    CaptureForm --> ValidateForm[Frontend: Form Validation]
    ValidateForm --> SubmitForm[Frontend: Submit to Backend API]
    SubmitForm --> StoreData[Backend: Store in Database]
    StoreData --> CheckLocation{Backend: Location Has Vacancy?}
    
    CheckLocation -->|No| RejectEmail[Backend: Send Non-Opportunity Email]
    CheckLocation -->|Yes| AckEmail[Backend: Send Acknowledgement Email]
    
    AckEmail --> SendQuestionnaire[Backend: Send Questionnaire Link]
    SendQuestionnaire --> WaitResponse{Backend: Response Received?}
    
    WaitResponse -->|No - D+2| Reminder1[Backend: Send Reminder Email]
    Reminder1 --> WaitResponse2{Backend: Response Received?}
    WaitResponse2 -->|No - D+5| Reminder2[Backend: Send Final Reminder]
    Reminder2 --> WaitResponse3{Backend: Response Received?}
    WaitResponse3 -->|No - D+20| ExpireLink[Backend: Close Questionnaire]
    
    WaitResponse -->|Yes| ProcessResponse[Backend: Calculate Weighted Rank]
    WaitResponse2 -->|Yes| ProcessResponse
    WaitResponse3 -->|Yes| ProcessResponse
    
    ProcessResponse --> DDShortlist[Staff Portal: DD Team Reviews & Shortlists Top 10]
    DDShortlist --> AssignZM[Backend: Assign to Zonal Manager DD]
    
    AssignZM --> ZMEval{Staff Portal: ZM-DD KT Evaluation}
    ZMEval -->|Rejected| ZMReject[Backend: Store Rejection Reason]
    ZMEval -->|Shortlisted| AssignRBM[Backend: Auto-Assign to RBM]
    
    AssignRBM --> RBMEval{Staff Portal: RBM Evaluation}
    RBMEval -->|Rejected| RBMReject[Backend: Store Rejection Reason]
    RBMEval -->|Approved| AssignDDL[Backend: Auto-Assign to DDL Team]
    
    AssignDDL --> FDD[Backend: Send OTP-Protected Link for FDD]
    FDD --> UploadFDD[External Agency: Upload FDD Report L1/L2]
    UploadFDD --> SubmitNBH[Staff Portal: DD Team Submits to NBH]
    
    SubmitNBH --> NBHApproval{Staff Portal: NBH Approval}
    NBHApproval -->|Rejected| NBHReject[Backend: Store Rejection & Notify]
    NBHApproval -->|Approved| IssueLOI[Backend: Generate & Send LOI]
    
    IssueLOI --> UploadLOI[Staff Portal: Upload LOI to System]
    UploadLOI --> IssueLOA[Backend: Generate & Send LOA]
    IssueLOA --> UploadLOA[Staff Portal: Upload LOA to System]
    
    UploadLOA --> ScheduleEOR[Staff Portal: Regional DD Schedules EOR Audit]
    ScheduleEOR --> UploadEOR[Staff Portal: Upload EOR Audit Report]
    UploadEOR --> EORApproval{Staff Portal: NBH EOR Approval}
    
    EORApproval -->|Rejected| EORReject[Backend: Store Rejection & Notify]
    EORApproval -->|Approved| UpdateDealer[Backend: Update Dealer Info]
    
    UpdateDealer --> ActiveDealer([Active Dealer Created])
    
    %% DEALER CLAIM PROCESS
    ClaimProcess --> ClaimForm[Frontend: Claim Request Form]
    ClaimForm --> ValidateClaim[Frontend: Form Validation]
    ValidateClaim --> SubmitClaim[Frontend: Submit to Backend API]
    SubmitClaim --> RI[Backend: Request Initiation]
    
    RI --> RI1[Backend: Store Request Details]
    RI1 --> PS[Backend: Proposal Submission]
    PS --> PS1[Frontend: Dealer Submits Proposal]
    PS1 --> PS2[Frontend: Cost Breakup]
    PS1 --> PS3[Frontend: Timeline for Closure]
    PS1 --> PS4[Frontend: Supporting Documents]
    
    PS2 --> RE[Backend: Request Evaluation]
    PS3 --> RE
    PS4 --> RE
    
    RE --> RE1{Staff Portal: Requestor Reviews}
    RE1 -->|Needs Clarification| RE2[Backend: Request More Info]
    RE2 --> PS1
    RE1 -->|Approved| RE3[Backend: Confirm to Proceed]
    
    RE3 --> DLA[Staff Portal: Dept. Lead Approval]
    DLA --> DLA1{Staff Portal: Lead Reviews Request}
    DLA1 -->|Needs Clarification| DLA2[Backend: Request Clarification]
    DLA2 --> RE
    DLA1 -->|Approved| DLA3[Backend: Approve Request]
    
    DLA3 --> BUD[Backend: Budgeting]
    BUD --> BUD1[Backend: Block Budget]
    BUD1 --> BUD2[Backend: Assign to IO]
    
    BUD2 --> AC[Backend: Activity Creation]
    AC --> AC1[Backend: System Creates Activity]
    AC1 --> AC2[Backend: Auto-Email Confirmation]
    AC2 --> AC3[Backend: Notify Requestor]
    AC2 --> AC4[Backend: Notify Dealer]
    AC2 --> AC5[Backend: Notify Lead]
    
    AC3 --> AE[Frontend: Activity Execution Interface]
    AC4 --> AE
    AC5 --> AE
    
    AE --> AE1[Frontend: Dealer Executes Activity]
    AE1 --> AE2[Frontend: Submit Required Documents]
    
    AE2 --> CA[Backend: Claim Approval]
    CA --> CA1{Staff Portal: Requestor Reviews Documents}
    CA1 -->|More Info Needed| CA2[Backend: Request Additional Info]
    CA2 --> AE2
    CA1 -->|Approved| CA3{Staff Portal: Approval Type}
    
    CA3 -->|Full Approval| EI[Backend: E-Invoicing]
    CA3 -->|Partial Approval| EI
    EI --> EI1[Backend: Generate E-Invoice]
    EI1 --> EI2[Backend: Issue Credit Note]
    EI2 --> ClaimSettled([Claim Settled])
    
    %% DEALER OFFBOARDING - RESIGNATION
    ActiveDealer --> DealerOps{Staff Portal: Dealer Operations}
    DealerOps -->|Resignation Request| ResignStart[Frontend: Dealer Sends Email to ZBH]
    ResignStart --> ResignRecord[Staff Portal: ZBH Records in System]
    ResignRecord --> ResignZBH{Staff Portal: ZBH Approval}
    
    ResignZBH -->|Rejected| ResignRejectZBH[Backend: Return to Previous Level & Notify]
    ResignZBH -->|Approved| ResignDDL{Staff Portal: DD Lead Approval}
    
    ResignDDL -->|Rejected| ResignRejectDDL[Backend: Return to ZBH & Notify]
    ResignDDL -->|Approved| ResignNBH{Staff Portal: NBH Approval}
    
    ResignNBH -->|Rejected| ResignRejectNBH[Backend: Return to DD Lead & Notify]
    ResignNBH -->|Approved| GenResignLetter[Backend: Generate Resignation Acceptance Letter]
    GenResignLetter --> SendResignLetter[Staff Portal: NBH Approves & Shares Letter]
    SendResignLetter --> StartFF1[Backend: Start F&F Process]
    
    %% DEALER OFFBOARDING - TERMINATION
    DealerOps -->|Termination Initiated| TermStart[Staff Portal: Identify Termination Reason]
    TermStart --> TermType{Staff Portal: Termination Type}
    TermType -->|Immediate| TermDocs1[Staff Portal: ASM Collects Documentation]
    TermType -->|By Convenience| TermDocs2[Staff Portal: ASM Collects Documentation]
    
    TermDocs1 --> PrepNotes[Backend: Generate Termination Notes PDF]
    TermDocs2 --> PrepNotes
    
    PrepNotes --> TermZBH{Staff Portal: ZBH Approval}
    TermZBH -->|Rejected| TermRejectZBH[Backend: Return & Notify]
    TermZBH -->|Approved| TermDDL{Staff Portal: DD Lead Approval}
    
    TermDDL -->|Rejected| TermRejectDDL[Backend: Return to ZBH & Notify]
    TermDDL -->|Approved| TermNBH{Staff Portal: NBH Approval}
    
    TermNBH -->|Rejected| TermRejectNBH[Backend: Return to DD Lead & Notify]
    TermNBH -->|Approved| TermCCO{Staff Portal: CCO Approval}
    
    TermCCO -->|Rejected| TermRejectCCO[Backend: Return to NBH & Notify]
    TermCCO -->|Approved| TermCEO{Staff Portal: CEO Approval}
    
    TermCEO -->|Rejected| TermRejectCEO[Backend: Return to CCO & Notify]
    TermCEO -->|Approved| IssueNotice[Backend: Generate Show Cause Notice]
    
    IssueNotice --> Send15Day[Backend: Send Notice with 15-Day Deadline]
    Send15Day --> WaitDealer{Frontend: Dealer Response?}
    
    WaitDealer -->|Response Received| ReviewResponse[Staff Portal: Review Response]
    WaitDealer -->|No Response| UploadTermLetter[Staff Portal: DD Lead Uploads Signed Termination Letter]
    ReviewResponse --> FinalDecision{Staff Portal: Final Decision}
    
    FinalDecision -->|Proceed| UploadTermLetter
    FinalDecision -->|Withdraw| TermWithdraw[Backend: Termination Withdrawn]
    
    UploadTermLetter --> StartFF2[Backend: Start F&F Process]
    
    %% FULL & FINAL SETTLEMENT PROCESS
    StartFF1 --> FFNotify[Backend: Send F&F Notification to All Stakeholders]
    StartFF2 --> FFNotify
    
    FFNotify --> FFTrack[Frontend: F&F Tracking System Activated]
    FFTrack --> FFStakeholders[Backend: Each Stakeholder Receives Task]
    
    FFStakeholders --> FFFinance[Frontend: Finance - Settlement Forms]
    FFStakeholders --> FFLegal[Frontend: Legal - Clearance Forms]
    FFStakeholders --> FFOthers[Frontend: Other Stakeholders - Respective Forms]
    
    FFFinance --> FFUpdate1[Frontend: Update Status in System]
    FFLegal --> FFUpdate2[Frontend: Update Status in System]
    FFOthers --> FFUpdate3[Frontend: Update Status in System]
    
    FFUpdate1 --> FFCheck{Backend: All Tasks Complete?}
    FFUpdate2 --> FFCheck
    FFUpdate3 --> FFCheck
    
    FFCheck -->|No| FFReminder[Backend: Send Reminder if TAT Exceeded]
    FFReminder --> FFCheck
    
    FFCheck -->|Yes| BlockDealer[Backend: Block Dealer from System]
    BlockDealer --> FFComplete[Backend: Send Completion Email to All Stakeholders]
    
    FFComplete --> End([Process Complete])
    
    %% FIELD VISIT REPORT MODULE
    StaffPortal --> FieldVisit[Field Visit Report Module]
    FieldVisit --> FJC[Frontend: FJC Planning Interface]
    FJC --> FJC1[Frontend: Document Preparation]
    FJC --> FJC2[Frontend: Review & Approval Flow]
    FJC --> FJC3[Frontend: Dealer Coordination]
    
    FJC1 --> VE[Frontend: Visit Execution Module]
    FJC2 --> VE
    FJC3 --> VE
    
    VE --> VE1{Staff Portal: Field Visit As Per Plan?}
    VE1 -->|Yes| VE2[Frontend: Execute Visit]
    VE1 -->|No| VE3[Frontend: Re-plan Changes]
    VE3 --> VE2
    VE2 --> VE4[Frontend: Dealer Discussion]
    
    VE4 --> DM[Frontend: Discussion Management Module]
    DM --> DM1[Frontend: Capture Actionable Insights]
    DM --> DM2[Frontend: Record Feedback]
    DM --> DM3[Frontend: Performance Metrics]
    
    DM1 --> MOM[Frontend: MOM Creation Module]
    DM2 --> MOM
    DM3 --> MOM
    
    MOM --> MOM1[Frontend: Structured Documentation]
    MOM --> MOM2[Frontend: Action Item Tracking]
    MOM --> MOM3[Frontend: Task Delegation]
    
    MOM1 --> CS[Backend: Closure & Sharing Module]
    MOM2 --> CS
    MOM3 --> CS
    
    CS --> CS1[Backend: Document Submission]
    CS1 --> CS2[Backend: Archiving]
    CS2 --> CS3[Backend: Stakeholder Communication]
    
    CS3 --> AW[Backend: Approval Workflow]
    AW --> AW1{Staff Portal: Hierarchy-Based Approval}
    AW1 -->|Approved| FieldVisitComplete([Field Visit Completed & Archived])
    AW1 -->|Rejected| Feedback[Backend: Feedback to Team]
    Feedback --> FJC
    
    %% ADMIN PORTAL FEATURES WITH PERMISSION CHECKS
    AdminPortal --> AdminActions{Admin Actions}
    AdminActions -->|User Management| CheckUserMgmt{Backend: Has Permission?}
    AdminActions -->|Master Data| CheckMasterData{Backend: Has Permission?}
    AdminActions -->|Workflow Engine| CheckWorkflow{Backend: Has Permission?}
    AdminActions -->|Reports| CheckReports{Backend: Has Permission?}
    
    %% USER MANAGEMENT PERMISSIONS
    CheckUserMgmt -->|Yes - Super Admin/System Admin| UserMgmt[Frontend: User Management Interface]
    CheckUserMgmt -->|No| AccessDenied1[Frontend: Access Denied - Insufficient Permissions]
    
    UserMgmt --> RoleMgmt[Frontend: Role Management]
    UserMgmt --> PermissionMgmt[Frontend: Permission Matrix]
    UserMgmt --> RegionalScoping[Frontend: Regional Scoping]
    
    %% MASTER DATA PERMISSIONS
    CheckMasterData -->|Yes - Super Admin/System Admin/Regional Admin| MasterData[Frontend: Master Data Management]
    CheckMasterData -->|No| AccessDenied2[Frontend: Access Denied - Insufficient Permissions]
    
    MasterData --> CheckRegionalScope{Backend: Is Regional Admin?}
    CheckRegionalScope -->|Yes| RegionalMasterData[Frontend: Regional Scoped Data Only]
    CheckRegionalScope -->|No| FullMasterData[Frontend: Full Master Data Access]
    
    RegionalMasterData --> TownMgmtRegional[Frontend: Town Management - Regional]
    RegionalMasterData --> DealerCodeMgmtRegional[Frontend: Dealer Code Management - Regional]
    
    FullMasterData --> TownMgmt[Frontend: Town Management]
    FullMasterData --> DealerCodeMgmt[Frontend: Dealer Code Management]
    FullMasterData --> EvaluationCriteria[Frontend: Evaluation Criteria Management]
    
    %% WORKFLOW ENGINE PERMISSIONS
    CheckWorkflow -->|Yes - Super Admin/Workflow Admin| WorkflowEngine[Frontend: Custom Workflow Engine]
    CheckWorkflow -->|No| AccessDenied3[Frontend: Access Denied - Insufficient Permissions]
    
    WorkflowEngine --> TemplateBuilder[Frontend: Template Builder]
    WorkflowEngine --> DynamicWorkflow[Frontend: Dynamic Workflow Creation]
    WorkflowEngine --> WorkflowMonitoring[Frontend: Workflow Monitoring]
    
    %% REPORTS PERMISSIONS
    CheckReports -->|Yes - All Admin Roles| Reports[Frontend: Reports & Analytics]
    CheckReports -->|No| AccessDenied4[Frontend: Access Denied - Insufficient Permissions]
    
    Reports --> CheckReportScope{Backend: Determine Report Scope}
    CheckReportScope -->|Regional Admin| RegionalReports[Frontend: Regional Reports Only]
    CheckReportScope -->|Other Admins| AllReports[Frontend: All Reports]
    
    RegionalReports --> DealerWiseReportRegional[Frontend: Dealer-wise Report - Regional]
    RegionalReports --> PendingRequestReportRegional[Frontend: Pending Request Report - Regional]
    
    AllReports --> DealerWiseReport[Frontend: Dealer-wise Report]
    AllReports --> MISReport[Frontend: MIS Report]
    AllReports --> PendingRequestReport[Frontend: Pending Request Report]
    AllReports --> FFTrackingReport[Frontend: F&F Tracking Report]
    AllReports --> ApplicationSummaryReport[Frontend: Application Summary Report]
    
    %% ACCESS DENIED ENDPOINTS
    AccessDenied1 --> AdminActions
    AccessDenied2 --> AdminActions
    AccessDenied3 --> AdminActions
    AccessDenied4 --> AdminActions
    
    %% END POINTS
    RejectEmail --> End
    ExpireLink --> End
    ZMReject --> End
    RBMReject --> End
    NBHReject --> End
    EORReject --> End
    TermWithdraw --> End
    LoginError --> Login
    
    %% STYLING
    style Start fill:#90EE90
    style End fill:#FFB6C1
    style ActiveDealer fill:#87CEEB
    style ClaimSettled fill:#87CEEB
    style FieldVisitComplete fill:#87CEEB
    style NBHApproval fill:#FFD700
    style EORApproval fill:#FFD700
    style ResignNBH fill:#FFD700
    style TermCEO fill:#FFD700
    style BlockDealer fill:#FF6B6B
    style Dashboard fill:#E6E6FA
    style StaffPortal fill:#F0F8FF
    style DealerPortal fill:#F5F5DC
    style AdminPortal fill:#FFE4E1
    style AdminAuthCheck fill:#DDA0DD
    style SystemAdminView fill:#F0E68C
    style WorkflowAdminView fill:#F0E68C
    style ReportsAdminView fill:#F0E68C
    style RegionalAdminView fill:#F0E68C
    style AccessDenied1 fill:#FFA07A
    style AccessDenied2 fill:#FFA07A
    style AccessDenied3 fill:#FFA07A
    style AccessDenied4 fill:#FFA07A
    style CheckUserMgmt fill:#ADD8E6
    style CheckMasterData fill:#ADD8E6
    style CheckWorkflow fill:#ADD8E6
    style CheckReports fill:#ADD8E6
    style CheckRegionalScope fill:#ADD8E6
    style CheckReportScope fill:#ADD8E6
