# EasyStream Complete Database Setup

This directory contains the complete database schema for the EasyStream video streaming platform.

## 🗄️ Complete Database Schema

The `__install/easystream.sql` file contains **ALL** tables needed for a production-ready EasyStream installation:

### Core Tables
- **db_accountuser** - User accounts with token balances and streaming settings
- **db_videofiles** - Video files with processing status and analytics
- **db_settings** - Platform configuration settings
- **db_sessions** - User session management

### RBAC (Role-Based Access Control)
- **db_roles** - System roles (guest, member, verified, creator, moderator, admin, super_admin)
- **db_user_roles** - User role assignments with expiration
- **db_role_permissions** - Permission mappings for each role

### Live Streaming
- **db_live_streams** - Live stream sessions with SRS integration
- **db_stream_viewers** - Real-time viewer tracking
- Stream analytics and recording management

### API & Analytics
- **db_api_keys** - API key management with rate limiting
- **db_api_logs** - Complete API request logging
- **db_video_analytics** - Detailed video viewing analytics
- **db_platform_metrics** - Aggregated platform statistics

### Token Monetization System
- **token_transactions** - All token-based transactions
- **token_purchases** - USD to token conversions
- **token_donations** - User-to-user token transfers

### Branding & Customization
- **db_branding_settings** - Complete theme customization
- **db_branding_images** - Logo and image management
- Color schemes, typography, and layout settings

### Additional Features
- IP tracking and fingerprinting
- Email logging and notifications
- Password reset management
- Enhanced security features

## 🚀 Quick Setup

### For Docker Users (Recommended)

1. **Start your Docker containers:**
   ```bash
   docker-compose up -d
   ```

2. **Load the schema:**
   ```bash
   docker exec -i easystream-db mysql -u easystream -peasystream easystream < __install/easystream.sql
   ```

### Manual Setup

If you prefer to run the SQL manually:

```bash
# Copy SQL to container
docker cp __install/easystream.sql vs-db:/tmp/schema.sql

# Execute the schema
docker exec vs-db mysql -u root -proot easystream -e "source /tmp/schema.sql"

# Clean up
docker exec vs-db rm /tmp/schema.sql
```

## ✅ What Gets Created

After running the setup, your EasyStream platform will have:

- **Complete user management** with role-based permissions
- **Video processing pipeline** with HLS streaming support
- **Live streaming capabilities** integrated with SRS
- **Comprehensive analytics** for videos, streams, and platform metrics
- **Token-based monetization** system ready for payments
- **Full branding customization** with theme management
- **Production-ready API** with logging and rate limiting
- **Advanced security features** including IP tracking and fingerprinting

## 🔧 Default Configuration

The schema includes sensible defaults:

- **7 user roles** from guest to super_admin with appropriate permissions
- **Token system** configured with "EasyCoins" as the default currency
- **Branding settings** with a clean blue theme
- **Platform metrics** initialized and ready for data collection
- **API rate limits** set to 100/hour for public, 1000/hour for members

## 📊 Database Statistics

The complete schema creates approximately:
- **25+ core tables** for full functionality
- **50+ indexes** for optimal performance
- **100+ default settings** for immediate use
- **Foreign key constraints** for data integrity
- **Triggers** for automatic balance updates

## 🛠️ Maintenance

The database is designed for production use with:
- Proper indexing for performance
- Foreign key constraints for data integrity
- Timestamp tracking for audit trails
- JSON columns for flexible metadata storage
- Partitioning-ready for high-volume analytics

## 🔍 Verification

After setup, verify your installation by checking that key tables exist:

```sql
SHOW TABLES LIKE 'db_%';
SHOW TABLES LIKE 'token_%';
```

You should see all the tables listed above.

## 📝 Notes

- This schema is compatible with MariaDB 10.6+ and MySQL 8.0+
- All tables use UTF8MB4 charset for full Unicode support
- JSON columns are used for flexible metadata storage
- The schema is designed to handle high-volume production workloads

Your EasyStream platform is now ready for production use! 🎉


