# MediaVerse — System Architecture

## 1. Architecture Overview

MediaVerse employs a **Modular Monolith** architecture that balances the simplicity of a monolithic application with the maintainability of modular design. This approach enables rapid development while preserving the ability to extract services into microservices as the platform scales.

### Architecture Principles

1. **Modularity**: Clear boundaries between functional domains
2. **Testability**: Each module independently testable
3. **Scalability**: Horizontal scaling of worker processes
4. **Resilience**: Graceful degradation and recovery
5. **Observability**: Comprehensive logging, metrics, and tracing

---

## 2. Architecture Comparison

### 2.1 Monolith vs Modular Monolith vs Microservices

| Aspect | Monolith | Modular Monolith | Microservices |
|--------|----------|------------------|---------------|
| **Complexity** | Low | Medium | High |
| **Deployment** | Single unit | Single unit | Multiple units |
| **Team Scaling** | Limited | Good | Excellent |
| **Performance** | Excellent | Excellent | Network overhead |
| **Fault Isolation** | Poor | Good | Excellent |
| **Data Consistency** | Simple | Simple | Complex (distributed) |
| **Development Speed** | Fast | Fast | Slower |
| **Operational Cost** | Low | Low | High |
| **Technology Diversity** | Limited | Limited | Flexible |

### 2.2 Recommended Architecture: Modular Monolith

**Rationale:**

1. **Team Size**: Initial team of 6-8 developers fits well within modular monolith
2. **Time to Market**: Faster development than microservices
3. **Complexity**: Media processing is complex; avoid distributed system complexity
4. **Performance**: Local IPC faster than network calls
5. **Future Path**: Can extract to microservices when needed

**When to Migrate to Microservices:**
- Team grows beyond 15 developers
- Specific modules need independent scaling
- Different modules need different technology stacks
- Compliance requires service isolation

---

## 3. System Architecture Diagram

