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

BASE="./"
DOCS="$BASE/docs"
JSON="$BASE/json-definitions/forms"

echo "Creating documentation and JSON directories..."
mkdir -p "$DOCS"
mkdir -p "$JSON"

write_md() {
  FILE="$DOCS/$1"
  echo "Writing $FILE"
  cat << 'EOF' > "$FILE"
EOF
}

write_json() {
  FILE="$JSON/$1"
  echo "Writing $FILE"
  cat << 'EOF' > "$FILE"
EOF
}

###############################################
# project.ideas.md
###############################################
cat > "$DOCS/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.

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

EOF

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

## Layers
1. Application Layer
2. JSON Definition Layer
3. Database Layer
4. Media Storage Layer
5. Caching Layer

EOF

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

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

EOF

###############################################
# json.schema.md
###############################################
cat > "$DOCS/json.schema.md" << 'EOF'
# JSON Schema – Life Archive Application
Status: Incomplete

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

Field Definition:
{
  "name": "",
  "label": "",
  "type": "",
  "required": true,
  "validation": "",
  "help": "",
  "icon": "",
  "order": 1,
  "multiple": false,
  "allowed_types": ["jpeg","png","gif","pdf","txt"]
}

EOF

###############################################
# ui.flow.md
###############################################
cat > "$DOCS/ui.flow.md" << 'EOF'
# UI Flow – Life Archive

Viewer Flow:
1. Select person
2. Filter by category/timeframe/location
3. Choose view mode
4. View event details

Admin Flow:
1. Open admin
2. Choose module
3. Configure system

EOF

###############################################
# ui.wireframes.md
###############################################
cat > "$DOCS/ui.wireframes.md" << 'EOF'
# UI Wireframes – Life Archive

Person Selection:
- Tree + search

Filter Screen:
- Category, timeframe, location

Timeline:
- Event cards

Slideshow:
- Full-width media

EOF

###############################################
# admin.wireframes.md
###############################################
cat > "$DOCS/admin.wireframes.md" << 'EOF'
# Admin Wireframes – Life Archive

Modules:
- Settings
- Categories
- JSON Forms
- Persons
- Events

EOF

###############################################
# component.library.md
###############################################
cat > "$DOCS/component.library.md" << 'EOF'
# Component Library – Life Archive

Viewer Components:
- PersonSelector
- FilterBar
- Timeline
- Slideshow

Admin Components:
- SettingsForm
- CategoryTable
- JsonFormEditor

EOF

###############################################
# timeline.behavior.md
###############################################
cat > "$DOCS/timeline.behavior.md" << 'EOF'
# Timeline Behavior – Life Archive

Ordering:
- Exact chronological

Interaction:
- Click → detail modal

EOF

###############################################
# slideshow.behavior.md
###############################################
cat > "$DOCS/slideshow.behavior.md" << 'EOF'
# Slideshow Behavior – Life Archive

Modes:
- Auto
- Manual

Media:
- Images full-screen
- PDF first page
- TXT text slide

EOF

###############################################
# md.status.md
###############################################
cat > "$DOCS/md.status.md" << 'EOF'
# MD Status Dashboard

project.ideas.md – Complete  
architecture.md – Complete  
data-model.md – Complete  
json.schema.md – Incomplete  
ui.flow.md – Complete  
ui.wireframes.md – Complete  
admin.wireframes.md – Complete  
component.library.md – Complete  
timeline.behavior.md – Complete  
slideshow.behavior.md – Complete  

EOF

###############################################
# docs.index.json
###############################################
cat > "$DOCS/docs.index.json" << 'EOF'
{
  "project.ideas.md": "complete",
  "architecture.md": "complete",
  "data-model.md": "complete",
  "json.schema.md": "incomplete",
  "ui.flow.md": "complete",
  "ui.wireframes.md": "complete",
  "admin.wireframes.md": "complete",
  "component.library.md": "complete",
  "timeline.behavior.md": "complete",
  "slideshow.behavior.md": "complete"
}
EOF

###############################################
# docs.index.php
###############################################
cat > "$DOCS/docs.index.php" << 'EOF'
<?php
$dir = __DIR__;
$files = glob("$dir/*.md");
?>
<!DOCTYPE html>
<html>
<head>
<title>Life Docs</title>
</head>
<body>
<h1>Documentation</h1>
<ul>
<?php foreach ($files as $f): ?>
<li><a href="?file=<?=basename($f)?>"><?=basename($f)?></a></li>
<?php endforeach; ?>
</ul>
<?php
if (isset($_GET['file'])) {
    $file = $dir . "/" . basename($_GET['file']);
    if (file_exists($file)) {
        echo "<pre>" . htmlspecialchars(file_get_contents($file)) . "</pre>";
    }
}
?>
</body>
</html>
EOF

###############################################
# JSON forms (homes, cars, education)
###############################################

cat > "$JSON/homes.json" << 'EOF'
{
  "form_key": "homes_event",
  "label": "Home Event",
  "description": "Record a home-related life event.",
  "version": "1.0",
  "fields": [
    { "name": "event_date", "label": "Event Date", "type": "date", "required": true },
    { "name": "title", "label": "Title", "type": "text", "required": true },
    { "name": "address", "label": "Address", "type": "text" },
    { "name": "city", "label": "City", "type": "text" },
    { "name": "description", "label": "Description", "type": "textarea" },
    { "name": "attachments", "label": "Attachments", "type": "attachments", "multiple": true }
  ]
}
EOF

cat > "$JSON/cars.json" << 'EOF'
{
  "form_key": "cars_event",
  "label": "Car Event",
  "description": "Record a car-related life event.",
  "version": "1.0",
  "fields": [
    { "name": "event_date", "label": "Event Date", "type": "date", "required": true },
    { "name": "title", "label": "Title", "type": "text", "required": true },
    { "name": "make", "label": "Make", "type": "text" },
    { "name": "model", "label": "Model", "type": "text" },
    { "name": "year", "label": "Year", "type": "number" },
    { "name": "description", "label": "Description", "type": "textarea" },
    { "name": "attachments", "label": "Attachments", "type": "attachments", "multiple": true }
  ]
}
EOF

cat > "$JSON/education.json" << 'EOF'
{
  "form_key": "education_event",
  "label": "Education Event",
  "description": "Record an education milestone.",
  "version": "1.0",
  "fields": [
    { "name": "event_date", "label": "Event Date", "type": "date", "required": true },
    { "name": "title", "label": "Title", "type": "text", "required": true },
    { "name": "institution", "label": "Institution", "type": "text" },
    { "name": "degree", "label": "Degree", "type": "text" },
    { "name": "description", "label": "Description", "type": "textarea" },
    { "name": "attachments", "label": "Attachments", "type": "attachments", "multiple": true }
  ]
}
EOF

echo "All documentation and JSON files installed successfully."

