# Phase 2 - CodeIgniter 4 ERP Integration & Database Migration Plan

This guide details the steps required to transition the Phase 1 HTML/CSS/JS frontend prototype into a production-ready CodeIgniter 4 (CI4) application.

---

## 1. Directory Mapping & File Restructuring

Migrate the asset files from the prototype into the standard CodeIgniter 4 directory structure:

| Prototype Path | CI4 Destination Path | Description |
| :--- | :--- | :--- |
| `index.html` | `app/Views/public_home.php` | Main Public Landing View |
| `dealer.html` | `app/Views/dealer/dashboard.php` | Dealer Dashboard view |
| `distributor.html` | `app/Views/distributor/dashboard.php` | Distributor Dashboard view |
| `admin.html` | `app/Views/admin/dashboard.php` | Super Admin ERP View |
| `email_templates.html` | `app/Views/emails/showcase.php` | Email templates review view |
| `assets/css/style.css` | `public/assets/css/style.css` | Production Compiled CSS |
| `assets/js/*.js` | `public/assets/js/` | Charts and custom interaction scripts |

---

## 2. Reusable Component Layouts Setup

Break down public pages into reusable view chunks in `app/Views/templates/`:
- `header.php`: Include the top contact ribbon, stickiness Javascript hook, and mega menu links.
- `footer.php`: Newsletter action form, links, and copyright info.
- `sidebar.php`: Sidebar menu options for portal view states.

In CI4, use layout inheritance or view cells:
```php
// In app/Views/dealer/dashboard.php
<?= $this->extend('layouts/dashboard_layout') ?>
<?= $this->section('content') ?>
    <!-- Content goes here -->
<?= $this->endSection() ?>
```

---

## 3. Database Schema Design (MySQL)

Create the tables representing the enterprise business metrics:

```sql
-- 1. Users Table (Super Admin, Dealers, Distributors)
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    role ENUM('admin', 'dealer', 'distributor') NOT NULL,
    gst_number VARCHAR(15),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 2. Orders Table
CREATE TABLE orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    product_category VARCHAR(255) NOT NULL,
    order_value DECIMAL(12,2) NOT NULL,
    commission_rate DECIMAL(5,2) NOT NULL,
    status ENUM('processing', 'dispatched', 'delivered') DEFAULT 'processing',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- 3. Inventory Stock Table
CREATE TABLE inventory (
    id INT AUTO_INCREMENT PRIMARY KEY,
    sku VARCHAR(50) UNIQUE NOT NULL,
    product_name VARCHAR(255) NOT NULL,
    category VARCHAR(100) NOT NULL,
    quantity_liters DECIMAL(10,2) NOT NULL,
    warehouse_location VARCHAR(100),
    status VARCHAR(50) DEFAULT 'In Stock'
);
```

---

## 4. Controller & Dynamic Routing Configuration

Configure custom dynamic routes in `app/Config/Routes.php`:

```php
$routes->get('/', 'Home::index');

$routes->group('dealer', ['filter' => 'auth:dealer'], function($routes) {
    $routes->get('dashboard', 'DealerController::dashboard');
    $routes->post('order', 'DealerController::placeOrder');
});

$routes->group('distributor', ['filter' => 'auth:distributor'], function($routes) {
    $routes->get('dashboard', 'DistributorController::dashboard');
    $routes->post('stock', 'DistributorController::addStock');
});

$routes->group('admin', ['filter' => 'auth:admin'], function($routes) {
    $routes->get('dashboard', 'AdminController::dashboard');
    $routes->post('approve/(:num)', 'AdminController::approvePartner/$1');
});
```

---

## 5. Security & CSRF Protection
Before deploying Phase 2:
1. Enable CSRF (Cross-Site Request Forgery) in `app/Config/Filters.php`.
2. Secure inputs using the CI4 validation service (`$this->validate()`).
3. Store passwords securely with `password_hash($pwd, PASSWORD_BCRYPT)`.
