# MTGJSON Data Manager - File Type Support

## Supported File Types

### 1. AllPrintings (Primary Card Database)
- **Accepts:** `AllPrintings.json` OR `AllPrintings.psql`
- **Processing:**
  - `.json`: Parses JSON structure, extracts card data from `cards` array
  - `.psql`: Parses SQL INSERT statements to extract card data
- **Database:** `mtg_cards` table
- **Required:** Yes (one of the two formats)

### 2. AllIdentifiers (Stable Card Referencing)
- **Accepts:** `AllIdentifiers.json`
- **Processing:** Parses JSON structure, extracts identifiers
- **Database:** `mtg_identifiers` table
- **Required:** Yes

### 3. Keywords & CardTypes (Game Logic/Mechanics)
- **Accepts:** `Keywords.json` and `CardTypes.json`
- **Processing:** Parses JSON arrays
- **Database:** `mtg_keywords` and `mtg_card_types` tables
- **Required:** Yes (both)

### 4. AllDeckFiles (Deck Format Testing)
- **Accepts:** `AllDeckFiles.zip`
- **Processing:**
  1. Unzips the archive
  2. Finds all `.json` files recursively
  3. Parses each JSON file
  4. Upserts deck data
- **Database:** `mtg_deck_list` table
- **Required:** Yes

## File Validation

The health check endpoint now validates that all required files are present:

```json
{
  "status": "healthy",
  "data": {
    "required_files": {
      "all_present": true,
      "found": ["AllPrintings.json", "AllIdentifiers.json", "Keywords.json", "CardTypes.json", "AllDeckFiles.zip"],
      "missing": [],
      "details": {
        "AllPrintings": "OK (.json)",
        "AllIdentifiers": "OK",
        "Keywords": "OK",
        "CardTypes": "OK",
        "AllDeckFiles": "OK"
      }
    }
  }
}
```

## PSQL File Parsing

For `AllPrintings.psql`, the manager:
1. Reads the SQL file
2. Extracts `INSERT INTO mtg_cards (...) VALUES (...)` statements using regex
3. Parses column names and values
4. Handles NULL values, quoted strings, and JSON arrays
5. Converts parsed data into card objects for upsert

## Deck File ZIP Processing

For `AllDeckFiles.zip`:
1. Extracts to temporary directory
2. Recursively finds all `.json` files
3. Parses each file expecting deck format:
   ```json
   {
     "listId": "...",
     "name": "...",
     "year": "...",
     "date": "...",
     "format": "..."
   }
   ```
4. Upserts each deck into the database
5. Cleans up temporary files

## Error Handling

- Validates file existence before processing
- Logs warnings for missing files
- Handles malformed JSON/PSQL gracefully
- Rolls back transactions on individual card failures
- Continues processing remaining files on errors

## Health Check

The app is considered "healthy" when:
1. Cards and sets are loaded in database (>0)
2. All required files are present in the mounted volume
3. No critical processing errors occurred

If any required file is missing, the health status shows "unhealthy" with details about what's missing.
