# Customer Type Implementation Guide

## Quick Start

Follow these steps to implement the customer type filtering for products:

### Step 1: Run the Migration

```bash
cd afinet-portal-backend
php artisan migrate
```

This will update existing products with the correct `customer_type` values.

### Step 2: (Optional) Run the Seeder

If you want to ensure all products have correct values and descriptions:

```bash
php artisan db:seed --class=ProductCustomerTypeSeeder
```

### Step 3: Verify the Changes

Run the verification queries to ensure everything is set up correctly:

```bash
# Connect to your database
mysql -u your_user -p your_database

# Run verification query
SELECT customer_type, COUNT(*) as count, GROUP_CONCAT(code) as products
FROM product_packages
WHERE is_active = 1
GROUP BY customer_type;
```

Expected output:
```
+---------------+-------+------------------------------------------+
| customer_type | count | products                                 |
+---------------+-------+------------------------------------------+
| business      | 3     | SHARED_BROADBAND,DEDICATED_BURST,DIA     |
| wholesale     | 2     | DARK_FIBRE,IPLC                          |
| both          | 3     | MAGELLAN,IP_TRANSIT,INTERCITY_VPN        |
+---------------+-------+------------------------------------------+
```

### Step 4: Test the API

Test the filtering with different customer types:

```bash
# Test business customer view
curl -X GET "http://your-api/api/products?customer_type=business" \
  -H "Authorization: Bearer YOUR_TOKEN"

# Test wholesale customer view
curl -X GET "http://your-api/api/products?customer_type=wholesale" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### Step 5: Run Automated Tests

```bash
php artisan test --filter ProductCustomerTypeFilterTest
```

All tests should pass ✅

## Product Assignments Reference

### Business Only (customer_type = 'business')
- ✅ Shared Business Broadband (SHARED_BROADBAND)
- ✅ Dedicated Burst Internet (DEDICATED_BURST)
- ✅ Dedicated Internet Access (DIA)

### Wholesale Only (customer_type = 'wholesale')
- ✅ Dark Fibre Lease (DARK_FIBRE)
- ✅ International Private Line Circuit (IPLC)

### Both Business and Wholesale (customer_type = 'both')
- ✅ Magellan Metro-VPN (MAGELLAN)
- ✅ IP Transit (IP_TRANSIT)
- ✅ InterCity-VPN (INTERCITY_VPN)

## Frontend Behavior

The frontend automatically filters products based on the logged-in customer type:

- **Company customers** (`customer.type = 'company'`):
  - See: Business + Both products
  - Don't see: Wholesale-only products

- **Individual customers** (`customer.type = 'individual'`):
  - See: Wholesale + Both products
  - Don't see: Business-only products

## Troubleshooting

### Products not showing for a customer type

1. Check the customer type in the database:
```sql
SELECT id, email, type FROM customers WHERE email = 'customer@example.com';
```

2. Check the product's customer_type:
```sql
SELECT code, name, customer_type, is_active FROM product_packages WHERE code = 'PRODUCT_CODE';
```

3. Verify the product is active:
```sql
UPDATE product_packages SET is_active = 1 WHERE code = 'PRODUCT_CODE';
```

### Wrong products appearing

Check the filtering logic in the API response:
```bash
curl -X GET "http://your-api/api/products?customer_type=business" \
  -H "Authorization: Bearer YOUR_TOKEN" | jq '.data[] | {code, customer_type}'
```

### Need to change a product's customer type

```sql
-- Change to business only
UPDATE product_packages SET customer_type = 'business' WHERE code = 'PRODUCT_CODE';

-- Change to wholesale only
UPDATE product_packages SET customer_type = 'wholesale' WHERE code = 'PRODUCT_CODE';

-- Change to both
UPDATE product_packages SET customer_type = 'both' WHERE code = 'PRODUCT_CODE';
```

## Files Created/Modified

### Backend Files
- ✅ `database/migrations/2026_02_16_125630_update_product_customer_types.php`
- ✅ `database/seeders/ProductCustomerTypeSeeder.php`
- ✅ `tests/Feature/ProductCustomerTypeFilterTest.php`
- ✅ `docs/PRODUCT_CUSTOMER_TYPE_FILTERING.md`
- ✅ `docs/SQL_VERIFICATION_QUERIES.sql`

### Frontend Files
- ℹ️ No changes needed - already implemented correctly

### Documentation Files
- ✅ `DATABASE_CUSTOMER_TYPE_SOLUTION.md`
- ✅ `CUSTOMER_TYPE_IMPLEMENTATION_GUIDE.md` (this file)

## Next Steps

1. ✅ Run migration to update product customer types
2. ✅ Verify changes with SQL queries
3. ✅ Test API endpoints with different customer types
4. ✅ Run automated test suite
5. ✅ Test in frontend with both company and individual accounts
6. ✅ Update any product documentation or user guides
7. ✅ Monitor logs for any filtering issues

## Support

If you encounter any issues:

1. Check the logs: `storage/logs/laravel.log`
2. Run the verification queries in `docs/SQL_VERIFICATION_QUERIES.sql`
3. Review the full documentation in `docs/PRODUCT_CUSTOMER_TYPE_FILTERING.md`
4. Run the test suite to identify specific failures

## Summary

✅ Database schema supports 'business', 'wholesale', and 'both' customer types  
✅ Backend filtering logic correctly handles all three values  
✅ Frontend sends correct customer_type based on customer.type  
✅ Migration updates existing products with correct values  
✅ Seeder provides fallback for missing products  
✅ Tests verify filtering works correctly  
✅ Documentation covers all aspects of the implementation  

The system is ready to properly filter products based on customer segments!
