# 9cozone Technologies - WhatsApp AI CRM Setup Guide

This guide provides step-by-step instructions to set up the 9cozone WhatsApp AI CRM.

## Prerequisites
- Apache web server (or equivalent)
- PHP 7.4 or higher with cURL and PDO extensions enabled
- MySQL 5.7 or higher
- Meta Developer Account (for WhatsApp API)
- OpenAI API Key

## 1. Database Setup
1. Create a new MySQL database named `9cozone_crm`.
2. Import the `database.sql` file provided in the root directory into your newly created database.
   ```bash
   mysql -u root -p 9cozone_crm < database.sql
   ```
3. The default admin credentials are:
   - **Username**: admin
   - **Password**: password

## 2. Configuration
Update the configuration files located in the `/config` directory:

### `config/db.php`
Update the database connection details to match your environment.
```php
$host = 'localhost';
$dbname = '9cozone_crm';
$username = 'root'; // Update
$password = '';     // Update
```

### `config/whatsapp.php`
Update the WhatsApp API credentials.
```php
define('WA_PHONE_NUMBER_ID', 'YOUR_PHONE_NUMBER_ID');
define('WA_ACCESS_TOKEN', 'YOUR_WHATSAPP_ACCESS_TOKEN');
define('WA_VERIFY_TOKEN', 'YOUR_WEBHOOK_VERIFY_TOKEN'); // Set a custom string here for webhook verification
```

### `config/openai.php`
Update the OpenAI API credentials.
```php
define('OPENAI_API_KEY', 'YOUR_OPENAI_API_KEY');
```

## 3. WhatsApp Webhook Setup
1. Go to your Meta Developer App dashboard.
2. Navigate to **WhatsApp > Configuration**.
3. Under Webhook, click **Edit**.
4. Set the Callback URL to your server's webhook endpoint (e.g., `https://yourdomain.com/messages/webhook.php`).
   *Note: Meta requires HTTPS for webhooks.*
5. Set the Verify Token to the exact string you defined as `WA_VERIFY_TOKEN` in `config/whatsapp.php`.
6. Click **Verify and Save**.
7. Under Webhook fields, click **Manage** and subscribe to the `messages` field.

## 4. Automation Setup (Cron Jobs)
To enable follow-ups and AI replies, you must configure cron jobs on your server to run the PHP scripts periodically.

Open your crontab:
```bash
crontab -e
```

Add the following lines (adjust the paths to match your server environment):

```bash
# Run AI replies every minute
* * * * * php /path/to/9cozone_whatsapp-automaiton/automation/cron_ai_reply.php

# Run follow-ups every hour
0 * * * * php /path/to/9cozone_whatsapp-automaiton/automation/cron_followups.php
```

## 5. Security & Deployment Notes
- Ensure the `/config` directory is protected from direct web access.
- In a production environment, use a secure password for the admin account (update via database hash or build a profile update feature).
- Always use HTTPS.

## 6. Accessing the CRM
Navigate to the application in your web browser:
`http://yourdomain.com/public/login.php`
Log in with the default admin credentials and begin using your new system!