### 3.1 High-Level Architecture

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                              MediaVerse Application                          │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                         Presentation Layer                           │   │
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  ┌───────────┐ │   │
│  │  │   React UI  │  │  System Tray│  │   Shortcuts │  │  Menubar  │ │   │
│  │  │  (Renderer) │  │   (Native)  │  │   (Global)  │  │  (Native) │ │   │
│  │  └──────┬──────┘  └─────────────┘  └─────────────┘  └───────────┘ │   │
│  │         │                                                           │   │
│  │         ▼                                                           │   │
│  │  ┌─────────────────────────────────────────────────────────────────┐ │   │
│  │  │                    Tauri Bridge / IPC                            │ │   │
│  │  │         (TypeScript ↔ Rust communication layer)                  │ │   │
│  │  └─────────────────────────────────────────────────────────────────┘ │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    ▼                                        │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                         Application Core                             │   │
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  ┌───────────┐ │   │
│  │  │   Event Bus │  │    State    │  │   Config    │  │   Auth    │ │   │
│  │  │   (Pub/Sub) │  │  (Zustand)  │  │   Manager   │  │   (Local) │ │   │
│  │  └──────┬──────┘  └─────────────┘  └─────────────┘  └───────────┘ │   │
│  │         │                                                           │   │
│  │         ▼                                                           │   │
│  │  ┌─────────────────────────────────────────────────────────────────┐ │   │
│  │  │                      Module Interface                            │ │   │
│  │  │              (Clean Architecture / Ports & Adapters)           │ │   │
│  │  └─────────────────────────────────────────────────────────────────┘ │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    ▼                                        │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                         Domain Modules                               │   │
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  ┌───────────┐ │   │
│  │  │  Downloader │  │   Media     │  │ Automation  │  │   User    │ │   │
│  │  │   Module    │  │   Manager   │  │   Engine    │  │  Module   │ │   │
│  │  │             │  │   Module    │  │             │  │           │ │   │
│  │  │ • URL Parse │  │ • Library   │  │ • Rules     │  │ • Profile │ │   │
│  │  │ • Download  │  │ • Search    │  │ • Scheduler │  │ • Settings│ │   │
│  │  │ • Convert   │  │ • Organize  │  │ • Workers   │  │ • History │ │   │
│  │  │ • Queue     │  │ • Preview   │  │ • Events    │  │ • Sync    │ │   │
│  │  └──────┬──────┘  └──────┬──────┘  └──────┬──────┘  └─────┬─────┘ │   │
│  │         │                │                │               │       │   │
│  │         └────────────────┴────────────────┴───────────────┘       │   │
│  │                              │                                     │   │
│  │                              ▼                                     │   │
│  │  ┌─────────────────────────────────────────────────────────────────┐ │   │
│  │  │                    Infrastructure Layer                          │ │   │
│  │  │  ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐  │ │   │
│  │  │  │ SQLite  │ │  FS     │ │  HTTP   │ │ FFmpeg  │ │  yt-dlp │  │ │   │
│  │  │  │ (DB)    │ │ (Files) │ │ (Client)│ │ (Media) │ │ (DL)    │  │ │   │
│  │  │  └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘  │ │   │
│  │  └─────────────────────────────────────────────────────────────────┘ │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────────┐
│                           External Services                                  │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  ┌─────────────────┐  │
│  │   YouTube   │  │   Vimeo     │  │   Other     │  │   Update Server   │  │
│  │   API/CDN   │  │   API       │  │   Sources   │  │   (Tauri)         │  │
│  └─────────────┘  └─────────────┘  └─────────────┘  └─────────────────┘  │
│                                                                             │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  ┌─────────────────┐  │
│  │   PostHog   │  │   Sentry    │  │   Cloud     │  │   License       │  │
│  │ (Analytics) │  │   (Errors)  │  │   Storage   │  │   Server        │  │
│  └─────────────┘  └─────────────┘  └─────────────┘  └─────────────────┘  │
└─────────────────────────────────────────────────────────────────────────────┘
```

### 3.2 Module Dependencies

```
┌─────────────────────────────────────────────────────────────┐
│                    Dependency Graph                          │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│                         ┌─────────┐                        │
│                         │  Core   │                        │
│                         │ (Event, │                        │
│                         │ Config) │                        │
│                         └────┬────┘                        │
│                              │                               │
│         ┌────────────────────┼────────────────────┐         │
│         │                    │                    │         │
│         ▼                    ▼                    ▼         │
│    ┌─────────┐        ┌─────────┐          ┌─────────┐   │
│    │Downloader│◀──────▶│  Media  │◀─────────▶│Automation│   │
│    │ Module  │        │ Manager │            │ Engine  │   │
│    └────┬────┘        └────┬────┘            └────┬────┘   │
│         │                  │                      │         │
│         │                  │                      │         │
│         ▼                  ▼                      ▼         │
│    ┌─────────┐        ┌─────────┐          ┌─────────┐   │
│    │Infrastructure│   │Infrastructure│       │Infrastructure│ │
│    │ (yt-dlp,   │    │ (SQLite,   │        │ (Queue,   │   │
│    │ FFmpeg)   │     │ Thumbnails)│        │ Scheduler)│   │
│    └─────────┘        └─────────┘          └─────────┘   │
│                                                              │
│  Legend: ───▶ depends on                                    │
│                                                              │
└─────────────────────────────────────────────────────────────┘
```

---

## 4. C4 Diagrams

### 4.1 Context Diagram (C4 Level 1)

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                              System Context                                  │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│                              ┌─────────────┐                                │
│                              │   User      │                                │
│                              │ (Consumer,  │                                │
│                              │  Creator)   │                                │
│                              └──────┬──────┘                                │
│                                     │                                        │
│                                     │ Uses                                    │
│                                     │                                        │
│                                     ▼                                        │
│  ┌─────────────┐           ┌─────────────────┐           ┌─────────────┐  │
│  │   Media     │◀─────────▶│    MediaVerse   │◀─────────▶│   Update    │  │
│  │   Sources   │  Fetches  │   Application   │  Receives   │   Server    │  │
│  │(YouTube,   │  content  │                 │  updates    │             │  │
│  │ Vimeo, etc)│           │                 │             │             │  │
│  └─────────────┘           └────────┬────────┘             └─────────────┘  │
│                                     │                                        │
│                                     │ Sends telemetry                         │
│                                     │                                        │
│                                     ▼                                        │
│                              ┌─────────────┐                                │
│                              │  Analytics  │                                │
│                              │  (PostHog)  │                                │
│                              └─────────────┘                                │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

### 4.2 Container Diagram (C4 Level 2)

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                              Container Diagram                               │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                         Desktop Application                          │   │
│  │                                                                     │   │
│  │  ┌─────────────┐    ┌─────────────┐    ┌─────────────────────────┐│   │
│  │  │  React UI   │    │  Tauri      │    │    Rust Backend Core      ││   │
│  │  │  (WebView)  │◀──▶│  Bridge     │◀──▶│  • Business Logic         ││   │
│  │  │             │IPC │  (Native)   │    │  • Module Orchestration   ││   │
│  │  │ • Components│    │             │    │  • Event Bus              ││   │
│  │  │ • State     │    │ • Window    │    │  • Configuration          ││   │
│  │  │ • Routing   │    │ • Menu      │    │  • Security               ││   │
│  │  │ • Styling   │    │ • Shortcuts │    │                           ││   │
│  │  └─────────────┘    └─────────────┘    └───────────┬───────────────┘│   │
│  │                                                     │                  │   │
│  │                                                     │ Uses             │   │
│  │                                                     │                  │   │
│  │                                                     ▼                  │   │
│  │  ┌─────────────┐    ┌─────────────┐    ┌─────────────────────────┐│   │
│  │  │   SQLite    │    │   File      │    │    External Processes   ││   │
│  │  │   Database  │◀──▶│   System    │◀──▶│  • yt-dlp (downloads)   ││   │
│  │  │             │    │             │    │  • FFmpeg (conversion)    ││   │
│  │  │ • Media     │    │ • Downloads│    │  • Thumbnail generator    ││   │
│  │  │ • Settings  │    │ • Library   │    │                           ││   │
│  │  │ • History   │    │ • Temp      │    │                           ││   │
│  │  └─────────────┘    └─────────────┘    └─────────────────────────┘│   │
│  │                                                                     │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    │ HTTPS                                  │
│                                    │                                        │
│                                    ▼                                        │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                         External Services                            │   │
│  │  ┌─────────┐  ┌─────────┐  ┌─────────┐  ┌─────────┐  ┌─────────┐   │   │
│  │  │ YouTube │  │  Vimeo  │  │ PostHog │  │ Sentry  │  │ License │   │   │
│  │  │   API   │  │   API   │  │Analytics│  │ Errors  │  │ Server  │   │   │
│  │  └─────────┘  └─────────┘  └─────────┘  └─────────┘  └─────────┘   │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

### 4.3 Component Diagram (C4 Level 3) — Downloader Module

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                    Downloader Module — Component Diagram                       │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                      Downloader Module                               │   │
│  │                                                                     │   │
│  │  ┌─────────────────────────────────────────────────────────────────┐ │   │
│  │  │                    Download Manager                              │ │   │
│  │  │  • Orchestrates download lifecycle                              │ │   │
│  │  │  • Manages concurrent downloads                                 │ │   │
│  │  │  • Handles state transitions                                    │ │   │
│  │  └─────────────────────────────────────────────────────────────────┘ │   │
│  │                              │                                       │   │
│  │         ┌────────────────────┼────────────────────┐                 │   │
│  │         │                    │                    │                 │   │
│  │         ▼                    ▼                    ▼                 │   │
│  │  ┌─────────────┐      ┌─────────────┐      ┌─────────────┐       │   │
│  │  │   Queue     │      │   Worker    │      │   State     │       │   │
│  │  │   Manager   │◀────▶│   Pool      │◀────▶│   Machine   │       │   │
│  │  │             │      │             │      │             │       │   │
│  │  │ • Priority  │      │ • Execute   │      │ • Transitions│      │   │
│  │  │ • Scheduling│      │ • Monitor   │      │ • Persistence│      │   │
│  │  │ • Limits    │      │ • Recovery  │      │ • Events    │       │   │
│  │  └─────────────┘      └──────┬──────┘      └─────────────┘       │   │
│  │                              │                                       │   │
│  │         ┌────────────────────┼────────────────────┐                 │   │
│  │         │                    │                    │                 │   │
│  │         ▼                    ▼                    ▼                 │   │
│  │  ┌─────────────┐      ┌─────────────┐      ┌─────────────┐       │   │
│  │  │   URL       │      │   Platform  │      │   Format    │       │   │
│  │  │   Parser    │      │   Adapter   │      │   Selector  │       │   │
│  │  │             │      │             │      │             │       │   │
│  │  │ • Validate  │      │ • YouTube   │      │ • Quality   │       │   │
│  │  │ • Extract   │      │ • Vimeo     │      │ • Codec     │       │   │
│  │  │ • Normalize │      │ • Generic   │      │ • Size      │       │   │
│  │  └─────────────┘      └──────┬──────┘      └─────────────┘       │   │
│  │                              │                                       │   │
│  │                              │ Uses                                  │   │
│  │                              │                                       │   │
│  │                              ▼                                       │   │
│  │  ┌─────────────────────────────────────────────────────────────────┐ │   │
│  │  │                    Infrastructure Adapters                       │ │   │
│  │  │  ┌─────────┐  ┌─────────┐  ┌─────────┐  ┌─────────┐           │ │   │
│  │  │  │ yt-dlp  │  │  HTTP   │  │ FFmpeg  │  │  File   │           │ │   │
│  │  │  │ Adapter │  │ Client  │  │ Wrapper │  │ System  │           │ │   │
│  │  │  └─────────┘  └─────────┘  └─────────┘  └─────────┘           │ │   │
│  │  └─────────────────────────────────────────────────────────────────┘ │   │
│  │                                                                     │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

---

## 5. Data Flow

### 5.1 Download Flow

```
┌─────────┐     ┌─────────────┐     ┌──────────────┐     ┌──────────┐
│   UI    │     │ DownloadMgr │     │   Worker     │     │ External │
└────┬────┘     └──────┬──────┘     └───────┬──────┘     └────┬─────┘
     │                 │                    │                 │
     │ 1. Submit URL   │                    │                 │
     │────────────────▶│                    │                 │
     │                 │                    │                 │
     │                 │ 2. Parse & Validate│                 │
     │                 │────────────────────│                 │
     │                 │                    │                 │
     │                 │ 3. Create Job      │                 │
     │                 │────────────────────▶│                 │
     │                 │                    │                 │
     │ 4. Queued       │                    │                 │
     │◀────────────────│                    │                 │
     │                 │                    │                 │
     │                 │ 5. Start Download  │                 │
     │                 │◀───────────────────│                 │
     │                 │                    │                 │
     │                 │                    │ 6. Fetch Info   │
     │                 │                    │────────────────▶│
     │                 │                    │                 │
     │                 │                    │ 7. Stream Data  │
     │                 │                    │◀────────────────│
     │                 │                    │                 │
     │ 8. Progress     │                    │                 │
     │◀────────────────────────────────────│                 │
     │                 │                    │                 │
     │                 │                    │ 9. Complete     │
     │                 │                    │────────────────▶│
     │                 │                    │                 │
     │ 10. Finished    │                    │                 │
     │◀────────────────────────────────────│                 │
     │                 │                    │                 │
