# BookDash - AI Book Creation Platform

An innovative platform that enables users to create books and stories by purchasing chapters and adding content through typing, email, or phone calls. All content is automatically synchronized into structured book chapters.

## Features

### Core Functionality
- **Multi-Method Content Creation**
  - Rich text editor with auto-save
  - Email-to-chapter conversion
  - Phone call recording with voice-to-text transcription
  
- **Credit System**
  - Purchase credits to create chapters (1 credit = 1 chapter)
  - Secure payment processing
  - Transaction history and invoicing

- **Book Management**
  - Create and organize multiple books
  - Chapter ordering and reordering
  - Progress tracking
  - Export to PDF, DOCX, and EPUB (coming soon)

- **Admin Panel**
  - User management
  - Credit allocation
  - Call and transcription monitoring
  - Analytics dashboard

## Installation

### Requirements
- PHP >= 8.2
- Composer
- MySQL/PostgreSQL
- Node.js & NPM
- Laravel 11

### Setup Steps

1. **Clone and Install Dependencies**
```bash
cd c:\xampp\htdocs\bookdash
composer install
npm install
```

2. **Environment Configuration**
```bash
cp .env.example .env
php artisan key:generate
```

3. **Configure Database**
Update your `.env` file:
```env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=bookdash
DB_USERNAME=root
DB_PASSWORD=
```

4. **Configure Bird API (for call features)**
Add to `.env`:
```env
BIRD_API_KEY=your_bird_api_key
BIRD_API_URL=https://api.bird.com/v1
BIRD_PHONE_NUMBER=your_bird_phone_number
```

5. **Run Migrations**
```bash
php artisan migrate
```

6. **Create Storage Link**
```bash
php artisan storage:link
```

7. **Build Assets**
```bash
npm run build
# or for development
npm run dev
```

8. **Start Server**
```bash
php artisan serve
```

Visit: http://localhost:8000

## Database Structure

### Main Tables
- **users** - User accounts with role (user/admin) and credit balance
- **books** - Book information and status
- **chapters** - Chapter content, order, and metadata
- **credits** - Transaction history for credits
- **payments** - Payment records and invoices
- **calls** - Phone call records and status
- **transcriptions** - Call transcription data
- **email_logs** - Email processing logs

## User Workflows

### 1. Creating a Book
1. Navigate to Dashboard
2. Click "Create New Book"
3. Enter title, description, and optional cover image
4. Submit to create book

### 2. Adding Chapters

#### Method 1: Typing
1. Open a book
2. Click "Add Chapter"
3. Type content in the rich text editor
4. Auto-save activates after 2 seconds of inactivity
5. Manually save or mark as complete

#### Method 2: Email
1. User has a unique email address (format: `user-{id}-{hash}@bookdash.app`)
2. Send email with:
   - Subject: "Book Title – Chapter Name" (optional)
   - Body: Story content
3. System automatically creates chapter from email body
4. Deducts 1 credit

#### Method 3: Phone Call
1. Open chapter editor
2. Click "Start Call" in sidebar
3. Enter phone number
4. System initiates call via Bird API
5. Narrate story content
6. After call ends:
   - Recording is saved
   - Transcription is generated
   - Content is added to chapter
7. Receive notification when transcription is ready

### 3. Managing Credits
1. Visit Credits page
2. Select a package (5, 10, 25, or 50 credits)
3. Complete payment
4. Credits added to account immediately
5. View transaction history

### 4. Exporting Books
1. Open a book
2. Select export format (PDF, DOCX, EPUB)
3. Download generated file

## Admin Features

### Admin Dashboard
Access: `/admin` (requires admin role)

Features:
- User statistics
- Revenue tracking
- Recent activity monitoring

### User Management
- View all users
- Adjust credit balances
- Monitor user activity
- View user books and chapters

### Call Management
- Monitor all calls
- View call statuses
- Access recordings
- Review transcriptions

### Analytics
- User growth charts
- Revenue trends
- Chapter creation statistics by source
- System usage metrics

## API Endpoints

### Webhooks

#### Email Webhook
```
POST /webhook/email
```
Receives incoming emails and processes them into chapters.

**Expected Payload:**
```json
{
  "from": "user@example.com",
  "to": "user-123-abc@bookdash.app",
  "subject": "My Book – Chapter 1",
  "body": "Chapter content here..."
}
```

#### Call Webhook
```
POST /webhook/call
```
Receives call status updates from Bird API.

**Expected Payload:**
```json
{
  "call_id": "BIRD-123",
  "status": "completed",
  "recording_url": "https://..."
}
```

## Configuration

### Email Service
Configure your email service provider to forward emails to:
- Route: `/webhook/email`
- Method: POST

### Bird API Integration
1. Sign up for Bird API account
2. Get API credentials
3. Configure webhook URL: `{your-domain}/webhook/call`
4. Set up phone number

### Payment Gateway
Currently uses simulated payments. To integrate real payments:
1. Choose provider (Stripe, PayPal, etc.)
2. Add credentials to `.env`
3. Update `CreditController::processPurchase()` method

## Notifications

Users receive email notifications for:
- Chapter saved
- Call completed
- Transcription ready
- Low credit balance (when ≤ 5 credits)

Configure mail settings in `.env`:
```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=noreply@bookdash.app
MAIL_FROM_NAME="BookDash"
```

## Security Features

- Role-based access control (User/Admin)
- Policy-based authorization
- CSRF protection
- SQL injection prevention
- Secure file uploads
- API authentication (for webhooks)

## Future Enhancements

- [ ] AI rewriting and polishing tools
- [ ] Multi-language transcription support
- [ ] Collaboration features (share books)
- [ ] Audiobook generation
- [ ] Advanced analytics
- [ ] Mobile app
- [ ] Real-time collaboration
- [ ] Version control for chapters

## Troubleshooting

### Common Issues

**1. Credits not deducting**
- Check database connection
- Verify user has sufficient credits
- Check credits table for transactions

**2. Email webhook not working**
- Verify webhook endpoint is accessible
- Check email service configuration
- Review email_logs table for errors

**3. Call feature not working**
- Verify Bird API credentials
- Check network connectivity
- Review calls table for error messages

**4. Transcription stuck in "pending"**
- Would need to set up queue worker: `php artisan queue:work`
- Check transcription service status
- Review logs: `storage/logs/laravel.log`

## Support

For issues or questions:
- Email: support@bookdash.app
- Documentation: [Link to docs]
- GitHub Issues: [Link to repo]

## License

Proprietary - All rights reserved

## Credits

Built with Laravel 11, Tailwind CSS, and Bird API.

---

**Version:** 1.0.0  
**Last Updated:** December 24, 2025
