# Data Directory

This directory contains the SQLite databases used by the Watch Finished system.

## Database Schema Overview

```mermaid
erDiagram
    FILES ||--o{ TASKS : "processed-by"
    SETTINGS ||--o{ DATASETS : "configures"

    FILES {
        string dataset PK
        string input PK
        string output
        string status
        string date
    }

    TASKS {
        integer id PK
        string dataset
        string input
        string output
        string preset
        string status
        integer progress
        string created_at
        string updated_at
    }

    SETTINGS {
        string key PK
        string value
    }

    DATASETS {
        string name PK
        boolean enabled
        string destination
        string exts
        string ext
        string preset
        string clean
    }
```

## Files

- `database.db` - Main application database containing:
  - `files` table: Processed video files with metadata
  - `tasks` table: Video processing queue and task status
  - `settings` table: Application configuration and dataset settings

- `database.db.bak` - Backup of the main database (created during migrations)

## Database Schema

### Files Table

```sql
CREATE TABLE files (
  dataset TEXT,      -- Dataset name (e.g., 'movies', 'tvshows')
  input TEXT,        -- Original file path
  output TEXT,       -- Processed file path
  status TEXT,       -- Processing status ('pending', 'processing', 'success', 'failed')
  date TEXT,         -- ISO timestamp of last update
  PRIMARY KEY (dataset, input)
);
```

### Tasks Table

```sql
CREATE TABLE tasks (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  dataset TEXT,      -- Target dataset
  input TEXT,        -- Input file path
  output TEXT,       -- Output file path
  preset TEXT,       -- HandBrake preset used
  status TEXT,       -- Task status
  progress INTEGER,  -- Processing progress (0-100)
  created_at TEXT,   -- Creation timestamp
  updated_at TEXT    -- Last update timestamp
);
```

### Settings Table

```sql
CREATE TABLE settings (
  key TEXT PRIMARY KEY,
  value TEXT         -- JSON-encoded setting value
);
```

## Dataset Configuration

Dataset settings are stored in the settings table with keys like:

- `datasets/kids` - Kids movies dataset configuration
- `datasets/pr0n` - Adult content dataset configuration
- `datasets/tvshows` - TV shows dataset configuration

Each dataset configuration includes:

- `enabled`: Whether the dataset is active for watching
- `destination`: Output directory for processed files
- `exts`: File extensions to process
- `ext`: Output file extension
- `preset`: HandBrake encoding preset
- `clean`: Filename cleaning rules

## Backup and Recovery

- Always backup `database.db` before major changes
- The system creates `database.db.bak` during migrations
- To restore from backup: `cp database.db.bak database.db`

## Migration

The system includes migration tools to convert from the legacy JSON-based storage to SQLite. See the service README for migration commands.
