# Email Notification System Implementation

## Overview
Comprehensive email notification system for all customer and company interactions throughout the customer journey.

## Email Configuration
**Location:** `.env` file

```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=tnrwatida@gmail.com
MAIL_PASSWORD="tqan cfdc veyi seeg"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=tnrwatida@gmail.com
MAIL_FROM_NAME="AFINET Customer Portal"

COMPANY_MAIL=tawona@quatrohaus.com
```

## Email Triggers & Recipients

### 1. Customer Registration
**Trigger:** Customer signs up  
**Email:** WelcomeEmail  
**To:** Customer  
**Subject:** Welcome to AFINET - Account Created Successfully  
**Content:**
- Welcome message
- Account details
- Available features
- Login link

### 2. Feasibility Check Results
**Trigger:** Feasibility check completed  
**Email:** FeasibilityResultEmail  
**To:** Customer  
**Subject:** Feasibility Check Results - Available Packages  
**Content:**
- Feasibility status
- Available packages
- Pricing information
- PDF attachment (if available)

### 3. Quotation Requested
**Trigger:** Customer creates quotation  
**Email:** QuotationRequestedNotification  
**To:** Company (COMPANY_MAIL)  
**Subject:** New Quotation Request - [Reference]  
**Content:**
- Customer details
- Requested package
- Service address
- Quotation details
- Contact information

### 4. Custom Package Enquiry
**Trigger:** Customer clicks "Contact Sales" on custom package  
**Email:** CustomPackageEnquiry  
**To:** Company (COMPANY_MAIL)  
**Subject:** Custom Package Enquiry - [Customer Name]  
**Content:**
- Customer details
- Requirements/specifications
- Contact preferences
- Additional notes

### 5. Quotation Status Changed
**Trigger:** Quotation status updates  
**Email:** QuotationStatusChanged  
**To:** Customer  
**Subject:** Quotation Status Update - [Reference]  
**Content:**
- Old status → New status
- Quotation details
- Next steps
- Action required (if any)

### 6. Quotation Accepted
**Trigger:** Customer accepts quotation  
**Email:** QuotationAcceptedNotification  
**To:** Company (COMPANY_MAIL)  
**Subject:** Quotation Accepted - [Reference]  
**Content:**
- Customer details
- Accepted quotation details
- Package information
- Installation preferences
- Next steps for processing

### 7. Sales Order Created
**Trigger:** Sales order generated  
**Email:** SalesOrderCreated  
**To:** Customer  
**Subject:** Order Confirmed - [Order Number]  
**Content:**
- Order confirmation
- Order details
- Package information
- Installation timeline
- Payment information

### 8. Invoice Created
**Trigger:** Invoice generated  
**Email:** InvoiceCreated  
**To:** Customer  
**Subject:** Invoice Generated - [Invoice Number]  
**Content:**
- Invoice details
- Amount due
- Due date
- Payment methods
- Payment link

### 9. Payment Received (Pesepay)
**Trigger:** Pesepay payment completed  
**Email:** PaymentReceivedNotification  
**To:** Company (COMPANY_MAIL)  
**Subject:** Payment Received - [Reference]  
**Content:**
- Customer details
- Payment amount
- Payment method (Pesepay)
- Invoice details
- Transaction reference

### 10. Payment Received (Bank Transfer)
**Trigger:** Customer uploads proof of payment  
**Email:** PaymentReceivedNotification  
**To:** Company (COMPANY_MAIL)  
**Subject:** Payment Received - [Reference]  
**Content:**
- Customer details
- Payment amount
- Payment method (Bank Transfer)
- Bank reference
- Proof of payment (PDF attachment)
- Requires verification

### 11. Payment Approved
**Trigger:** Finance approves payment  
**Email:** PaymentApproved  
**To:** Customer  
**Subject:** Payment Approved - Invoice [Number]  
**Content:**
- Payment confirmation
- Invoice marked as paid
- Receipt details
- Service activation timeline

## Email Classes Created

### Customer Emails
1. `WelcomeEmail.php` - Account creation
2. `FeasibilityResultEmail.php` - Coverage check results
3. `QuotationStatusChanged.php` - Status updates
4. `SalesOrderCreated.php` - Order confirmation
5. `InvoiceCreated.php` - Invoice notification
6. `PaymentConfirmation.php` - Payment success (already exists)
7. `PaymentApproved.php` - Payment verification

### Company Emails
1. `QuotationRequestedNotification.php` - New quotation
2. `CustomPackageEnquiry.php` - Custom package request
3. `QuotationAcceptedNotification.php` - Quotation accepted
4. `PaymentReceivedNotification.php` - Payment notification
5. `ProofOfPaymentReceived.php` - Bank transfer POP (already exists)

