# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "Client Layer"
        WebApp[Web Dashboard<br/>Vue.js 3]
        Widget[Chat Widget<br/>Vue.js]
        Mobile[Mobile Apps]
        API_Client[External API Clients]
    end

    subgraph "Edge Layer"
        CDN[CDN / Load Balancer]
        RackAttack[Rack::Attack<br/>Rate Limiting]
    end

    subgraph "Application Layer"
        subgraph "Web Servers"
            Puma[Puma<br/>Application Server]
        end

        subgraph "Background Processing"
            Sidekiq[Sidekiq Workers]
            SidekiqCron[Sidekiq-Cron<br/>Scheduled Jobs]
        end

        subgraph "Real-time"
            ActionCable[ActionCable<br/>WebSocket Server]
        end
    end

    subgraph "Rails Application"
        subgraph "Presentation"
            Controllers[Controllers]
            Policies[Pundit Policies]
            Views[Views / Jbuilder]
        end

        subgraph "Application Services"
            Services[Service Classes]
            Builders[Builders]
            Finders[Finders]
            Actions[Actions]
        end

        subgraph "Domain"
            Models[ActiveRecord Models]
            Concerns[Model Concerns]
            Dispatchers[Event Dispatchers]
            Listeners[Event Listeners]
        end

        subgraph "Infrastructure"
            Jobs[Background Jobs]
            Mailers[ActionMailer]
            Mailboxes[ActionMailbox]
        end
    end

    subgraph "Data Layer"
        PostgreSQL[(PostgreSQL 16<br/>+ pgvector)]
        Redis[(Redis<br/>Cache/Queue/PubSub)]
    end

    subgraph "External Services"
        OpenSearch[(OpenSearch)]
        S3[AWS S3 / GCS / Azure]
        SMTP[SMTP Server]

        subgraph "Messaging Channels"
            Facebook[Facebook/Meta]
            WhatsApp[WhatsApp]
            Twitter[Twitter/X]
            Instagram[Instagram]
            Telegram[Telegram]
            Line[LINE]
            TikTok[TikTok]
            Twilio[Twilio SMS/Voice]
        end

        subgraph "Integrations"
            Slack[Slack]
            Linear_Int[Linear]
            Shopify_Int[Shopify]
            Notion_Int[Notion]
        end

        subgraph "AI Services"
            OpenAI[OpenAI]
            Dialogflow[Google Dialogflow]
            Translate[Google Translate]
        end

        subgraph "Monitoring"
            Sentry[Sentry]
            APM[APM Services]
        end
    end

    %% Client connections
    WebApp --> CDN
    Widget --> CDN
    Mobile --> CDN
    API_Client --> CDN

    CDN --> RackAttack
    RackAttack --> Puma

    %% WebSocket
    WebApp --> ActionCable
    Widget --> ActionCable
    ActionCable --> Redis

    %% Application flow
    Puma --> Controllers
    Controllers --> Policies
    Controllers --> Services
    Services --> Builders
    Services --> Finders
    Services --> Models
    Models --> Dispatchers
    Dispatchers --> Listeners
    Listeners --> Jobs

    %% Background processing
    Jobs --> Sidekiq
    Sidekiq --> Redis
    SidekiqCron --> Redis

    %% Data storage
    Models --> PostgreSQL
    Sidekiq --> PostgreSQL

    %% External integrations
    Services --> Facebook
    Services --> WhatsApp
    Services --> Twitter
    Services --> Instagram
    Services --> Telegram
    Services --> Line
    Services --> TikTok
    Services --> Twilio
    Services --> Slack
    Services --> Linear_Int
    Services --> Shopify_Int
    Services --> Notion_Int
    Services --> OpenAI
    Services --> Dialogflow
    Services --> Translate

    %% Storage
    Models --> S3
    Jobs --> OpenSearch
    Mailers --> SMTP

    %% Monitoring
    Puma --> Sentry
    Sidekiq --> Sentry
    Puma --> APM
