erDiagram
    agencies ||--o{ users : "has"
    agencies ||--o{ campaigns : "owns"
    users ||--o{ campaigns : "creates"
    users ||--o{ proofs : "creates"
    users ||--o{ flagged_items : "submits"
    users ||--o{ resolved_items : "submits"
    users ||--o{ user_change_logs : "subject"
    campaigns ||--o{ proofs : "contains"
    proofs ||--o{ proof_versions : "has"
    proof_versions ||--o{ flagged_items : "has"
    proof_versions ||--o{ resolved_items : "has"
    proof_versions ||--o{ error_items : "has"
    knowledge_bases ||--o{ source_documents : "has"
    knowledge_bases ||--o{ spec_versions : "has"
    knowledge_bases ||--o{ processing_jobs : "has"
    dropdown_options ||--o{ dropdown_options : "parent"

    agencies {
        uuid id PK
        string name UK
        timestamp created_at
    }

    users {
        uuid id PK
        string azure_ad_oid UK
        string email
        string name
        string role
        uuid agency_id FK
        timestamp created_at
    }

    user_change_logs {
        uuid id PK
        uuid user_id FK
        uuid changed_by_id FK
        string change_type
        string field_changed
        string old_value
        string new_value
        timestamp created_at
    }

    campaigns {
        uuid id PK
        string name
        string workfront_id
        string client_lead
        string agency_lead
        string brand_guidelines
        string status
        uuid agency_id FK
        uuid created_by FK
        timestamp created_at
        timestamp updated_at
    }

    proofs {
        uuid id PK
        uuid campaign_id FK
        string proof_name
        string channel
        string sub_channel
        string proof_type
        string workfront_id
        uuid created_by FK
        timestamp created_at
    }

    proof_versions {
        uuid id PK
        uuid proof_id FK
        integer version
        string file_storage_key
        text thumbnail_url
        jsonb agent_review
        string overall_status
        string file_hash
        boolean is_identical_file
        timestamp created_at
    }

    flagged_items {
        uuid id PK
        uuid proof_version_id FK
        string agent_flagged
        text comments
        uuid submitter_id FK
        timestamp created_at
    }

    resolved_items {
        uuid id PK
        uuid proof_version_id FK
        string agent
        text issue
        text resolution
        uuid submitter_id FK
        timestamp created_at
    }

    error_items {
        uuid id PK
        uuid proof_version_id FK
        text error_summary
        timestamp created_at
    }

    knowledge_bases {
        uuid id PK
        string agent_key UK
        string display_name
        text description
        timestamp created_at
    }

    source_documents {
        uuid id PK
        uuid knowledge_base_id FK
        string filename
        string file_storage_key
        integer file_size_bytes
        string mime_type
        uuid uploaded_by_id FK
        string uploaded_by_name
        text parsed_markdown
        string parse_status
        timestamp created_at
    }

    spec_versions {
        uuid id PK
        uuid knowledge_base_id FK
        integer version_number
        text content
        jsonb source_document_ids
        uuid generated_by_id FK
        boolean is_active
        integer char_count
        timestamp created_at
    }

    processing_jobs {
        uuid id PK
        uuid knowledge_base_id FK
        string status
        uuid triggered_by_id FK
        integer total_documents
        integer parsed_documents
        uuid spec_version_id FK
        text error_message
        jsonb log
        timestamp started_at
        timestamp completed_at
        timestamp created_at
    }

    dropdown_options {
        uuid id PK
        string option_type
        uuid parent_id FK
        string value
        integer display_order
        timestamp created_at
    }
