# ChatBud PHP Backend - Complete Setup Checklist

## ✅ Phase 1: Automated Setup (COMPLETED)

- [x] Run `setup.sh` or `bash setup.sh`
- [x] Created `.env` configuration file
- [x] Created required directories
- [x] Set file permissions
- [x] Project structure initialized

## ⏳ Phase 2: Database Configuration (ACTION REQUIRED)

### Choose Your Database

- [ ] **Option A: MySQL** (Recommended)
  - [ ] Download MySQL: https://dev.mysql.com/downloads/mysql/
  - [ ] Install MySQL Community Edition
  - [ ] Create database and user:
    ```sql
    mysql -u root -p
    CREATE DATABASE chatbud DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    CREATE USER 'chatbud_user'@'localhost' IDENTIFIED BY 'YourStrongPassword123!';
    GRANT ALL PRIVILEGES ON chatbud.* TO 'chatbud_user'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
    ```
  - [ ] Import database schema:
    ```bash
    mysql -u chatbud_user -p chatbud < database.sql
    ```
  - [ ] Update `.env` file:
    ```env
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=chatbud
    DB_USERNAME=chatbud_user
    DB_PASSWORD=YourStrongPassword123!
    ```

- [ ] **Option B: PostgreSQL**
  - [ ] Download PostgreSQL: https://www.postgresql.org/download/
  - [ ] Install PostgreSQL
  - [ ] Create database and user:
    ```sql
    createdb chatbud
    createuser chatbud_user --password
    psql -d chatbud -c "GRANT ALL PRIVILEGES ON DATABASE chatbud TO chatbud_user;"
    ```
  - [ ] Import schema (convert to PostgreSQL first)
  - [ ] Update `.env` file:
    ```env
    DB_CONNECTION=pgsql
    DB_HOST=localhost
    DB_PORT=5432
    DB_DATABASE=chatbud
    DB_USERNAME=chatbud_user
    DB_PASSWORD=your_password
    ```

## ⏳ Phase 3: Email Service Setup (ACTION REQUIRED)

### Choose Your Email Provider

- [ ] **Option A: Mailtrap** (Recommended for Development)
  - [ ] Sign up: https://mailtrap.io (free account)
  - [ ] Create inbox named "Development"
  - [ ] Go to Integrations → SMTP
  - [ ] Copy SMTP credentials
  - [ ] Update `.env` file:
    ```env
    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.mailtrap.io
    MAIL_PORT=465
    MAIL_USERNAME=your_mailtrap_username
    MAIL_PASSWORD=your_mailtrap_password
    MAIL_FROM_ADDRESS=noreply@chatbud.com
    ```

- [ ] **Option B: SendGrid** (Recommended for Production)
  - [ ] Sign up: https://sendgrid.com
  - [ ] Go to Settings → API Keys
  - [ ] Create new API key with full access
  - [ ] Copy the key
  - [ ] Update `.env` file:
    ```env
    MAIL_DRIVER=sendgrid
    SENDGRID_API_KEY=SG.your_api_key_here
    MAIL_FROM_ADDRESS=noreply@chatbud.com
    ```

- [ ] **Option C: Gmail SMTP** (Free)
  - [ ] Enable 2-factor authentication
  - [ ] Generate App Password
  - [ ] Update `.env` file:
    ```env
    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=587
    MAIL_USERNAME=your-email@gmail.com
    MAIL_PASSWORD=your-16-char-app-password
    MAIL_FROM_ADDRESS=your-email@gmail.com
    ```

## ✅ Phase 4: Verification (RUN THESE TESTS)

- [ ] Verify database connection:
  ```bash
  php test_db.php
  ```
  Expected: `✓ Database connection successful`

- [ ] Verify email configuration:
  ```bash
  php test_email.php
  ```
  Expected: `✓ Email sent successfully!` (or SMTP error if not configured)

- [ ] Check `.env` file has all required values:
  ```bash
  cat .env
  ```
  Should contain all database and email settings

## ⏳ Phase 5: Start Web Server (ACTION REQUIRED)

Choose one option:

### Option A: PHP Built-in Server (Easiest for Development)

```bash
cd ChatBud-PHP
php -S localhost:8000 -t public/
```

Expected output:
```
[date time] PHP Development Server is running...
Listening on http://localhost:8000
```

### Option B: Apache

1. Create vhost configuration:
   ```apache
   <VirtualHost *:80>
       ServerName chatbud.local
       DocumentRoot /path/to/ChatBud-PHP/public
       
       <Directory /path/to/ChatBud-PHP/public>
           AllowOverride All
           Require all granted
       </Directory>
   </VirtualHost>
   ```

2. Enable mod_rewrite:
   ```bash
   a2enmod rewrite
   a2ensite chatbud
   systemctl restart apache2
   ```

3. Add to hosts file:
   ```
   127.0.0.1  chatbud.local
   ```

### Option C: Nginx

1. Create configuration:
   ```nginx
   server {
       listen 80;
       server_name chatbud.local;
       root /path/to/ChatBud-PHP/public;
       
       index index.php;
       
       location / {
           try_files $uri $uri/ /index.php?$query_string;
       }
       
       location ~ \.php$ {
           fastcgi_pass unix:/var/run/php/php8.x-fpm.sock;
           fastcgi_index index.php;
           include fastcgi_params;
           fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       }
   }
   ```

2. Enable and reload:
   ```bash
   ln -s /etc/nginx/sites-available/chatbud /etc/nginx/sites-enabled/
   nginx -t
   systemctl restart nginx
   ```