```

## レイヤー構成図

```mermaid
graph TB
    subgraph "Presentation Layer"
        direction TB
        API_V1[API v1 Controllers]
        API_V2[API v2 Controllers]
        Platform_API[Platform API]
        Public_API[Public API]
        Webhooks[Webhook Controllers]
        SuperAdmin[Super Admin]
        Policies[Pundit Policies]
    end

    subgraph "Application Layer"
        direction TB
        Services[Services]
        Builders[Builders]
        Finders[Finders]
        Actions[Actions]
    end

    subgraph "Domain Layer"
        direction TB
        Models[Models]
        Concerns[Concerns]
        Channels[Channel Models]
        Dispatchers[Dispatchers]
        Listeners[Listeners]
        Drops[Liquid Drops]
    end

    subgraph "Infrastructure Layer"
        direction TB
        Jobs[Sidekiq Jobs]
        Mailers[Mailers]
        Mailboxes[Mailboxes]
        Integrations[Integration Libs]
        Captain[Captain AI Lib]
    end

    API_V1 --> Services
    API_V2 --> Services
    Platform_API --> Services
    Public_API --> Services
    Webhooks --> Services
    SuperAdmin --> Services

    Services --> Models
    Builders --> Models
    Finders --> Models
    Actions --> Models

    Models --> Jobs
    Listeners --> Jobs
    Dispatchers --> Listeners
```

## イベント駆動アーキテクチャ

```mermaid
sequenceDiagram
    participant Controller
    participant Service
    participant Model
    participant Dispatcher
    participant SyncDispatcher
    participant AsyncDispatcher
    participant EventDispatcherJob
    participant Listener
    participant Job

    Controller->>Service: ビジネスロジック実行
    Service->>Model: データ操作
    Model->>Model: コールバック実行
    Model->>Dispatcher: Dispatcher.dispatch(event)

    Dispatcher->>SyncDispatcher: dispatch(event)
    SyncDispatcher->>Listener: ActionCableListener
    Listener->>Controller: WebSocket通知

    Dispatcher->>AsyncDispatcher: dispatch(event)
    AsyncDispatcher->>EventDispatcherJob: perform_later
    EventDispatcherJob->>AsyncDispatcher: publish_event
    AsyncDispatcher->>Listener: 各種Listener
    Listener->>Job: perform_later
    Job->>Job: 非同期処理実行
```

## 認証・認可フロー

```mermaid
flowchart TB
    subgraph "認証フロー"
        Request[HTTP Request]
        DeviseToken[DeviseTokenAuth]
        Session[Session認証]
        JWT_Auth[JWT認証]
        OAuth[OAuth2/SAML]
        MFA[MFA検証]
        Authenticated[認証済み]

        Request --> DeviseToken
        Request --> Session
        Request --> JWT_Auth
        Request --> OAuth

        DeviseToken --> MFA
        Session --> MFA
        JWT_Auth --> Authenticated
        OAuth --> MFA

        MFA --> Authenticated
    end

    subgraph "認可フロー"
        Authenticated --> Controller
        Controller --> Pundit[Pundit Policy]
        Pundit -->|許可| Action[アクション実行]
        Pundit -->|拒否| Unauthorized[401/403]
    end

    subgraph "レート制限"
        RackAttack[Rack::Attack]
        RackAttack -->|許可| Request
        RackAttack -->|制限| TooMany[429 Too Many Requests]
    end
```

## データフロー図

```mermaid
flowchart LR
    subgraph "入力"
        WebUI[Web UI]
        ChatWidget[Chat Widget]
        ChannelWebhook[Channel Webhooks]
        Email[Inbound Email]
        PublicAPI[Public API]
    end

    subgraph "処理"
        Controllers[Controllers]
        Services[Services]
        Models[Models]
    end

    subgraph "永続化"
        PostgreSQL[(PostgreSQL)]
        Redis[(Redis)]
        S3[(Object Storage)]
    end

    subgraph "出力"
        ActionCable[WebSocket]
        OutboundEmail[Outbound Email]
        ChannelAPI[Channel APIs]
        Webhook[Outbound Webhooks]
        Notifications[Push Notifications]
    end

    WebUI --> Controllers
    ChatWidget --> Controllers
    ChannelWebhook --> Controllers
    Email --> Controllers
    PublicAPI --> Controllers

    Controllers --> Services
    Services --> Models

    Models --> PostgreSQL
    Models --> Redis
    Models --> S3

    Models --> ActionCable
    Models --> OutboundEmail
    Models --> ChannelAPI
    Models --> Webhook
    Models --> Notifications
