#!/usr/bin/env bash
set -e

DOC_DIR="./docs"
mkdir -p "$DOC_DIR"

echo "Installing Life Archive documentation into $DOC_DIR"
echo

###############################################
# Helper function
###############################################
write() {
  FILE="$DOC_DIR/$1"
  echo "Creating $FILE"
  cat > "$FILE" << 'EOF'
$2
EOF
}

###############################################
# project.ideas.md
###############################################
cat > "$DOC_DIR/project.ideas.md" << 'EOF'
# Project Ideas – Life Archive Application
Status: Complete

## Core Purpose
A unified Life Archive system that captures, organizes, and displays a person’s life events — with photos, documents, and media — across categories, timeframes, and locations. Forms are dynamically generated from JSON definitions.

## Primary Goals
- Centralized life event repository  
- Dynamic JSON-defined forms  
- Multi-mode viewing (timeline, slideshow)  
- Smart filtering  
- Attachment management  
- Clean modular tech stack  

## Mission Statement
To preserve and present the story of a person’s life through structured events, rich media, and intuitive navigation.
EOF

###############################################
# architecture.md
###############################################
cat > "$DOC_DIR/architecture.md" << 'EOF'
# Architecture – Life Archive Application
Status: Complete

## Layers
1. Application Layer (PHP, AdminLTE, Bootstrap)
2. JSON Definition Layer
3. Database Layer (MySQL/MariaDB)
4. Media Storage Layer
5. Metadata & Caching Layer

## Core Concept
JSON defines forms → DB stores metadata → filesystem stores media → UI renders timeline/slideshow.
EOF

###############################################
# data-model.md
###############################################
cat > "$DOC_DIR/data-model.md" << 'EOF'
# Data Model – Life Archive Application
Status: Complete

## Tables
- persons  
- family_tree  
- categories  
- subcategories  
- life_events  
- life_event_attachments  
- locations  
- timeframes  

## Indexing
- person_id  
- category_id  
- event_date  
EOF

###############################################
# json.schema.md
###############################################
cat > "$DOC_DIR/json.schema.md" << 'EOF'
# JSON Schema – Life Archive Application
Status: Incomplete (needs reviewer confirmation)

## Form Definition Schema
{
  "form_key": "string",
  "label": "string",
  "description": "string",
  "version": "1.0",
  "fields": []
}

## Field Schema
{
  "name": "string",
  "label": "string",
  "type": "text|textarea|number|date|select|attachments|repeatable|conditional",
  "required": true,
  "validation": "string",
  "help": "string",
  "icon": "fa-icon",
  "order": 1,
  "multiple": false,
  "allowed_types": ["jpeg","png","gif","pdf","txt"],
  "conditional": {
    "field": "string",
    "value": "string",
    "show": true
  },
  "repeatable": {
    "min": 0,
    "max": 10
  }
}

## Outstanding Questions
- Should repeatable groups support nested repeatables?  
- Should conditional fields support multiple conditions?  
- Should select fields load options dynamically from DB?  
EOF

###############################################
# ui.spec.md
###############################################
cat > "$DOC_DIR/ui.spec.md" << 'EOF'
# UI Specification – Life Archive Application
Status: Incomplete (needs wireframes)

## User Flow
1. Select person  
2. Choose category/timeframe/location  
3. Choose view mode  
4. View event details  

## Timeline View
- Exact chronological order  
- Event cards  
- Thumbnails  

## Slideshow View
- Auto/manual  
- PDF → first page  
- TXT → text page  
- Error message if conversion fails  

## Outstanding Questions
- Should timeline support zooming?  
- Should slideshow support keyboard navigation?  
EOF

###############################################
# admin.spec.md
###############################################
cat > "$DOC_DIR/admin.spec.md" << 'EOF'
# Admin Specification – Life Archive Application
Status: Incomplete (needs workflow diagrams)

## Modules
- Settings Manager (.env)
- Category Manager
- JSON Form Editor
- Person Manager
- Event Manager

## Outstanding Questions
- Should admin clone form definitions?  
- Should admin export/import JSON?  
- Should admin bulk edit categories?  
EOF

###############################################
# storage.rules.md
###############################################
cat > "$DOC_DIR/storage.rules.md" << 'EOF'
# Storage Rules – Life Archive Application
Status: Incomplete (needs reviewer confirmation)

## Structure
/storage/life-events/{person_id}/{event_id}/

## Thumbnails
- Auto-generated  
- PDF → first page  
- TXT → text preview  

## Conversion Rules
If conversion fails:
"Unable to convert {source} to {target}"

## Outstanding Questions
- Should thumbnails regenerate on cron?  
- Should deleted files be soft-deleted?  
EOF

###############################################
# security.model.md
###############################################
cat > "$DOC_DIR/security.model.md" << 'EOF'
# Security Model – Life Archive Application
Status: Incomplete (needs access matrix)

## Roles
- Admin  
- Editor  
- Viewer  
- Guest  

## Privacy Flags
private: true|false

## Outstanding Questions
- Should private events be hidden or blurred?  
- Should admin override be logged separately?  
EOF

###############################################
# api.endpoints.md
###############################################
cat > "$DOC_DIR/api.endpoints.md" << 'EOF'
# API Endpoints – Life Archive Application
Status: Incomplete (needs pagination rules)

## Persons
GET /api/persons  
GET /api/persons/{id}  
POST /api/persons  

## Events
GET /api/events  
GET /api/events/{id}  
POST /api/events  

## Attachments
GET /api/events/{id}/attachments  
POST /api/events/{id}/attachments  

## Outstanding Questions
- Should API support GraphQL?  
- Should API support bulk upload?  
EOF

###############################################
# md.status.md
###############################################
cat > "$DOC_DIR/md.status.md" << 'EOF'
# MD Status Dashboard – Life Archive Application

| File | Status | Notes |
|------|--------|-------|
| project.ideas.md | Complete | Core goals captured |
| architecture.md | Complete | System layers defined |
| data-model.md | Complete | DB schema complete |
| json.schema.md | Incomplete | Needs JSON schema confirmation |
| ui.spec.md | Incomplete | Needs wireframes |
| admin.spec.md | Incomplete | Needs workflows |
| storage.rules.md | Incomplete | Needs conversion rules |
| security.model.md | Incomplete | Needs access matrix |
| api.endpoints.md | Incomplete | Needs pagination rules |

EOF

echo
echo "All documentation files created successfully."