```

### 5.2 Media Library Update Flow

```
┌─────────┐     ┌─────────────┐     ┌──────────────┐     ┌──────────┐
│ Download│     │   Media     │     │   Index      │     │   UI     │
│ Complete│     │   Service   │     │   Queue      │     │          │
└────┬────┘     └──────┬──────┘     └───────┬──────┘     └────┬─────┘
     │                 │                    │                 │
     │ 1. Event        │                    │                 │
     │────────────────▶│                    │                 │
     │                 │                    │                 │
     │                 │ 2. Create Record   │                 │
     │                 │────────────────────▶│                 │
     │                 │                    │                 │
     │                 │ 3. Queue Indexing │                 │
     │                 │────────────────────▶│                 │
     │                 │                    │                 │
     │                 │                    │ 4. Process      │
     │                 │                    │ (Background)    │
     │                 │                    │                 │
     │                 │                    │ 5. Thumbnail    │
     │                 │                    │ 6. Metadata     │
     │                 │                    │ 7. Search Index │
     │                 │                    │                 │
     │                 │ 8. Update Complete │                 │
     │                 │◀───────────────────│                 │
     │                 │                    │                 │
     │                 │ 9. Notify UI       │                 │
     │                 │────────────────────│                 │
     │                 │                    │                 │
     │                 │                    │ 10. Refresh     │
     │                 │                    │────────────────▶│
     │                 │                    │                 │
