# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "クライアント"
        Browser[Web Browser<br/>Turbo/Stimulus]
        Mobile[Mobile App<br/>iOS/Android]
        External[外部システム<br/>Slack/Campfire/Basecamp]
    end

    subgraph "インフラ"
        LB[Thruster<br/>HTTP Proxy]
        Puma[Puma<br/>App Server]
    end

    subgraph "Rails Application"
        subgraph "Presentation Layer"
            Controllers[Controllers]
            Views[Views<br/>ERB/Turbo Streams]
            Channels[Action Cable<br/>Channels]
        end

        subgraph "Application Layer"
            Jobs[Background Jobs<br/>Solid Queue]
            Mailers[Mailers]
            Notifiers[Notifiers]
        end

        subgraph "Domain Layer"
            Models[Models]
            Concerns[Concerns]
        end

        subgraph "Infrastructure Layer"
            AR[Active Record]
            AS[Active Storage]
            AT[Action Text]
        end
    end

    subgraph "データストア"
        PrimaryDB[(Primary DB<br/>SQLite/MySQL)]
        CableDB[(Cable DB<br/>Solid Cable)]
        CacheDB[(Cache DB<br/>Solid Cache)]
        QueueDB[(Queue DB<br/>Solid Queue)]
        FileStore[(File Storage<br/>Disk/S3)]
    end

    subgraph "外部サービス"
        SMTP[SMTP Server]
        S3[S3 Compatible<br/>Storage]
        WebPush[Web Push<br/>Service]
    end

    Browser --> LB
    Mobile --> LB
    LB --> Puma
    Puma --> Controllers
    Controllers --> Views
    Controllers --> Models
    Controllers --> Jobs

    Channels --> Models

    Jobs --> Models
    Jobs --> Mailers
    Jobs --> Notifiers

    Models --> Concerns
    Models --> AR
    Models --> AS
    Models --> AT

    AR --> PrimaryDB
    AR --> CableDB
    AR --> CacheDB
    AR --> QueueDB

    AS --> FileStore
    AS --> S3

    Mailers --> SMTP
    Notifiers --> WebPush

    External -.->|Webhook| LB
```

## レイヤー構成図

```mermaid
graph TB
    subgraph "Presentation Layer"
        AC[ApplicationController]
        BC[BoardsController]
        CC[CardsController]
        UC[UsersController]
        NC[NotificationsController]
    end

    subgraph "Controller Concerns"
        Auth[Authentication]
        Authz[Authorization]
        CR[CurrentRequest]
        CT[CurrentTimezone]
    end

    subgraph "Application Layer"
        AJ[ApplicationJob]
        WDJ[Webhook::DispatchJob]
        PNJ[PushNotificationJob]
        NRJ[NotifyRecipientsJob]
        AM[ApplicationMailer]
        MLM[MagicLinkMailer]
        NM[NotificationMailer]
    end

    subgraph "Domain Layer"
        Account[Account]
        User[User]
        Identity[Identity]
        Board[Board]
        Card[Card]
        Comment[Comment]
        Event[Event]
        Notification[Notification]
        Webhook[Webhook]
    end

    subgraph "Model Concerns"
        Accessible[Accessible]
        Attachments[Attachments]
        Eventable[Eventable]
        Notifiable[Notifiable]
        Searchable[Searchable]
        Mentionable[Mentions]
    end

    subgraph "Infrastructure Layer"
        AR[ActiveRecord::Base]
        AS[ActiveStorage]
        AT[ActionText]
        SC[SolidCache]
        SQ[SolidQueue]
        SCable[SolidCable]
    end

    AC --> Auth
    AC --> Authz
    AC --> CR
    AC --> CT

    BC --> AC
    CC --> AC
    UC --> AC
    NC --> AC

    BC --> Board
    CC --> Card
    UC --> User
    NC --> Notification

    Card --> Accessible
    Card --> Attachments
    Card --> Eventable
    Card --> Notifiable
    Card --> Searchable
    Card --> Mentionable

    Account --> AR
    User --> AR
    Board --> AR
    Card --> AR
    Card --> AS
    Card --> AT
```

## データフロー図

### リクエスト処理フロー

```mermaid
sequenceDiagram
    participant C as Client
    participant P as Puma
    participant MW as Middleware
    participant R as Router
    participant Ctrl as Controller
    participant M as Model
    participant DB as Database
    participant J as Job Queue

    C->>P: HTTP Request
    P->>MW: Process Request
    MW->>MW: CSP Check
    MW->>MW: Session Load
    MW->>R: Route Request
    R->>Ctrl: Dispatch to Action

    Ctrl->>Ctrl: require_account
    Ctrl->>Ctrl: require_authentication
    Ctrl->>Ctrl: ensure_can_access_account

    Ctrl->>M: Business Logic
    M->>DB: Query/Update
    DB-->>M: Result

    alt Async Processing Needed
        M->>J: Enqueue Job
    end

    M-->>Ctrl: Result
    Ctrl->>Ctrl: Render View
    Ctrl-->>P: Response
    P-->>C: HTTP Response
