@startuml buyer-side

actor Buyer
participant "eProcurement Frontend" as UI
participant "Negotiation Service" as NEGO
participant "eProcurement Server" as BE

== Buyer fetch negotiation data ==
Buyer -> UI: open negotiation screen
UI -> NEGO: fetch negotiation data
alt negotiation unknown
    NEGO -> BE: fetch quote data
    BE -> NEGO: get quote data (last modified)
    NEGO -> NEGO: create blank negotiation, store last modified of quote
else negotiation known
    NEGO -> NEGO
    note right: nothing to do
end
NEGO -> UI: return negotiation data including state
UI -> Buyer: render negotiation screen\n(quote data and negotiation data merged)

== Buyer pushed negotiation data ==
Buyer -> UI: submit negotiation data (discounts)
UI -> NEGO: discount data
NEGO -> BE: fetch quote data
BE -> NEGO: get quote data (last modified)
NEGO -> NEGO: compare stored last modified with returned result
alt quote did NOT change since last check
    NEGO -> NEGO: store discounts for negotiation
    NEGO --> BE: inform about new negotiation
    NEGO -> UI: return updated negotiation data including state
    UI -> Buyer: render submitted negotiation
else quote did CHANGE since last check
    NEGO -> NEGO: update quotes last modified\ndiscard discount data
    NEGO -> UI: error, negotiation is invalided\ndue to outdated quote data
    UI -> Buyer: display error message\nreload quote data and rerender negotiation screen
end

@enduml