## Email Templates Location
`afinet-portal-backend/resources/views/emails/`

### Templates to Create
- [x] `welcome.blade.php`
- [ ] `feasibility-result.blade.php`
- [ ] `quotation-requested-notification.blade.php`
- [ ] `custom-package-enquiry.blade.php`
- [ ] `quotation-status-changed.blade.php`
- [ ] `quotation-accepted-notification.blade.php`
- [ ] `sales-order-created.blade.php`
- [ ] `invoice-created.blade.php`
- [ ] `payment-received-notification.blade.php`
- [ ] `payment-approved.blade.php`

## Integration Points

### 1. Registration (AuthController)
```php
use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;

// After customer registration
Mail::to($customer->email)->send(new WelcomeEmail($customer));
```

### 2. Feasibility Check (FeasibilityController)
```php
use App\Mail\FeasibilityResultEmail;

// After feasibility check
Mail::to($customer->email)->send(
    new FeasibilityResultEmail($feasibilityCheck, $packages, $pdfPath)
);
```

### 3. Quotation Created (QuotationController)
```php
use App\Mail\QuotationRequestedNotification;

// After quotation creation
Mail::to(config('mail.company_mail'))->send(
    new QuotationRequestedNotification($quotation, $customer)
);
```

### 4. Custom Package Enquiry (ProductController)
```php
use App\Mail\CustomPackageEnquiry;

// When customer contacts sales
Mail::to(config('mail.company_mail'))->send(
    new CustomPackageEnquiry($customer, $enquiryDetails)
);
```

### 5. Quotation Status Change (QuotationController)
```php
use App\Mail\QuotationStatusChanged;

// When status changes
Mail::to($customer->email)->send(
    new QuotationStatusChanged($quotation, $oldStatus, $newStatus)
);
```

### 6. Quotation Accepted (QuotationController)
```php
use App\Mail\QuotationAcceptedNotification;

// When customer accepts
Mail::to($customer->email)->send(new SalesOrderCreated($salesOrder));
Mail::to(config('mail.company_mail'))->send(
    new QuotationAcceptedNotification($quotation, $customer)
);
```

### 7. Sales Order Created (QuotationController)
```php
use App\Mail\SalesOrderCreated;

// After order creation
Mail::to($customer->email)->send(new SalesOrderCreated($salesOrder));
```

### 8. Invoice Created (QuotationController/PaymentController)
```php
use App\Mail\InvoiceCreated;

// After invoice generation
Mail::to($customer->email)->send(new InvoiceCreated($invoice));
```

### 9. Payment Received (PaymentController)
```php
use App\Mail\PaymentReceivedNotification;

// After payment (Pesepay or Bank Transfer)
Mail::to(config('mail.company_mail'))->send(
    new PaymentReceivedNotification($payment, $invoice, $customer, $proofPath)
);
```

### 10. Payment Approved (PaymentController)
```php
use App\Mail\PaymentApproved;

// After finance approval
Mail::to($customer->email)->send(new PaymentApproved($payment, $invoice));
```

## Additional Email Opportunities

### Installation Scheduled
- When installation date is set
- Reminder 24 hours before installation

### Service Activated
- When service goes live
- Welcome to service email

### Support Ticket Created ✅ IMPLEMENTED
- Ticket confirmation to customer (SupportTicketConfirmation)
- New ticket notification to support team (SupportTicketCreated)
- Sends to: Customer email + company_email (tawona@quatrohaus.com)

### Support Ticket Resolved
- Resolution notification to customer

### Payment Reminder
- Before invoice due date
- After invoice overdue

### Subscription Renewal
- Renewal reminder
- Auto-renewal confirmation

## Email Queue Configuration

For production, use queue for email sending:

```env
QUEUE_CONNECTION=database
```

```php
// Send email to queue
Mail::to($customer->email)->queue(new WelcomeEmail($customer));
```

## Testing Emails

### Artisan Command
```bash
php artisan tinker

# Test welcome email
$customer = App\Models\Customer::first();
Mail::to($customer->email)->send(new App\Mail\WelcomeEmail($customer));
```

### Mailtrap (Development)
```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
```

## Email Styling

All emails use consistent styling:
- AFINET brand colors (purple gradient)
- Responsive design
- Professional layout
- Clear call-to-action buttons
- Company branding

## Status: IN PROGRESS

- [x] Email classes created (10/10)
- [x] Welcome email template created (1/10)
- [ ] Remaining email templates (9/10)
- [ ] Controller integration
- [ ] Testing
- [ ] Documentation

## Next Steps

1. Create remaining email templates
2. Integrate emails into controllers
3. Test all email triggers
4. Add email queue processing
5. Create custom package enquiry modal
6. Add email logging/tracking