```

### 5.3 Automation Execution Flow

```
┌─────────┐     ┌─────────────┐     ┌──────────────┐     ┌──────────┐
│ Trigger │     │   Rule      │     │   Worker     │     │  Action  │
│  Event  │     │   Engine     │     │   Pool       │     │  Exec    │
└────┬────┘     └──────┬──────┘     └───────┬──────┘     └────┬─────┘
     │                 │                    │                 │
     │ 1. Event Fired  │                    │                 │
     │────────────────▶│                    │                 │
     │                 │                    │                 │
     │                 │ 2. Match Rules       │                 │
     │                 │ (Evaluate conditions)                │
     │                 │                    │                 │
     │                 │ 3. Create Job        │                 │
     │                 │────────────────────▶│                 │
     │                 │                    │                 │
     │                 │                    │ 4. Execute      │
     │                 │                    │                 │
     │                 │                    │ 5. Run Actions  │
     │                 │                    │────────────────▶│
     │                 │                    │                 │
     │                 │                    │ 6. Results      │
     │                 │                    │◀────────────────│
     │                 │                    │                 │
     │                 │ 7. Complete         │                 │
     │                 │◀───────────────────│                 │
     │                 │                    │                 │
     │ 8. Log Result   │                    │                 │
     │◀────────────────│                    │                 │
     │                 │                    │                 │
