# Deployment Checklist

## Prerequisites
- Apache with mod_rewrite enabled
- PHP 7.4 or higher
- MySQL/MariaDB server
- Composer dependencies installed (`vendor/autoload.php` exists)

## Step 1: Check Apache Configuration

Ensure `.htaccess` files are processed. Check your Apache config or .htaccess:

```bash
# Test if mod_rewrite is enabled
apache2ctl -M | grep rewrite

# Or check if .htaccess is processed (look for AllowOverride All)
grep -r "AllowOverride" /etc/apache2/
```

If .htaccess not being read, ensure the virtual host has:
```apache
<Directory /home/fettlxrdhe/public_html/bank/public>
    AllowOverride All
</Directory>
```

## Step 2: Database Setup

```bash
# Create database and import schema
mysql -u root -p << EOF
CREATE DATABASE bank_converter CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE bank_converter;
SOURCE /path/to/database.sql;
EOF
```

Verify tables were created:
```bash
mysql -u root -p bank_converter -e "SHOW TABLES;"
```

Expected output:
```
billing_plans
conversion_history
users
user_credits
```

## Step 3: Configure Database Connection

Option A - Environment variables:
```bash
export DB_HOST=localhost
export DB_NAME=bank_converter
export DB_USER=root
export DB_PASS=your_password
```

Option B - Edit `src/Database.php` directly:
```php
private const DEFAULT_HOST = 'localhost';
private const DEFAULT_NAME = 'bank_converter';
private const DEFAULT_USER = 'root';
private const DEFAULT_PASS = 'your_password';
```

## Step 4: Verify Installation

Test the login page:
```bash
curl -i https://fettlen.local/bank/public/login
```

Should return 200 OK (not 404).

Test API endpoint:
```bash
curl -i https://fettlen.local/bank/public/convert-api
```

Should return 405 Method Not Allowed (POST required).

## Step 5: Test Login Flow

1. Visit https://fettlen.local/bank/public/login
2. Try login with admin credentials:
   - Email: `admin@example.com`
   - Password: `admin123`
3. Should redirect to https://fettlen.local/bank/public/dashboard

## Step 6: Test Anonymous Access

1. Visit https://fettlen.local/bank/public/login
2. Click "Try 5 Free Conversions"
3. Should redirect to https://fettlen.local/bank/public/
4. Should show yellow badge with "⏱️ 5 free conversions"
5. Check browser cookies - should have `bank_anon_id` and `bank_anon_conversions`

## Step 7: Test PDF Conversion

1. Upload a PDF file to convert
2. Verify credit/counter decreases
3. Download CSV result
4. For authenticated: Check `/bank/public/dashboard` → History tab for database record
5. For anonymous: Verify no database record created

## Troubleshooting

### Issue: URLs still show .php extension

**Cause:** mod_rewrite not enabled or .htaccess not processed

**Solution:**
```bash
# Enable mod_rewrite
sudo a2enmod rewrite
sudo systemctl restart apache2

# Verify AllowOverride All in virtualhost or .htaccess
```

### Issue: Redirect loop (keeps redirecting to login)

**Cause:** Session not starting or CSRF token issue

**Solution:**
```bash
# Check PHP session.save_path is writable
php -i | grep session.save_path
# Ensure /tmp or your session dir has write permissions
```

### Issue: "Database connection failed"

**Cause:** DB credentials wrong or MySQL not running

**Solution:**
```bash
# Test connection manually
mysql -u root -p bank_converter -e "SELECT 1;"

# Check variables are set
php -r "echo getenv('DB_HOST');"

# Review error logs
tail -f /var/log/apache2/error.log
```

### Issue: Anonymous cookie not persisting

**Cause:** Cookies blocked or path mismatch

**Solution:**
- Ensure you're accessing via `/bank/public/` path (not different port/domain)
- Check browser cookie settings allow third-party cookies
- Verify .htaccess is setting cookie path correctly

## File Permissions

Ensure files have correct permissions:

```bash
cd /home/fettlxrdhe/public_html/bank

# PHP scripts readable
chmod 644 public/*.php src/*.php

# .htaccess readable
chmod 644 public/.htaccess

# Session directory writable (if using custom session path)
chmod 755 /path/to/sessions
```

## Security Notes

⚠️ **Before Production:**
1. Change admin password immediately
2. Set secure random DB_PASS
3. Enable HTTPS (don't use HTTP)
4. Set `session.cookie_secure = On` in php.ini
5. Disable debug mode (`?debug=true` should not work)
6. Set up proper logging and monitoring
7. Implement rate limiting
8. Consider adding 2FA for admin accounts

## Performance Optimization

For production:
```ini
# php.ini
display_errors = Off
log_errors = On
error_log = /var/log/php-errors.log

# Enable opcache
opcache.enable = 1
opcache.memory_consumption = 128
```

## SSL Certificate (Let's Encrypt Example)

```bash
sudo certbot certonly --apache -d fettlen.local
# Then update VirtualHost to use SSL cert paths
```

## Backup Strategy

```bash
# Backup database
mysqldump -u root -p bank_converter > backup.sql

# Backup files
tar -czf backup.tar.gz /home/fettlxrdhe/public_html/bank/

# Restore database
mysql -u root -p bank_converter < backup.sql
```

---

Once all steps are complete, the application is ready for testing and deployment!
