# Technical Architecture

## Stack

- **Framework**: CodeIgniter 4 (CI4)
- **Pattern**: MVC (Controllers + Models + Views)
- **Database**: MySQL/MariaDB (CI4 database layer + migrations)

## Request Lifecycle (High Level)

1. Request enters via `public/index.php`.
2. Global filters run:
   - Authentication filter blocks all routes except `/` and `/auth/*`.
3. Routing resolves:
   - Explicit routes in `app/Config/Routes.php`
   - Plus CI4 AutoRoute (enabled)
4. Controller executes, uses models or query builder.
5. View is rendered and returned.

## Routing

The main route groups:

- `/shopkeeper/*` → `App\Controllers\Frontend\Shopkeeper\...`
- `/user/*` → `App\Controllers\Frontend\User\...`

AutoRoute is enabled, so controller methods can also be reachable by convention. If you want tighter security, consider disabling AutoRoute and defining routes explicitly.

## Main Domain Modules (Code Pointers)

- Shops (public browsing): [Home](file:///c:/wamp64/www/rewarder/app/Controllers/Home.php)
- Discounts (admin): [RewarderDiscounts](file:///c:/wamp64/www/rewarder/app/Controllers/RewarderDiscounts.php)
- Discounts (customer view): [Frontend User Discounts](file:///c:/wamp64/www/rewarder/app/Controllers/Frontend/User/Discounts.php)
- Reward rules (admin): [RewarderRewardRules](file:///c:/wamp64/www/rewarder/app/Controllers/RewarderRewardRules.php)
- Wallet/Ledger (admin): [RewarderWallets](file:///c:/wamp64/www/rewarder/app/Controllers/RewarderWallets.php), [RewarderLedgers](file:///c:/wamp64/www/rewarder/app/Controllers/RewarderLedgers.php)
- Subscriptions (admin): [Subscriptions](file:///c:/wamp64/www/rewarder/app/Controllers/Subscriptions.php), [ShopSubscriptions](file:///c:/wamp64/www/rewarder/app/Controllers/ShopSubscriptions.php)
- Orders (shopkeeper): [Frontend Shopkeeper Orders](file:///c:/wamp64/www/rewarder/app/Controllers/Frontend/Shopkeeper/Orders.php)

## Where Business Rules Live

The critical “offer + points” computation currently lives in the shopkeeper order creation flow:

- Discount selection and reward selection are implemented in `Frontend\Shopkeeper\Orders::save()`, using helper methods for:
  - `calculateDiscount(...)`
  - `calculatePoints(...)`

Wallet and ledger updates are performed in the same transaction scope after order creation.

## Environments and Configuration

- CI4 uses `.env` for per-environment settings (base URL, database connection, etc.).
- Do not commit secrets (database passwords, API keys) into the repository.