```

---

## 6. Module Structure

### 6.1 Module Interface Definition

```rust
// Rust trait defining module contract
pub trait Module: Send + Sync {
    fn name(&self) -> &str;
    fn version(&self) -> &str;
    fn dependencies(&self) -> Vec<&str>;
    
    async fn initialize(&mut self, ctx: &Context) -> Result<(), Error>;
    async fn shutdown(&mut self) -> Result<(), Error>;
    
    fn register_handlers(&self, registry: &mut HandlerRegistry);
    fn register_apis(&self, router: &mut ApiRouter);
}

// Module manifest
pub struct ModuleManifest {
    pub name: String,
    pub version: String,
    pub description: String,
    pub author: String,
    pub dependencies: Vec<String>,
    pub exports: Vec<Export>,
    pub permissions: Vec<Permission>,
}
```

### 6.2 Module Communication

```rust
// Event bus for inter-module communication
pub struct EventBus {
    subscribers: RwLock<HashMap<String, Vec<Box<dyn EventHandler>>>>,
}

impl EventBus {
    pub async fn publish(&self, event: Event) -> Result<(), Error> {
        let subscribers = self.subscribers.read().await;
        if let Some(handlers) = subscribers.get(&event.topic) {
            for handler in handlers {
                handler.handle(event.clone()).await?;
            }
        }
        Ok(())
    }
    
    pub async fn subscribe(
        &self, 
        topic: &str, 
        handler: Box<dyn EventHandler>
    ) -> Subscription {
        let mut subscribers = self.subscribers.write().await;
        subscribers.entry(topic.to_string())
            .or_insert_with(Vec::new)
            .push(handler);
        Subscription::new(topic)
    }
}

// Request/Response for synchronous calls
pub struct ModuleClient {
    router: Arc<RwLock<HashMap<String, Box<dyn Handler>>>>,
}