```

## Sidekiqジョブキュー構成

```mermaid
flowchart TB
    subgraph "優先度: Critical"
        CriticalQueue[critical]
    end

    subgraph "優先度: High"
        HighQueue[high]
    end

    subgraph "優先度: Medium"
        MediumQueue[medium]
    end

    subgraph "優先度: Default"
        DefaultQueue[default]
        MailersQueue[mailers]
        MailboxRouting[action_mailbox_routing]
    end

    subgraph "優先度: Low"
        LowQueue[low]
        ScheduledJobs[scheduled_jobs]
        Deferred[deferred]
    end

    subgraph "優先度: Background"
        Purgable[purgable]
        Housekeeping[housekeeping]
        BulkReindex[bulk_reindex_low]
        ActiveStorage[active_storage_*]
    end

    Redis[(Redis)] --> CriticalQueue
    CriticalQueue --> HighQueue
    HighQueue --> MediumQueue
    MediumQueue --> DefaultQueue
    DefaultQueue --> LowQueue
    LowQueue --> Purgable
```

## モジュール依存関係図

```mermaid
graph TB
    subgraph "Core Domain"
        Account[Account]
        User[User]
        Conversation[Conversation]
        Message[Message]
        Contact[Contact]
        Inbox[Inbox]
    end

    subgraph "Channel Implementations"
        WebWidget[Channel::WebWidget]
        FacebookPage[Channel::FacebookPage]
        WhatsApp_Ch[Channel::Whatsapp]
        Twitter_Ch[Channel::TwitterProfile]
        Instagram_Ch[Channel::Instagram]
        Telegram_Ch[Channel::Telegram]
        Email_Ch[Channel::Email]
        SMS_Ch[Channel::Sms]
        TwilioSms[Channel::TwilioSms]
        Line_Ch[Channel::Line]
        API_Ch[Channel::Api]
        TikTok_Ch[Channel::Tiktok]
    end

    subgraph "Supporting Domains"
        Team[Team]
        Label[Label]
        Campaign[Campaign]
        AutomationRule[AutomationRule]
        Macro[Macro]
        Notification[Notification]
        Webhook_Model[Webhook]
    end

    subgraph "Knowledge Base"
        Portal[Portal]
        Article[Article]
        Category[Category]
    end

    subgraph "Enterprise"
        Enterprise_Features[Enterprise Features]
    end

    Account --> User
    Account --> Inbox
    Account --> Conversation
    Account --> Contact
    Account --> Team
    Account --> Campaign

    Inbox --> WebWidget
    Inbox --> FacebookPage
    Inbox --> WhatsApp_Ch
    Inbox --> Twitter_Ch
    Inbox --> Instagram_Ch
    Inbox --> Telegram_Ch
    Inbox --> Email_Ch
    Inbox --> SMS_Ch
    Inbox --> TwilioSms
    Inbox --> Line_Ch
    Inbox --> API_Ch
    Inbox --> TikTok_Ch

    Conversation --> Message
    Conversation --> Contact
    Conversation --> Inbox

    Account --> Portal
    Portal --> Article
    Portal --> Category

    User -.-> Enterprise_Features
    Account -.-> Enterprise_Features
```

## フロントエンドアーキテクチャ

```mermaid
graph TB
    subgraph "Vue.js Application"
        subgraph "Entry Points"
            Dashboard[Dashboard App]
            Widget_FE[Widget App]
            Portal_FE[Portal App]
            Survey[Survey App]
        end

        subgraph "State Management"
            Vuex[Vuex Store]
            Pinia[Pinia Store]
            Modules[Store Modules]
        end

        subgraph "Components"
            Components[Components]
            ComponentsNext[Components-Next]
            Composables[Composables]
        end

        subgraph "API Layer"
            APIClient[API Client]
            CacheEnabled[Cache Enabled Client]
        end

        subgraph "Utilities"
            Helpers[Helpers]
            Constants[Constants]
            I18n[i18n]
        end
    end

    subgraph "Backend API"
        Rails_API[Rails API]
        ActionCable_WS[ActionCable WebSocket]
    end

    Dashboard --> Vuex
    Dashboard --> Pinia
    Widget_FE --> Vuex
    Portal_FE --> Vuex
    Survey --> Vuex

    Vuex --> Modules

    Dashboard --> Components
    Dashboard --> ComponentsNext
    Dashboard --> Composables

    Components --> APIClient
    ComponentsNext --> APIClient
    APIClient --> CacheEnabled

    APIClient --> Rails_API
    Dashboard --> ActionCable_WS
```
