@startuml

!include ./schemes_setup.wsd
!include ./requests_scheme.wsd

$table("FIELD", "field") {
    $pk("ID") INTEGER NOT NULL
    $enum_link("TYPE") VARCHAR 
    $fk("STEP_ID") INTEGER NOT NULL
    $fk("BLOCK_FIELD_ID") INTEGER NOT NULL
    $column("REQUIRED") BOOLEAN
}

$table("BLOCK_FIELD", "block_field") {
    $pk("ID") INTEGER NOT NULL
    $fk("STEP_ID") INTEGER NOT NULL
    $column("NAME") VARCHAR
    $enum_link("TYPE") VARCHAR NOT NULL
    $column("REQUIRED") BOOLEAN
    $column("FLAGS") JSON
}

$table("STEP", "step") {
    $pk("ID") INTEGER NOT NULL
    $fk("WORKFLOW_ID") INTEGER NOT NULL
    $column("NAME") VARCHAR
    $column("DESCRIPTION") TEXT
    $column("NEED_AGREEMENT") BOOLEAN
    $column("STATUS") VARCHAR
}

$table("RESPONSIBLE", "responsible") {
    $pk("ID") INTEGER NOT NULL
    $fk("STEP_ID") INTEGER NOT NULL
    $column("ASSIGNATION_TYPE") VARCHAR
    $column("IS_PARALLEL_EXECUTION") BOOL
    $column("AVAILABLE_USERS") []INT
    $column("ASSIGNED_USERS") []INT
}

$table("RESPONSIBLE_USER", "responsible_user") {
    $pk("ID") INTEGER NOT NULL
    $fk("RESPONSIBLE") INTEGER NOT NULL
    $column("DONE") BOOL
    $column("LAST_UPDATED") DATE
}

responsible_user::RESPONSIBLE }|--o| responsible::ID

!startsub WORKFLOW_TABLE
$table("WORKFLOW", "workflow") {
    $pk("ID") INTEGER NOT NULL
    $column("NAME") VARCHAR
    $column("CREATED_AT") TIMESTAMP
    $enum_link("TYPE") VARCHAR
    $enum_link("STATUS") VARCHAR
    $column("DESCRIPTION") TEXT
    $column("IS_TEMPLATE") BOOL
}
!endsub

$type("FIELD_TYPE", "field_type") {
    $enum_field("select")
    $enum_field("input")
    $enum_field("datetime")
    $enum_field("multiselect")
    $enum_field("filefield")
    $enum_field("blockfield")
}

$type("BLOCK_FIELD_TYPE", "block_field_type") {
    $enum_field("allow_access_field")
    $enum_field("object")
    $enum_field("device")
}

$type("WORKFLOW_TYPE", "workflow_type") {
    $enum_field("general")
    $enum_field("access_change")
    $enum_field("object_change")
    $enum_field("custom")
}

$type("WORKFLOW_STATUS", "workflow_status") {
    $enum_field("active")
    $enum_field("inactive")
    $enum_field("draft")
}

$type("STEP_STATUS", "step_status") {
    $enum_field("done")
    $enum_field("in_progress")
    $enum_field("todo")
}

field::TYPE -- field_type
field::BLOCK_FIELD_ID }o--|| block_field::ID
workflow::TYPE -- workflow_type
workflow::STATUS -- workflow_status
field::STEP }o--|| step::ID
block_field::STEP }o--|| step::ID
block_field::TYPE -- block_field_type
step::WORKFLOW_ID }|--|| workflow::ID
responsible::STEP_ID ||--|| step::ID
step::STATUS -- step_status

@enduml