impl ModuleClient {
    pub async fn call<R: Request>(
        &self, 
        module: &str, 
        method: &str, 
        request: R
    ) -> Result<R::Response, Error> {
        let router = self.router.read().await;
        let key = format!("{}:{}", module, method);
        let handler = router.get(&key)
            .ok_or(Error::HandlerNotFound)?;
        handler.handle(request).await
    }
}
```

---

## 7. Technology Integration

### 7.1 Tauri Integration

```
┌─────────────────────────────────────────────────────────────┐
│                    Tauri Architecture                        │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │                    Frontend (React)                      │ │
│  │  ┌─────────┐  ┌─────────┐  ┌─────────┐  ┌─────────┐  │ │
│  │  │  UI     │  │  State  │  │  API    │  │ Events  │  │ │
│  │  │Components│  │Zustand  │  │Client   │  │Handlers │  │ │
│  │  └────┬────┘  └────┬────┘  └────┬────┘  └────┬────┘  │ │
│  │       │            │            │            │        │ │
│  │       └────────────┴────────────┴────────────┘        │ │
│  │                      │                                   │ │
│  │                      ▼                                   │ │
│  │              ┌─────────────┐                            │ │
│  │              │  Tauri API  │                            │ │
│  │              │  (invoke)   │                            │ │
│  │              └──────┬──────┘                            │ │
│  └─────────────────────┼────────────────────────────────────┘ │
│                        │                                     │
│                        │ IPC                                  │
│                        │                                     │
│  ┌─────────────────────┼────────────────────────────────────┐ │
│  │                     ▼                                    │ │
│  │              ┌─────────────┐                            │ │
│  │              │   Command   │                            │ │
│  │              │   Router    │                            │ │
│  │              └──────┬──────┘                            │ │
│  │                     │                                    │ │
│  │       ┌─────────────┼─────────────┐                     │ │
│  │       │             │             │                     │ │
│  │       ▼             ▼             ▼                     │ │
│  │  ┌─────────┐  ┌─────────┐  ┌─────────┐                │ │
│  │  │Download │  │  Media  │  │Automation│               │ │
│  │  │Commands │  │Commands │  │Commands  │                │ │
│  │  └─────────┘  └─────────┘  └─────────┘                │ │
│  │                                                         │ │
│  │  ┌─────────────────────────────────────────────────────┐ │ │
│  │  │              Core Services (Rust)                   │ │ │
│  │  │  • Event Bus  • Config  • Security  • Storage        │ │ │
│  │  └─────────────────────────────────────────────────────┘ │ │
│  │                                                         │ │
│  └─────────────────────────────────────────────────────────┘ │
│                                                              │
└─────────────────────────────────────────────────────────────┘
```

### 7.2 FFmpeg Integration

```rust
pub struct FFmpegProcessor {
    binary_path: PathBuf,
    temp_dir: PathBuf,
}

impl FFmpegProcessor {
    pub async fn convert(
        &self,
        input: &Path,
        options: ConversionOptions,
    ) -> Result<PathBuf, Error> {
        let output = self.temp_dir.join(format!(
            "{}.{}",
            Uuid::new_v4(),
            options.format.extension()
        ));
        
        let mut cmd = Command::new(&self.binary_path);
        cmd.arg("-i").arg(input)
            .arg("-y"); // Overwrite output
        
        // Video options
        if let Some(codec) = options.video_codec {
            cmd.arg("-c:v").arg(codec);
        }
        if let Some(quality) = options.video_quality {
            cmd.arg("-crf").arg(quality.to_string());
        }
        
        // Audio options
        if options.audio_only {
            cmd.arg("-vn"); // No video
        } else if let Some(codec) = options.audio_codec {
            cmd.arg("-c:a").arg(codec);
        }
        
        // Metadata
        if options.preserve_metadata {
            cmd.arg("-map_metadata").arg("0");
        }
        
        cmd.arg(&output);
        
        let output = cmd.output().await?;
        
        if !output.status.success() {
            return Err(Error::FFmpegError(
                String::from_utf8_lossy(&output.stderr).to_string()
            ));
        }
        
        Ok(output)
    }
    