```

### 非同期処理フロー

```mermaid
sequenceDiagram
    participant Ctrl as Controller
    participant M as Model
    participant Q as Solid Queue
    participant W as Worker
    participant J as Job
    participant Mail as Mailer
    participant SMTP as SMTP Server

    Ctrl->>M: Create Event
    M->>M: after_create_commit
    M->>Q: Enqueue WebhookDispatchJob

    loop Polling (0.1s interval)
        W->>Q: Fetch Jobs
        Q-->>W: Job Data
    end

    W->>J: Execute Job
    J->>M: Load Event
    M-->>J: Event Data
    J->>J: Build Payload
    J->>External: HTTP POST (Webhook)

    alt Notification Required
        J->>Mail: Send Notification
        Mail->>SMTP: Deliver Email
    end
```

## コンポーネント依存関係図

```mermaid
graph LR
    subgraph "認証・認可"
        Identity
        Session
        MagicLink
        AccessToken[Identity::AccessToken]
    end

    subgraph "コアドメイン"
        Account
        User
        Board
        Card
        Column
        Comment
    end

    subgraph "補助機能"
        Tag
        Tagging
        Step
        Closure
        Reaction
        Mention
    end

    subgraph "通知・イベント"
        Event
        Notification
        Webhook
        NotificationPusher
    end

    subgraph "検索・フィルター"
        Search
        Filter
    end

    Identity --> Session
    Identity --> MagicLink
    Identity --> AccessToken
    Identity --> User

    User --> Account
    User --> Identity

    Board --> Account
    Board --> User

    Card --> Board
    Card --> Account
    Card --> User

    Column --> Board

    Comment --> Card
    Comment --> User

    Tag --> Account
    Tagging --> Card
    Tagging --> Tag

    Step --> Card
    Closure --> Card
    Reaction --> Card
    Reaction --> Comment
    Mention --> Card
    Mention --> Comment

    Event --> Card
    Event --> Comment
    Event --> Board

    Notification --> User
    Notification --> Event

    Webhook --> Board
    Webhook --> Event

    Filter --> User
    Filter --> Board
```

## マルチデータベース構成図

```mermaid
graph TB
    subgraph "Rails Application"
        AR[Active Record]
    end

    subgraph "Primary Database"
        PDB[(Primary<br/>アプリケーションデータ)]
    end

    subgraph "Cable Database"
        CDB[(Cable<br/>WebSocketメッセージ)]
    end

    subgraph "Cache Database"
        CADB[(Cache<br/>キャッシュデータ)]
    end

    subgraph "Queue Database"
        QDB[(Queue<br/>ジョブデータ)]
    end

    AR -->|"connects_to :primary"| PDB
    AR -->|"connects_to :cable"| CDB
    AR -->|"connects_to :cache"| CADB
    AR -->|"connects_to :queue"| QDB

    subgraph "SQLite Mode"
        S1[storage/development.sqlite3]
        S2[storage/development_cable.sqlite3]
        S3[storage/production_cache.sqlite3]
        S4[storage/production_queue.sqlite3]
    end

    subgraph "MySQL Mode"
        M1[fizzy_production]
        M2[fizzy_production_cable]
        M3[fizzy_production_cache]
        M4[fizzy_production_queue]
    end
```

## マルチテナント構成図

```mermaid
graph TB
    subgraph "リクエスト処理"
        Req[HTTP Request]
        MW[MultiTenant Middleware]
        Current[Current.account]
    end

    subgraph "テナント分離"
        A1[Account 1]
        A2[Account 2]
        A3[Account 3]
    end

    subgraph "データスコープ"
        U1[Users<br/>account_id = 1]
        B1[Boards<br/>account_id = 1]
        C1[Cards<br/>account_id = 1]

        U2[Users<br/>account_id = 2]
        B2[Boards<br/>account_id = 2]
        C2[Cards<br/>account_id = 2]
    end

    Req --> MW
    MW -->|"Set Current.account"| Current

    Current --> A1
    Current --> A2
    Current --> A3

    A1 --> U1
    A1 --> B1
    A1 --> C1

    A2 --> U2
    A2 --> B2
    A2 --> C2
```

## 認証フロー図

```mermaid
sequenceDiagram
    participant U as User
    participant B as Browser
    participant C as Controller
    participant Auth as Authentication
    participant I as Identity
    participant S as Session
    participant ML as MagicLink

    alt Session Authentication
        U->>B: Access Protected Page
        B->>C: HTTP Request with Cookie
        C->>Auth: resume_session
        Auth->>S: find_session_by_cookie
        S-->>Auth: Session
        Auth->>Auth: set_current_session
        Auth-->>C: Authenticated
    end

    alt Magic Link Authentication
        U->>B: Request Magic Link
        B->>C: POST /session/magic_link
        C->>I: find_by(email_address)
        I->>ML: create!
        ML->>ML: generate_token
        C->>Mailer: send_magic_link
        Mailer-->>U: Email with Link

        U->>B: Click Magic Link
        B->>C: GET /session/magic_link/:token
        C->>ML: find_by_token
        ML-->>C: MagicLink
        C->>S: create_session
        S-->>C: Session
        C->>C: set_session_cookie
        C-->>B: Redirect to Home
    end

    alt Bearer Token Authentication
        U->>B: API Request
        B->>C: Request with Authorization Header
        C->>Auth: authenticate_by_bearer_token
        Auth->>I: find_by_permissable_access_token
        I-->>Auth: Identity
        Auth->>Auth: Current.identity = identity
        Auth-->>C: Authenticated
    end
```
