# Product Profile System

A product profile is a named, version-controlled configuration that captures:
- Which **apps** are enabled (Sales, Product, Academic, CRM, etc.)
- Which **modules** are enabled within those apps
- **Personalizer** scalar settings (colors, fonts, login page text)
- **General settings** (app name, description)

One click — or one CLI command — restores the full product configuration after a database reseed.

---

## The Problem It Solves

Running `dev:install` (or any seeder reset) wipes `status` fields on `app_managements`, `module_managements`, and `personalizer_settings`, destroying every manual configuration change. Previously, the admin had to re-enable every app and module by hand after each reseed.

**Solution:** Store product configurations as PHP config files in version control. After a reseed, apply the matching profile in one step.

---

## Available Products

`taskco-erp` is the **default** — applied automatically on fresh install (`dev:install`).

| Profile slug | Product | Key apps |
|---|---|---|
| `taskco-erp` ⭐ default | Taskco ERP | All apps — Academic, Sales, Product, Ecommerce, Inventory, Purchase, Promotion, Contact, User, Website, Productivity |
| `taskco-ecommerce` | Taskco Ecommerce | Sales, Product, Ecommerce, Contact, User, Inventory, Promotion, Productivity |
| `taskco-sales` | Taskco Sales | Sales, CRM, Contact, Productivity, Accounting, Communication, Promotion |
| `taskco-education` | Taskco Education | Academic, User, Contact, Productivity, Website |

---

## After a Reseed — Recovery in One Step

### Option A — CLI (fastest, recommended for developers)

```bash
# List all available profiles and see which is active
php artisan profile:apply --list

# Apply a specific profile
php artisan profile:apply taskco-ecommerce

# Interactive — choose from a menu
php artisan profile:apply
```

### Option B — Admin UI

1. Log in to the Admin panel
2. Go to **Workspace Settings → Product Profiles**
3. Click **Apply** on the desired profile

---

## Profile Config Files

Location: `AdminApp/config/product-profiles/`

```
config/product-profiles/
├── taskco-erp.php          ← default (applied on fresh install)
├── taskco-ecommerce.php
├── taskco-sales.php
└── taskco-education.php
```

### Config file structure

```php
return [
    'name'        => 'Taskco Ecommerce',
    'slug'        => 'taskco-ecommerce',
    'description' => '...',
    'icon'        => 'ShoppingCart',   // Lucide icon name
    'color'       => '#4F46E5',
    'version'     => '1.0',

    // App slugs to ENABLE (all others are disabled)
    'apps' => [
        'sales', 'product', 'ecommerce', 'contact', 'user', 'website',
    ],

    // Module slugs to ENABLE
    'modules' => [
        'sales_orders', 'sales_payments', 'product_product', ...
    ],

    // Personalizer scalar values (file uploads are never touched)
    'personalizer' => [
        'branding' => [
            'primary_color'   => '#4F46E5',
            'secondary_color' => '#7C3AED',
        ],
        'login' => [
            'login_app_name' => 'Taskco Ecommerce',
            'login_title'    => 'Welcome back',
        ],
        'typography' => [
            'font_family' => 'Inter',
        ],
    ],

    // settings table key → value
    'settings' => [
        'app_name'        => 'Taskco Ecommerce',
        'app_description' => '...',
    ],
];
```

---

## Adding a New Product Profile

### Option A — Configure through UI, then export

1. Go to **Workspace Settings → Features** and enable the desired apps/modules
2. Go to **Workspace Settings → Appearance** and set branding/login settings
3. Go to **Workspace Settings → Product Profiles → Export Current State**
4. Enter a slug (e.g. `taskco-erp`), name, and description
5. A new file is generated at `config/product-profiles/taskco-erp.php`
6. Commit the file to version control

Or via CLI:

```bash
php artisan profile:apply --export=taskco-erp --name="Taskco ERP" --description="Full ERP suite"
```

### Option B — Write the config file manually

Create `AdminApp/config/product-profiles/taskco-erp.php` following the structure above.
All app and module slugs can be found by running:

```bash
php artisan tinker --execute="echo implode(\"\n\", \AdminApp\Models\AppManagement::pluck('slug')->toArray());"
```

---

## What `apply()` Does (and Does NOT Do)

### Does
- Disables all non-system apps not in the profile's `apps` list
- Enables all apps in the `apps` list
- Disables all modules not in the `modules` list
- Enables all modules in the `modules` list
- Updates personalizer **scalar** settings (colors, fonts, text)
- Updates `app_name` and `app_description` in the settings table
- Records `active_product_profile` in the settings table

### Does NOT
- Overwrite uploaded files (logos, favicons, login banners) — these are managed via the Personalizer UI and survive profile switches
- Touch tenant-level personalizer overrides
- Modify package definitions or subscriptions
- Change email settings

---

## Seeder Safety (firstOrCreate)

`AdminSettingsSeeder` uses `firstOrCreate` (not `updateOrCreate`) for product-controlled settings:

- `app_name`, `app_description`, and all general settings
- All `PersonalizerSetting` rows
- The `active_product_profile` key

This means **reseeding no longer wipes branding or the active profile record**. Only structural/email settings are reset on reseed.

However, `AppManagement.status` and `ModuleManagement.status` are still reset by the feature seeders. That is why you need to run `profile:apply` after a reseed.

---

## Architecture

```
config/product-profiles/*.php          ← version-controlled config files
        │
        ▼
ProductProfileService                  ← core business logic
├── listProfiles()   → reads all .php files
├── apply(slug)      → DB transaction: apps, modules, personalizer, settings
└── export(slug)     → reads DB state → writes .php file
        │
        ▼
ProductProfileController               ← HTTP layer
├── GET  /admin/settings/product-profile
├── POST /admin/settings/product-profile/{slug}
└── POST /admin/settings/product-profile-export
        │
        ▼
ApplyProductProfileCommand             ← CLI layer
└── php artisan profile:apply [slug]
```

---

## File Locations

| What | Where |
|------|-------|
| Profile configs | `AdminApp/config/product-profiles/` |
| Service | `AdminApp/app/Services/Admin/ProductProfileService.php` |
| Controller | `AdminApp/app/Http/Controllers/Admin/Settings/ProductProfileController.php` |
| Artisan command | `AdminApp/app/Console/Commands/ApplyProductProfileCommand.php` |
| React UI page | `AdminApp/resources/assets/pages/Admin/Settings/ProductProfile/Index.tsx` |
| Routes | `AdminApp/routes/admin.php` (search: `product-profile`) |
| Seeder (modified) | `AdminApp/database/seeders/AdminSettingsSeeder.php` |
