# AI Planning Files – E-commerce Module

This document contains structured `.md` files content for:
- Rules
- Database
- Relationships
- Tasks (step-by-step low-level execution)

---

# 1. rules.md

```md
# System Rules

## General Rules
- System must support digital, physical, and hybrid products
- Each product belongs to ONE category only
- Categories support unlimited nesting (tree structure)

## Product Rules
- Product has two independent types: Digital and Physical
- Each type has its own price
- Product can have:
  - digital_price (nullable)
  - physical_price (nullable)
- If both exist → user can choose either or both as separate items

## Cart Rules
- Each cart item represents a specific product type (digital OR physical)
- Digital and Physical are treated as separate items in cart
- Each item stores:
  - product_id
  - type (digital / physical)
  - price_snapshot
  - quantity (only for physical)

- Each cart item must store selected_type
- Quantity only applies to physical products (future-safe)

## Order Rules
- Order status is updated manually by admin
- Status flow:
  - pending → preparing → on_the_way → delivered → completed

## User Rules
- User can have multiple addresses
- One address can be default

## Digital Access Rules
- Granted ONLY after order is completed
- Unlimited downloads (for now)

## Scalability Rules
- All logic should be service-based
- APIs must be ready for mobile apps
```

---

# 2. database.md

```md
# Database Structure

## categories
- id
- name
- description
- image
- parent_id

## products
- id
- name
- description
- main_image
- digital_price (nullable)
- physical_price (nullable)
- category_id
- created_at
- updated_at
- is_active
- file_url (nullable)
- file_path (nullable)

## product_files
- id
- product_id
- file_path

## users
- id
- name
- email
- password

## addresses
- id
- user_id
- title
- address_line
- city
- notes
- is_default

## carts
- id
- user_id

## cart_items
- id
- cart_id
- product_id
- type (digital / physical)
- price
- quantity

## orders
- id
- user_id
- address_id
- status
- total_price

## order_items
- id
- order_id
- product_id
- type
- price
- quantity

## digital_access
- id
- user_id
- product_id
- order_id
```

---

# 3. relations.md

```md
# Relationships

Category
- hasMany Products
- belongsTo Parent Category

Product
- belongsTo Category
- hasMany ProductFiles
- hasMany OrderItems

User
- hasMany Addresses
- hasMany Orders
- hasMany DigitalAccess

Cart
- belongsTo User
- hasMany CartItems

Order
- belongsTo User
- belongsTo Address
- hasMany OrderItems

OrderItem
- belongsTo Order
- belongsTo Product

DigitalAccess
- belongsTo User
- belongsTo Product
- belongsTo Order
```

---

# 4. tasks/ (Structured using TASK_TEMPLATE)

## Task 01 – Setup Database

```md
# Task 01: Setup Database

## Goal
Prepare full database structure for e-commerce module.

## Database
All tables:
- categories
- products
- product_files
- users
- addresses
- carts
- cart_items
- orders
- order_items
- digital_access

## Relations
- categories → products
- products → product_files
- users → orders

## Files
- database/migrations/*

## Permissions
- admin: full access

## UI
- No UI required

## Steps
Step 1 – Migration
Step 2 – Model
```

---

## Task 02 – Models & Relations

```md
# Task 02: Models & Relations

## Goal
Create Eloquent models and define relationships.

## Database
Use existing tables

## Relations
- Category hasMany Products
- Product belongsTo Category
- Order hasMany Items

## Files
- app/Models/*

## Permissions
- system internal

## UI
- No UI

## Steps
Step 1 – Model
Step 2 – Relations
```

---

## Task 03 – Category Tree

```md
# Task 03: Category Tree

## Goal
Build recursive category system.

## Database
- categories

## Relations
- self relation (parent_id)

## Files
- CategoryService.php
- CategoryController.php

## Permissions
- admin manage
- user read

## UI
- Tree view categories

## Steps
Step 1 – Service
Step 2 – Controller
Step 3 – Views
```

---

## Task 04 – Product CRUD

```md
# Task 04: Product CRUD

## Goal
Manage products (create/update/delete)

## Database
- products
- product_files

## Relations
- Product → Category
- Product → Files

## Files
- ProductController.php
- ProductService.php

## Permissions
- admin full control

## UI
- Product form

## Steps
Step 1 – Migration (if needed)
Step 2 – Model
Step 3 – Service
Step 4 – Controller
Step 5 – Views
```

---

## Task 05 – Cart System

```md
# Task 05: Cart System

## Goal
Allow users to add/remove/update cart items.

## Database
- carts
- cart_items

## Relations
- Cart → Items

## Files
- CartService.php
- CartController.php

## Permissions
- user only

## UI
- Cart page

## Steps
Step 1 – Service
Step 2 – Controller
Step 3 – Views
```

---

## Task 06 – Checkout

```md
# Task 06: Checkout

## Goal
Create order from cart.

## Database
- orders
- order_items

## Relations
- Order → Items

## Files
- CheckoutService.php
- OrderController.php

## Permissions
- user

## UI
- Checkout page

## Steps
Step 1 – Service
Step 2 – Controller
Step 3 – Requests
Step 4 – Views
```

---

## Task 07 – Orders Management

```md
# Task 07: Orders

## Goal
Track and update orders.

## Database
- orders

## Relations
- Order → User

## Files
- OrderController.php

## Permissions
- admin update
- user view

## UI
- Orders list

## Steps
Step 1 – Controller
Step 2 – Views
```

---

## Task 08 – Digital Library

```md
# Task 08: Digital Library

## Goal
Provide access to digital products.

## Database
- digital_access

## Relations
- User → DigitalAccess

## Files
- DigitalLibraryController.php

## Permissions
- user

## UI
- Library page

## Steps
Step 1 – Service
Step 2 – Controller
Step 3 – Views
```

---

# End of Files