## ✅ Phase 6: Test API (OPTIONAL BUT RECOMMENDED)

### Using PowerShell (Windows)

```bash
.\test_api.ps1
```

This will:
- Register a test user
- Login
- Create a post
- Add comments
- Get feed
- Like posts
- Update profile

### Using Bash (Linux/Mac)

```bash
bash test_api.sh
```

### Manual Testing with curl

```bash
# 1. Register user
curl -X POST http://localhost:8000/api.php/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{"email":"user@example.com","username":"user","password":"pass123","display_name":"User"}'

# 2. Login (save cookies)
curl -X POST http://localhost:8000/api.php/v1/auth/login \
  -H "Content-Type: application/json" \
  -c cookies.txt \
  -d '{"email":"user@example.com","password":"pass123"}'

# 3. Create post
curl -X POST http://localhost:8000/api.php/v1/posts \
  -H "Content-Type: application/json" \
  -b cookies.txt \
  -d '{"content":"Hello ChatBud!"}'

# 4. Get feed
curl http://localhost:8000/api.php/v1/feed -b cookies.txt
```

## 📋 Daily Checklist (Before Deploying)

- [ ] Database connection test passes: `php test_db.php`
- [ ] Email service test passes: `php test_email.php`
- [ ] Web server running on localhost:8000
- [ ] Can create user via API
- [ ] Can login and get token
- [ ] Can create posts
- [ ] Can upload files
- [ ] Can like posts and add comments
- [ ] Can see feed with own posts
- [ ] Email notifications are working

## 🔒 Security Checklist (Before Production)

- [ ] Set `APP_DEBUG=false` in `.env`
- [ ] Use strong database password
- [ ] Use strong API keys
- [ ] Enable HTTPS/SSL
- [ ] Set up WAF/DDoS protection
- [ ] Configure database backups
- [ ] Set up error logging to file (not console)
- [ ] Set up monitoring and alerts
- [ ] Review CORS configuration
- [ ] Set security headers
- [ ] Enable rate limiting
- [ ] Test password reset flow
- [ ] Test email verification
- [ ] Test user authentication
- [ ] Backup database
- [ ] Document deployment process

## 📚 Documentation to Read

Read these in order:

1. **QUICK_START.md** - Quick reference guide
2. **API_DOCUMENTATION.md** - All API endpoints
3. **DATABASE_SETUP.md** - Database maintenance
4. **EMAIL_SETUP.md** - Email configuration details
5. **README_COMPLETE.md** - Full documentation
6. **INTEGRATION_GUIDE.md** - System architecture

## 🆘 Troubleshooting

### Database Issues

See **DATABASE_SETUP.md** → Troubleshooting section

Common issues:
- "could not find driver" → Install MySQL PDO driver
- "Access denied" → Check credentials in .env
- "Unknown database" → Import database.sql

### Email Issues

See **EMAIL_SETUP.md** → Troubleshooting section

Common issues:
- "Failed to connect to mailserver" → Configure SMTP in .env
- "Authorization failed" → Check API key/password
- "Email not sending" → Check .env configuration

### API Issues

See **API_DOCUMENTATION.md**

Common issues:
- 404 errors → Check mod_rewrite is enabled
- 401 errors → Login and include session cookie
- 403 errors → Check user permissions
- 500 errors → Check error logs

## ✅ Success Indicators

You'll know you're ready when:

- [x] Setup script ran successfully
- [ ] `.env` file is configured with database and email
- [ ] `php test_db.php` shows database tables
- [ ] `php test_email.php` shows email configuration
- [ ] Web server running on localhost:8000
- [ ] Can register user via API
- [ ] Can login and receive token
- [ ] Can create posts
- [ ] Can upload files
- [ ] API test script (`test_api.ps1` or `test_api.sh`) passes

## 🚀 Ready to Deploy!

Once all checks are complete:

1. **Copy project to production server**
2. **Update .env with production values**
3. **Set up automatic backups**
4. **Configure monitoring**
5. **Set up CI/CD pipeline**
6. **Test all API endpoints in production**
7. **Monitor logs and performance**

## 📊 Current Progress

```
Setup Status:
  ✓ Backend code: COMPLETE
  ✓ Database schema: COMPLETE
  ✓ API endpoints: COMPLETE
  ✓ File uploads: COMPLETE
  ✓ Email system: COMPLETE
  ✓ Documentation: COMPLETE
  ⏳ Database config: PENDING (Phase 2)
  ⏳ Email config: PENDING (Phase 3)
  ⏳ Web server: PENDING (Phase 5)
  ⏳ API testing: PENDING (Phase 6)

Overall Completion: 40% (setup), 60% (after config), 90% (after web server)
```

## 💡 Pro Tips

1. **Keep `.env` secure** - Never commit it to git
2. **Use strong passwords** - For database and email
3. **Test everything** - Use provided test scripts
4. **Read documentation** - Answers are in the docs
5. **Check error logs** - Helps with troubleshooting
6. **Start with test database** - Use Mailtrap for email development
7. **Backup regularly** - Database backups are critical
8. **Monitor performance** - Check slow queries

## 📞 Need Help?

- Check documentation files
- Read troubleshooting sections
- Check GitHub issues
- Email: support@chatbud.com

---

**Keep this checklist handy!** Print it or bookmark it.

Next step: Go to Phase 2 and set up your database. 👉 **DATABASE_SETUP.md**

Good luck! 🚀
