# FoamKing Setup Guide

Complete setup instructions for deploying the FoamKing flooring price calculator.

---

## 1. Prerequisites

- **Node.js** 18 or later (20 LTS recommended)
- **npm** (included with Node.js)

## 2. Installation

```bash
npm install
```

## 3. Configure Environment

Copy the example file and fill in your values:

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

Open `.env.local` in a text editor and configure each section as described below.

### Admin Login (Required)

```env
ADMIN_EMAIL=admin@example.com
ADMIN_PASSWORD=changeme
```

These credentials are used to sign in at `/intern/login`. Change both values before going to production.

### Email — Office 365 SMTP (Required)

```env
SMTP_HOST=smtp.office365.com
SMTP_PORT=587
SMTP_USER=tilbud@foamking.dk
SMTP_PASS=your_password_here
EMAIL_FROM_NAME=Foam King Prisberegner
EMAIL_TO=info@foamking.dk
```

The application sends quote-request emails through Office 365. For this to work, **SMTP AUTH must be enabled** on the sending mailbox:

1. Go to **Exchange Admin Center** — [admin.exchange.microsoft.com](https://admin.exchange.microsoft.com)
2. Navigate to **Recipients** → **Mailboxes**
3. Select the **tilbud@foamking.dk** mailbox
4. Click **Manage email apps settings** (or Mail flow → Email apps)
5. Enable **Authenticated SMTP (SMTP AUTH)**
6. Save

> **Note:** If your organization uses Security Defaults or Conditional Access policies that block legacy authentication, you may need to create an exclusion for this mailbox. If MFA is enabled on the account, use an App Password instead of the regular password.

### Base URL (Required)

```env
NEXT_PUBLIC_BASE_URL=https://beregner.foamking.dk
```

Used when generating links in outgoing emails (e.g. links to quote pages).

### Distance Calculation (Optional)

```env
OPENROUTE_API_KEY=your_api_key_here
```

Without this key the application uses a built-in postal code distance table, which is perfectly fine for most cases. If you want more precise driving distances, sign up for a free API key at [openrouteservice.org](https://openrouteservice.org/dev/#/signup) (2,000 requests per day).

## 4. Initialize Database

```bash
npm run setup
```

This creates the SQLite database and required tables.

## 5. Build and Start

```bash
npm run build
npm start
```

The application starts on **port 3001**.

## 6. Route Overview

| Route               | Description                                  |
| -------------------- | -------------------------------------------- |
| `/`                  | Public price calculator                      |
| `/tilbud/[id]`       | Public quote view (linked from email)        |
| `/intern`            | Admin dashboard — quote management           |
| `/intern/historik`   | Admin — quote history                        |
| `/intern/beregner`   | Admin — detailed calculator with price breakdown |
| `/intern/login`      | Admin login page                             |

## 7. Custom Authentication

The default authentication uses the `ADMIN_EMAIL` and `ADMIN_PASSWORD` environment variables with in-memory sessions. This is simple and sufficient for a single-admin setup.

To integrate your own authentication (JWT, OAuth, SSO), edit **`lib/auth.ts`**. The entire application relies on only three functions:

- `checkAuth()` — checks whether the current request is authenticated
- `login(email, password)` — authenticates a user and creates a session
- `logout()` — destroys the current session

The middleware in **`middleware.ts`** checks for a session cookie on all `/intern/*` routes and redirects unauthenticated visitors to `/intern/login`. Update it if you switch to a different mechanism (e.g. a JWT in an `Authorization` header).

## 8. Development

```bash
npm run dev        # Start development server (port 3001)
npm run build      # Production build
npm start          # Start production server
npm run lint       # Run ESLint
npm run setup      # Initialize / reset database
```