    pub async fn extract_thumbnail(
        &self,
        input: &Path,
        timestamp: Duration,
        size: (u32, u32),
    ) -> Result<Vec<u8>, Error> {
        let mut cmd = Command::new(&self.binary_path);
        
        cmd.arg("-ss").arg(timestamp.as_secs().to_string())
            .arg("-i").arg(input)
            .arg("-vframes").arg("1")
            .arg("-s").arg(format!("{}x{}", size.0, size.1))
            .arg("-f").arg("image2")
            .arg("-c:v").arg("mjpeg")
            .arg("pipe:1");
        
        let output = cmd.output().await?;
        
        if !output.status.success() {
            return Err(Error::ThumbnailError);
        }
        
        Ok(output.stdout)
    }
}
```

---

## 8. Deployment Architecture

### 8.1 Desktop Deployment

```
┌─────────────────────────────────────────────────────────────┐
│                    Build Pipeline                            │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  ┌─────────┐    ┌─────────┐    ┌─────────┐    ┌─────────┐ │
│  │  Source │───▶│  Build  │───▶│ Package │───▶│ Release │ │
│  │  Code   │    │  (CI)   │    │  (CD)   │    │  (CD)   │ │
│  └─────────┘    └────┬────┘    └────┬────┘    └────┬────┘ │
│                      │              │              │       │
│                      ▼              ▼              ▼       │
│              ┌─────────────┐  ┌─────────────┐  ┌─────────┐ │
│              │ • Rust      │  │ • Windows   │  │ • GitHub│ │
│              │   Compile   │  │   (.msi)    │  │   Releases│ │
│              │ • React     │  │ • macOS     │  │ • Auto  │ │
│              │   Build     │  │   (.dmg)    │  │   Update│ │
│              │ • Tauri     │  │ • Linux     │  │ • CDN   │ │
│              │   Bundle    │  │   (.AppImage│  │         │ │
│              │             │  │    .deb)    │  │         │ │
│              └─────────────┘  └─────────────┘  └─────────┘ │
│                                                              │
└─────────────────────────────────────────────────────────────┘
```

### 8.2 Update Architecture

```
┌─────────────────────────────────────────────────────────────┐
│                    Auto-Update Flow                          │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  ┌─────────────┐                              ┌───────────┐ │
│  │   Client    │                              │  Server   │ │
│  │             │                              │           │ │
│  │ 1. Check    │─────GET /version ───────────▶│           │ │
│  │             │                              │           │ │
│  │             │◀────{version, url, sig}──────│           │ │
│  │             │                              │           │ │
│  │ 2. Compare  │                              │           │ │
│  │             │                              │           │ │
│  │ 3. Download │─────GET /download/{version} ─▶│           │ │
│  │             │                              │           │ │
│  │             │◀────Binary + Signature────────│           │ │
│  │             │                              │           │ │
│  │ 4. Verify   │                              │           │ │
│  │    (Ed25519)│                              │           │ │
│  │             │                              │           │ │
│  │ 5. Install  │                              │           │ │
│  │    (Atomic) │                              │           │ │
│  │             │                              │           │ │
│  │ 6. Restart  │                              │           │ │
│  │             │                              │           │ │
│  └─────────────┘                              └───────────┘ │
│                                                              │
└─────────────────────────────────────────────────────────────┘
```

---

## 9. Security Architecture

### 9.1 Security Layers

```
┌─────────────────────────────────────────────────────────────┐
│                    Security Architecture                       │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  Layer 1: Application Security                               │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │  • Input validation    • URL sanitization               │ │
│  │  • Path traversal prevention  • Content-type verification │ │
│  └─────────────────────────────────────────────────────────┘ │
│                              │                               │
│  Layer 2: Process Security                                     │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │  • Sandboxed downloads  • Resource limits               │ │
│  │  • Network isolation    • File system restrictions      │ │
│  └─────────────────────────────────────────────────────────┘ │
│                              │                               │
│  Layer 3: Data Security                                        │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │  • Credential encryption  • Secure storage                │ │
│  │  • Database encryption    • Memory protection           │ │
│  └─────────────────────────────────────────────────────────┘ │
│                              │                               │
│  Layer 4: Network Security                                     │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │  • TLS 1.3              • Certificate pinning           │ │
│  │  • DNS-over-HTTPS       • Proxy support                 │ │
│  └─────────────────────────────────────────────────────────┘ │
│                              │                               │
│  Layer 5: Update Security                                      │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │  • Code signing         • Signature verification        │ │
│  │  • Reproducible builds  • Rollback capability           │ │
│  └─────────────────────────────────────────────────────────┘ │
│                                                              │
└─────────────────────────────────────────────────────────────┘
```

---

*Document Version: 1.0*
*Last Updated: 2026-05-27*
*Owner: Engineering Team*