@startuml
title Email Admin Write Validation (After)

skinparam backgroundColor #FFFFFF
skinparam defaultFontColor #000000
skinparam ArrowColor #000000
skinparam SequenceLifeLineBorderColor #000000
skinparam SequenceLifeLineBackgroundColor #F7F7F7
skinparam ParticipantBorderColor #000000
skinparam ParticipantBackgroundColor #D7F0FF
skinparam NoteBackgroundColor #E9FFE9
skinparam NoteBorderColor #000000

actor "Admin Client" as Client
participant "AdminPlugin\n/email* create|update" as Plugin
participant "Admin Handlers\n(normalize + validate)" as Handlers
participant "Tenant Runtime\nDatabaseAdapter" as Adapter
database "tenant.emails / tenant.email_send_failures" as Tables
participant "Admin Audit Middleware" as Audit

Client -> Plugin: POST /admin/email/create\nor /email/update\nor /email-failure/create
Plugin -> Handlers: parsed request body
Handlers -> Handlers: enforce required trimmed values\nvalidate email fields\nreject blank optional overrides

alt validation fails
  Handlers --> Plugin: AuthError::bad_request
  Plugin -> Audit: audit_failure_if_needed(...)
  Audit --> Plugin: failure log persisted
  Plugin --> Client: 4xx
else validation passes
  Handlers -> Adapter: normalized write payload
  Adapter -> Tables: INSERT / UPDATE
  Tables --> Adapter: OK
  Adapter --> Handlers: OK
  Handlers --> Plugin: success response
  Plugin --> Client: 200
end

note right of Handlers
Empty/whitespace writes are blocked.\nEmail identity fields are verified before DB write.
end note

@enduml
