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

BASE_DIR="./docs"

echo "Creating docs directory at: $BASE_DIR"
mkdir -p "$BASE_DIR"

#######################################
# architecture.md
#######################################
cat > "$BASE_DIR/architecture.md" << 'EOF'
# Life Archive – Architecture

## Purpose
A unified Life Archive system that captures, organizes, and displays a person’s life events with media, across categories, timeframes, and locations. Forms and event types are driven by JSON definitions.

## High-Level Goals
- Centralized life event repository
- Dynamic JSON-defined forms
- Multi-mode viewing (timeline, slideshow)
- Smart filtering by person, category, timeframe, location
- Clean separation of app logic, data, and media

## Layers

### 1. Application Layer
- PHP, AdminLTE, Bootstrap, JavaScript
- Renders UI, timelines, slideshows, galleries
- Reads JSON definitions and DB data

### 2. JSON Definition Layer
- Stored as files:
  - `json-definitions/forms/*.json`
  - `json-definitions/categories/*.json`
  - `json-definitions/subcategories/*.json`
- Defines:
  - Fields, labels, help text
  - Required flags, validation rules
  - Icons, order, attachment rules

### 3. Database Layer
- MySQL / MariaDB
- Tables:
  - `persons`
  - `family_tree`
  - `life_events`
  - `life_event_attachments`
  - `categories`
  - `subcategories`
  - `locations`
  - `timeframes`
- Stores structured metadata, not media files.

### 4. Media Storage Layer
- Filesystem or object storage
- Path pattern:
  - `/storage/life-events/{person_id}/{event_id}/`
- Contains:
  - Images (jpeg, png, gif)
  - Documents (pdf, txt)
  - Thumbnails

### 5. Metadata & Caching Layer
- Optional performance layer:
  - `/apps/life/cache/`
- Stores:
  - Timeline indexes
  - Slideshow sequences
  - Category caches
  - Search indexes

## Core Concept
- JSON defines forms and event types.
- DB stores event metadata and relationships.
- Filesystem stores media.
- UI renders different views (timeline, slideshow, gallery) over the same data.
EOF

#######################################
# data-model.md
#######################################
cat > "$BASE_DIR/data-model.md" << 'EOF'
# Life Archive – Data Model

## Core Tables

### persons
- `id` (PK)
- `first_name`
- `last_name`
- `full_name`
- `birth_date`
- `death_date` (nullable)
- `notes`

### family_tree
- `id` (PK)
- `person_id` (FK -> persons.id)
- `parent_id` (FK -> persons.id, nullable)
- `relationship_type` (e.g., parent, child, spouse)

### categories
- `id` (PK)
- `slug` (e.g., "military", "cars")
- `name`
- `description`

### subcategories
- `id` (PK)
- `category_id` (FK -> categories.id)
- `slug`
- `name`
- `description`

### locations
- `id` (PK)
- `name`
- `city`
- `state`
- `country`
- `latitude` (nullable)
- `longitude` (nullable)

### timeframes
- `id` (PK)
- `label` (e.g., "Childhood", "College", "Military Service")
- `start_date` (nullable)
- `end_date` (nullable)

### life_events
- `id` (PK)
- `person_id` (FK -> persons.id)
- `category_id` (FK -> categories.id)
- `subcategory_id` (FK -> subcategories.id, nullable)
- `location_id` (FK -> locations.id, nullable)
- `timeframe_id` (FK -> timeframes.id, nullable)
- `event_date` (date or datetime)
- `title`
- `description`
- `form_definition_key` (links to JSON form type)
- `created_at`
- `updated_at`

### life_event_attachments
- `id` (PK)
- `event_id` (FK -> life_events.id)
- `file_path`
- `file_type` (jpeg, png, gif, pdf, txt, etc.)
- `original_name`
- `size_bytes`
- `created_at`

## Indexing & Performance
- Index on `life_events.person_id`
- Index on `life_events.category_id`
- Index on `life_events.event_date`
- Optional full-text index on `life_events.title`, `life_events.description`
EOF

#######################################
# json-forms.md
#######################################
cat > "$BASE_DIR/json-forms.md" << 'EOF'
# Life Archive – JSON Form Definitions

## Location of JSON Files
- `json-definitions/forms/*.json`
- `json-definitions/categories/*.json`
- `json-definitions/subcategories/*.json`

## Field Definition Schema (Example)

```json
{
  "form_key": "military_service_event",
  "label": "Military Service Event",
  "description": "Record a military-related life event.",
  "fields": [
    {
      "name": "event_date",
      "label": "Event Date",
      "type": "date",
      "required": true,
      "validation": "date",
      "help": "The date this event occurred.",
      "icon": "fa-calendar",
      "order": 1
    },
    {
      "name": "title",
      "label": "Title",
      "type": "text",
      "required": true,
      "validation": "string",
      "help": "Short title for this event.",
      "icon": "fa-heading",
      "order": 2
    },
    {
      "name": "description",
      "label": "Description",
      "type": "textarea",
      "required": false,
      "validation": "string",
      "help": "Details about this event.",
      "icon": "fa-align-left",
      "order": 3
    },
    {
      "name": "attachments",
      "label": "Attachments",
      "type": "attachments",
      "required": false,
      "multiple": true,
      "allowed_types": ["jpeg", "png", "gif", "pdf", "txt"],
      "help": "Upload photos or documents related to this event.",
      "icon": "fa-paperclip",
      "order": 4
    }
  ]
}

