@startuml CompetitorProductMultipleSave
scale 0.8
actor Client
participant "API Endpoint" as API
participant "Validation" as Validation
participant "Database" as DB
participant "Kafka" as Kafka
participant "Logging" as Log

autonumber
Client -> API: POST /v1/ui/competitor-product/multiple-save\n{JSON request}
API -> Validation: Deserialize JSON to Protobuf

alt Deserialization error
    Validation -> Log: ERROR "PRC-MATCH-1501 | <error>"
    API -> Client: 400 Bad Request\n{error details}
    return
end

group Type = CREATE (0)
    Validation -> Validation: Check for IDs in CREATE mode
    loop For each invalid object
        Validation -> Log: WARN "PRC-MATCH-1502 | <warning>"
    end

    Validation -> Validation: Check for duplicates in request
    loop For each duplicate
        Validation -> Log: WARN "PRC-MATCH-1503 | <warning>"
    end

    Validation -> DB: Bulk SELECT by (competitor_id + sku)
    alt DB error
        DB -> Log: ERROR "PRC-MATCH-1504 | <error>"
        API -> Client: 500 Server Error\n{error response}

    end

    loop For each existing record
        Validation -> Log: WARN "PRC-MATCH-1505 | <warning>"
    end

    DB -> API: Return existing records
    Validation -> Validation: Filter valid records

    alt No valid records
        API -> Client: 200 OK\n{empty response}

    end

    API -> DB: Bulk INSERT with RETURNING id
    alt DB error
        DB -> Log: ERROR "PRC-MATCH-1504 | <error>"
        API -> Client: 500 Server Error\n{error response}

    end
    DB -> API: Return new IDs
end

group Type = UPDATE (1)
    Validation -> Validation: Check for missing IDs
    loop For each invalid object
        Validation -> Log: WARN "PRC-MATCH-1502 | <warning>"
    end

    Validation -> Validation: Check for duplicate IDs
    loop For each duplicate
        Validation -> Log: WARN "PRC-MATCH-1503 | <warning>"
    end

    Validation -> DB: Bulk SELECT by IDs
    alt DB error
        DB -> Log: ERROR "PRC-MATCH-1504 | <error>"
        API -> Client: 500 Server Error\n{error response}
        return
    end

    loop For each not found ID
        Validation -> Log: WARN "PRC-MATCH-1505 | <warning>"
    end

    Validation -> Validation: Validate status transitions
    loop For each invalid transition
        Validation -> Log: WARN "PRC-MATCH-1506 | <warning>"
    end

    alt No valid records
        API -> Client: 200 OK\n{empty response}
        return
    end

    API -> DB: Bulk UPDATE
    alt DB error
        DB -> Log: ERROR "PRC-MATCH-1504 | <error>"
        API -> Client: 500 Server Error\n{error response}
        return
    end
end

API -> Kafka: Send to parsing.collector.out
alt Kafka error
    Kafka -> Log: ERROR "PRC-MATCH-1507 | <error>"
    API -> Client: 500 Server Error\n{error response}
    return
end

API -> Kafka: Send to parsing.competitor_product_status.update
alt Kafka error
    Kafka -> Log: ERROR "PRC-MATCH-1508 | <error>"
    API -> Client: 500 Server Error\n{error response}
    return
end

API -> Client: 200 OK\n{success response}
API -> Log: INFO "PRC-MATCH-1509 | <success>"
@enduml