# Market Basket

Market Basket is a local farmers-market marketplace that connects shoppers with nearby growers, bakers, and food producers. Customers can browse seasonal products, place pickup orders, and manage favorites, while vendors can maintain inventory and prepare orders for upcoming market days.

## Features

- Browse products by market, vendor, category, and availability
- Search for produce, baked goods, dairy, pantry items, and more
- View vendor profiles, growing practices, and pickup locations
- Add products from multiple vendors to a single basket
- Select an available market-day pickup window
- Create an account and review previous orders
- Save favorite vendors and products
- Vendor dashboard for products, inventory, and order fulfillment
- Responsive interface for desktop and mobile devices

## Tech Stack

- Node.js 20+
- TypeScript
- Next.js
- PostgreSQL
- Prisma ORM
- Stripe Checkout

## Installation

### Prerequisites

Install the following before getting started:

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

### Setup

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.local
```

Update the database connection and other required values in `.env.local`, then initialize the database:

```bash
npx prisma migrate dev
npm run db:seed
```

Start the development server:

```bash
npm run dev
```

Open [http://localhost:3000](http://localhost:3000) in your browser.

## Usage

### Customer workflow

1. Choose a participating farmers market.
2. Browse products available for the next market day.
3. Add items to your basket.
4. Select a pickup window and complete checkout.
5. Present the order confirmation to vendors at pickup.

### Vendor workflow

1. Sign in with a vendor account.
2. Create products and set prices, quantities, and availability.
3. Review incoming orders from the vendor dashboard.
4. Mark orders as prepared before the market opens.
5. Confirm each order when the customer collects it.

Seeded development accounts are listed in the terminal after running `npm run db:seed`.

### 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:seed      # Load development data
npm run db:studio    # Open Prisma Studio
```

## Configuration

Market Basket is configured with environment variables in `.env.local`.

| Variable | Required | Description |
| --- | --- | --- |
| `DATABASE_URL` | Yes | PostgreSQL connection string |
| `NEXTAUTH_URL` | Yes | Public application URL |
| `NEXTAUTH_SECRET` | Yes | Secret used to sign authentication sessions |
| `STRIPE_SECRET_KEY` | Yes | Stripe server-side API key |
| `STRIPE_WEBHOOK_SECRET` | Yes | Secret for validating Stripe webhook events |
| `NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY` | Yes | Stripe browser API key |
| `NEXT_PUBLIC_DEFAULT_MARKET_ID` | No | Market selected for first-time visitors |
| `ORDER_CUTOFF_HOURS` | No | Hours before opening when ordering closes; defaults to `12` |
| `PLATFORM_FEE_PERCENT` | No | Marketplace fee percentage; defaults to `5` |
| `EMAIL_FROM` | No | Sender address for order notifications |
| `SMTP_URL` | No | SMTP connection URL for transactional email |

Example:

```dotenv
DATABASE_URL="postgresql://market_basket:password@localhost:5432/market_basket"
NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="replace-with-a-random-secret"

STRIPE_SECRET_KEY="sk_test_..."
STRIPE_WEBHOOK_SECRET="whsec_..."
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY="pk_test_..."

NEXT_PUBLIC_DEFAULT_MARKET_ID="downtown-saturday"
ORDER_CUTOFF_HOURS="12"
PLATFORM_FEE_PERCENT="5"
```

Generate a suitable authentication secret with:

```bash
openssl rand -base64 32
```

For local Stripe webhook testing, forward events to the application:

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

Copy the webhook signing secret printed by the Stripe CLI into `STRIPE_WEBHOOK_SECRET`.

## License

This project is licensed under the MIT License.