# Data Model

This is a functional data dictionary for the Rewarder domain tables.

## Shops and Users

### rewarder_shops

- Primary key: `id`
- Key fields: `name`, `slug` (unique), `phone`, `address`, `status`
- Optional extensions (added by later migrations): `city_id`, `category_id`, `img_type`

### rewarder_shop_users

Maps platform users to shops.

- Primary key: `id`
- Keys: unique composite on (`shop_id`, `user_id`)
- Fields: `role` (default `OWNER`)

## Subscription Plans

### rewarder_subscriptions

- Primary key: `id`
- Fields: `title`, `description`, `price`, `duration_days`, `status`

### rewarder_shop_subscriptions

Assigns a subscription plan to a shop.

- Primary key: `id`
- Fields: `shop_id`, `subscription_id`, `start_at`, `end_at`, `status` (default `ACTIVE`)

## Catalog (Products and Services)

### rewarder_product_categories

- Primary key: `id`
- Fields: `shop_id`, `name`, `status`

### rewarder_products

- Primary key: `id`
- Fields: `shop_id`, `category_id` (nullable), `name`, `sku` (nullable), `price`, `status`

### rewarder_service_categories

- Primary key: `id`
- Fields: `shop_id`, `name`, `status`

### rewarder_services

- Primary key: `id`
- Fields: `shop_id`, `category_id` (nullable), `name`, `price`, `status`

## Offers and Rewards

### rewarder_discounts

- Primary key: `id`
- Fields:
  - `shop_id`
  - `applies_to` (default `ALL`)
  - `target_id` (nullable)
  - `discount_type` (default `PERCENT`)
  - `value`
  - `min_amount` (nullable)
  - `start_at`, `end_at` (nullable)
  - `status`

### rewarder_reward_rules

- Primary key: `id`
- Fields:
  - `shop_id`
  - `applies_to` (default `ALL`)
  - `target_id` (nullable)
  - `points_type` (default `PERCENT`)
  - `value`
  - `start_at`, `end_at` (nullable)
  - `status`

## Orders

### rewarder_orders

- Primary key: `id`
- Fields:
  - `shop_id`, `customer_id`
  - `subtotal`, `discount_total`, `total`
  - `points_earned`, `points_redeemed`
  - `status` (default `COMPLETED`)
  - `created_by`, `created_at`

### rewarder_order_items

- Primary key: `id`
- Fields:
  - `order_id`
  - `item_type` (`PRODUCT` or `SERVICE`)
  - `item_id`
  - `name`, `quantity`, `unit_price`
  - `discount_amount`, `line_total`
  - `points_earned`

## Wallet and Ledger

### rewarder_wallets

- Primary key: `user_id` (one wallet per user)
- Fields: `points_balance`, `updated_at`

### rewarder_ledger

Point transaction log.

- Primary key: `id`
- Fields:
  - `user_id`, `shop_id`
  - `order_id` (nullable)
  - `points_delta` (positive earn, negative redeem)
  - `description`, `created_at`

## Locations and Shop Categorization

### rewarder_countries / rewarder_states / rewarder_cities

Locations hierarchy used for filtering shops on the landing page.

- Countries: `id`, `name`, `status`
- States: `id`, `country_id`, `name`, `status`
- Cities: `id`, `state_id`, `name`, `status`

### rewarder_shop_categories

- Primary key: `id`
- Fields: `name`, `status`
- Used by shops via `rewarder_shops.category_id`

## Platform Auth / RBAC (Non-Rewarder Tables)

The base system includes:

- `users`, `roles`, `permissions`, `role_permissions`
- `settings`, `email_templates`, `activity_logs`

Rewarder migrations add the **Shopkeeper** and **Customer** roles and assign initial permissions for shopkeeper/customer features.
