# miniSISKA — AI Marketing & Social Content System

> AI-powered multi-platform content engine for Malaysian businesses.
> Built on XAMPP · PHP 8 · MySQL · OpenAI API · Meta Graph API

---

## What Is miniSISKA?

miniSISKA is a modular AI content operating system purpose-built for **ANS LEGACY** (a Malaysian titanium jewellery brand). It generates, schedules, and publishes social media content across multiple platforms using OpenAI GPT-4o — with a full web-based UI, media uploads, content history, and social account management.

---

## Modules

| Module | Location | Status |
|--------|----------|--------|
| AI Content Generator | `api/openai.php` + `public/index.php` | ✅ Active |
| Structured Content Output | `api/openai.php` | ✅ Active |
| Threads Content Engine (9 modes) | `api/openai.php` | ✅ Active |
| Media Upload (image/video + ffmpeg) | `api/upload.php` | ✅ Active |
| AI Media Analysis (Vision) | `api/media_analysis.php` | ✅ Active |
| Content History & Actions | `api/history.php` + `api/action.php` | ✅ Active |
| Social Account Manager | `api/social_accounts.php` + `public/social_accounts.php` | ✅ Active |
| Threads Posting | `social/threads.php` | ✅ Active |
| Facebook/Instagram Posting | `social/meta.php` | ✅ Active |
| Telegram Posting | `social/telegram.php` | ✅ Active |
| TikTok Module | `social/tiktok.php` | ⚙️ Placeholder |
| Post Scheduler (cron) | `cron/` | ✅ Active |
| Token Auto-Renewal (Threads) | `cron/refresh_tokens.php` | ✅ Active |
| Analytics Dashboard | `public/dashboard.php` | ✅ Active |
| Developer Tools | `public/dashboard.php` | ✅ Active |
| PWA / Mobile Support | `public/manifest.json` + `public/sw.js` | ✅ Active |

---

## Project Structure

```
mini-siska/
├── api/                   # PHP API endpoints (called by JS fetch)
│   ├── openai.php         # Core AI generation engine
│   ├── upload.php         # Media upload + ffmpeg auto-conversion
│   ├── media_analysis.php # Vision AI (image/video analysis)
│   ├── save.php           # Save content to DB
│   ├── history.php        # Content history list
│   ├── action.php         # Post/schedule/approve/delete actions
│   ├── post_now.php       # Immediate post trigger
│   ├── social_accounts.php# Account CRUD + token management
│   └── dev_tools.php      # Developer health check endpoint
│
├── config/                # Internal config — blocked from web access
│   ├── env.php            # .env parser (loaded by all scripts)
│   └── database.php       # MySQLi connection
│
├── cron/                  # Scheduled jobs (Windows Task Scheduler)
│   ├── post_scheduler.php # Main posting cron
│   ├── retry_failed.php   # Retry failed posts
│   ├── refresh_tokens.php # Threads token auto-renewal
│   └── *.bat              # Windows Task Scheduler launchers
│
├── database/              # DB schema + migrations
│   ├── schema.sql         # Full table definition
│   └── migrate_*.sql      # Incremental migration scripts
│
├── docs/                  # Developer documentation
│   └── git-workflow.md    # Git backup & rollback guide
│
├── backups/               # Local backup folder (never committed to GitHub)
│   ├── database/          # DB exports (.sql files) — gitignored
│   └── media/             # Media file backups — gitignored
│
├── logs/                  # Runtime logs (never committed)
│
├── prompts/               # AI system + platform prompts (.txt)
│   ├── system_prompt.txt  # Brand voice & persona
│   ├── fb_prompt.txt      # Facebook rules
│   ├── threads_prompt.txt # Threads engine rules
│   ├── tiktok_prompt.txt  # TikTok script rules
│   └── telegram_prompt.txt# Telegram promo rules
│
├── public/                # Web-accessible pages
│   ├── index.php          # Main app (generator + history)
│   ├── dashboard.php      # Analytics + developer tools
│   ├── social_accounts.php# Account manager UI
│   └── media.php          # Media library
│
├── social/                # Platform posting logic
│   ├── meta.php           # Facebook + Instagram Graph API
│   ├── threads.php        # Threads Graph API + token refresh
│   ├── telegram.php       # Telegram Bot API
│   └── tiktok.php         # TikTok (placeholder)
│
├── storage/uploads/       # User-uploaded media (never committed)
│
├── .env                   # NEVER commit — secrets here
├── .env.example           # Safe template (commit this)
├── .gitignore
├── .htaccess              # Blocks .env, config/, logs/, storage/
├── git-backup.bat         # Quick backup script (Windows)
├── git-backup.ps1         # Quick backup script (PowerShell)
├── VERSION.md             # Current version info
└── CHANGELOG.md           # Full change history
```

---

## Quick Start (Local XAMPP)

### 1. Clone to XAMPP htdocs
```
C:\xampp\htdocs\mini-siska\
```

### 2. Create `.env`
Copy `.env.example` and fill in your credentials:
```
OPENAI_API_KEY=sk-your-key-here
OPENAI_MODEL=gpt-4o
DB_HOST=localhost
DB_NAME=mini_siska
DB_USER=root
DB_PASS=
WHATSAPP_NUMBER=601XXXXXXXX
CRON_SECRET=your_secret_here
MAX_UPLOAD_MB=50
```

### 3. Create Database
```sql
CREATE DATABASE mini_siska CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```
Then import `database/schema.sql` via phpMyAdmin.

### 4. Access the App
- Generator: `http://localhost/mini-siska/public/`
- Dashboard: `http://localhost/mini-siska/public/dashboard.php`
- Accounts:  `http://localhost/mini-siska/public/social_accounts.php`

---

## ffmpeg (Video Auto-Conversion)

Install ffmpeg via winget for automatic MOV/AVI → MP4 conversion:
```
winget install --id Gyan.FFmpeg
```

---

## Cron Jobs (Windows Task Scheduler)

| Task | Schedule | Script |
|------|----------|--------|
| Post Scheduler | Every 5 min | `cron/run_scheduler.bat` |
| Retry Failed | Every 15 min | `cron/run_retry.bat` |
| Token Renewal | Weekly Sunday 3am | `cron/run_refresh_tokens.bat` |

---

## Tech Stack

- **Backend**: PHP 8.x, MySQLi, cURL
- **AI**: OpenAI GPT-4o (chat + vision)
- **Social APIs**: Meta Graph API, Threads Graph API, Telegram Bot API
- **Media**: ffmpeg (video conversion), PHP GD (image resize)
- **Frontend**: Vanilla JS, CSS custom properties, dark theme
- **Server**: XAMPP (Apache + MySQL + PHP)

---

## Security

- `.env` is excluded from Git and blocked by Apache `.htaccess`
- `config/` and `logs/` are blocked from web access
- `storage/uploads/.htaccess` blocks PHP execution in uploads folder
- All file uploads validated by MIME type + extension whitelist
- SQL queries use prepared statements throughout

---

## Git Workflow

See [`docs/git-workflow.md`](docs/git-workflow.md) for the full backup, rollback, and safe deployment guide.

Quick backup after any successful update:
```
git-backup.bat
```

---

## License

Private / Commercial — ANS LEGACY internal system.
