# Market Basket

Market Basket is a local farmers-market marketplace that helps shoppers discover nearby producers, browse seasonal inventory, place orders, and choose market pickup or local delivery. Vendors can manage products, availability, orders, and pickup schedules from a simple dashboard.

## Features

### For shoppers

- Browse products by market, vendor, category, and availability
- Search for produce, baked goods, dairy, meat, pantry items, and more
- View producer profiles, farming practices, and pickup locations
- Add items from multiple vendors to a single cart
- Select market pickup or local delivery when available
- Pay securely online and receive order-status notifications
- Review previous orders and quickly reorder common items

### For vendors

- Create a storefront and manage business information
- Add products, photos, prices, units, and inventory
- Schedule availability by market date
- Configure pickup windows and delivery zones
- Review, fulfill, and cancel orders
- Export order and payout reports

## Requirements

- Node.js 20 or later
- npm 10 or later
- PostgreSQL 15 or later
- Redis 7 or later

Optional integrations are required for online payments, transactional email, and file storage.

## Installation

Clone the repository and install dependencies:

```bash
git clone https://github.com/example/market-basket.git
cd market-basket
npm install
```

Create a local environment file:

```bash
cp .env.example .env
```

Start PostgreSQL and Redis, then initialize the database:

```bash
npm run db:migrate
npm run db:seed
```

Run the development server:

```bash
npm run dev
```

The application is available at `http://localhost:3000`.

The seed command creates sample markets, vendors, products, and development accounts. Credentials are printed after seeding.

## Usage

### Shopper workflow

1. Create an account or continue as a guest.
2. Select a nearby market or enter a delivery postal code.
3. Browse available products and add items to the cart.
4. Choose an eligible pickup window or delivery option.
5. Complete checkout and review the confirmation email.
6. Track the order from the account dashboard.

### Vendor workflow

1. Sign in with a vendor account.
2. Complete the storefront profile.
3. Add products and set inventory for upcoming market dates.
4. Review incoming orders from the vendor dashboard.
5. Mark orders as packed and fulfilled at pickup or delivery.

### Common commands

```bash
npm run dev          # Start the development server
npm run build        # Create a production build
npm start            # Run the production server
npm test             # Run the test suite
npm run lint         # Check code style
npm run db:migrate   # Apply pending database migrations
npm run db:seed      # Load development seed data
npm run db:reset     # Recreate and seed the local database
```

## Configuration

Configuration is loaded from environment variables in `.env`.

```dotenv
# Application
NODE_ENV=development
APP_URL=http://localhost:3000
PORT=3000
SESSION_SECRET=replace-with-a-long-random-value

# Database and cache
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/market_basket
REDIS_URL=redis://localhost:6379

# Payments
STRIPE_SECRET_KEY=
STRIPE_WEBHOOK_SECRET=
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=

# Email
SMTP_HOST=localhost
SMTP_PORT=1025
SMTP_USER=
SMTP_PASSWORD=
EMAIL_FROM="Market Basket <orders@example.test>"

# File storage
STORAGE_DRIVER=local
STORAGE_BUCKET=
STORAGE_REGION=
STORAGE_ACCESS_KEY_ID=
STORAGE_SECRET_ACCESS_KEY=

# Marketplace settings
DEFAULT_CURRENCY=CAD
ORDER_CUTOFF_HOURS=18
PLATFORM_FEE_PERCENT=5
DELIVERY_RADIUS_KM=20
```

### Required variables

- `DATABASE_URL`: PostgreSQL connection string
- `REDIS_URL`: Redis connection string
- `SESSION_SECRET`: Random secret used to sign sessions
- `APP_URL`: Public application URL

### Payment webhooks

To test Stripe payments locally, forward webhook events to the application:

```bash
stripe listen --forward-to localhost:3000/api/webhooks/stripe
```

Copy the webhook signing secret into `STRIPE_WEBHOOK_SECRET`, then restart the development server.

### Storage

`STORAGE_DRIVER=local` stores uploaded product images on the local filesystem and is intended for development only. In production, configure an S3-compatible bucket and provide the corresponding storage variables.

### Production

Before deploying:

- Set `NODE_ENV=production`
- Use a strong, unique `SESSION_SECRET`
- Configure TLS for PostgreSQL and Redis
- Provide production payment and email credentials
- Configure persistent object storage
- Run `npm run db:migrate` during deployment
- Set `APP_URL` to the public HTTPS URL

## License

Licensed under the MIT License.