﻿# Deployment Guide — সুন্না ঘর (Sunnaghar)

> **Architecture**: Next.js 14 → Firebase App Hosting | Laravel 11 → Railway.app

---

## Prerequisites

```bash
# Install CLIs (run once)
npm install -g firebase-tools
npm install -g @railway/cli
```

---

## Step 1 — Deploy Laravel Admin Panel to Railway

### 1.1 Create a Railway Project

```bash
cd admin-panel
railway login
railway init          # Choose "Empty Project", give name "sunnaghar-admin"
```

### 1.2 Set Environment Variables in Railway Dashboard

Go to Railway project → **Variables** → add:

| Variable | Value |
|---|---|
| `APP_NAME` | সুন্না ঘর |
| `APP_ENV` | production |
| `APP_DEBUG` | false |
| `APP_KEY` | *(generate: `php artisan key:generate --show`)* |
| `APP_URL` | https://your-service.up.railway.app |
| `DB_CONNECTION` | sqlite |
| `FRONTEND_URL` | https://sunnaghar.web.app *(your Firebase URL)* |
| `LOG_CHANNEL` | stderr |

> Generate APP_KEY locally: `cd admin-panel && php artisan key:generate --show`

### 1.3 Deploy

```bash
# From admin-panel/ directory
railway up
```

Railway detects the Dockerfile and builds automatically. The startup script runs:
- `php artisan migrate --force`
- Seeds: AdminSeeder → CategorySeeder → ProductSeeder → SiteSettingSeeder
- Caches config/routes/views
- Starts PHP-FPM + nginx via supervisord

### 1.4 Get Your Railway URL

After deploy, copy the URL shown:
`https://sunnaghar-admin-production.up.railway.app`

### 1.5 Test the API

```bash
curl https://YOUR-RAILWAY-URL.up.railway.app/api/v1/settings
curl https://YOUR-RAILWAY-URL.up.railway.app/api/v1/products
```

---

## Step 2 — Configure Next.js for Production

Create `.env.local` in project root:

```env
NEXT_PUBLIC_API_URL=https://YOUR-RAILWAY-URL.up.railway.app/api/v1
NEXT_PUBLIC_APP_URL=https://sunnaghar.web.app
NEXT_PUBLIC_GOOGLE_CLIENT_ID=YOUR_GOOGLE_CLIENT_ID
```

---

## Step 3 — Deploy Next.js to Firebase App Hosting

### 3.1 Create Firebase Project

1. Go to [console.firebase.google.com](https://console.firebase.google.com)
2. Add project → name it (e.g. `sunnaghar-prod`)
3. Copy the **Project ID**

### 3.2 Update .firebaserc

```json
{
  "projects": {
    "default": "YOUR-ACTUAL-FIREBASE-PROJECT-ID"
  }
}
```

### 3.3 Enable Firebase App Hosting

```bash
firebase login
firebase init apphosting
```

When prompted: select project, region **asia-south1**, backend ID `sunnaghar`, branch `main`.

### 3.4 Set Secret Environment Variables

```bash
firebase apphosting:secrets:set NEXT_PUBLIC_API_URL
# Enter: https://YOUR-RAILWAY-URL.up.railway.app/api/v1

firebase apphosting:secrets:set NEXT_PUBLIC_GOOGLE_CLIENT_ID
# Enter: your Google OAuth client ID
```

### 3.5 Deploy

```bash
firebase deploy --only apphosting
```

---

## Step 4 — Final Configuration

### Update CORS in Railway

Set `FRONTEND_URL=https://YOUR-PROJECT-ID.web.app` and redeploy: `railway up`

### Update Google OAuth Redirect URIs

In [Google Cloud Console](https://console.cloud.google.com) → APIs & Services → Credentials:
- **Authorized JavaScript origins**: add your Firebase URL
- **Authorized redirect URIs**: add your Firebase URL

### Access Admin Panel

```
https://YOUR-RAILWAY-URL.up.railway.app/admin
```

Default credentials (change immediately):
- Email: `admin@sunnaghar.com`
- Password: `Admin@12345`

---

## Re-Seeding Database

The startup script seeds automatically on first deploy.

To re-seed manually (Railway):
```bash
railway run php artisan migrate:fresh --seed      # WARNING: drops all data
# OR (safe — only re-seeds products/categories):
railway run php artisan db:seed --class=CategorySeeder --force
railway run php artisan db:seed --class=ProductSeeder --force
```

---

## Deployment Checklist

- [ ] Railway deploy successful, `/api/v1/products` returns Islamic products
- [ ] `.env.local` created with Railway API URL
- [ ] `.firebaserc` updated with real Firebase project ID
- [ ] Firebase App Hosting initialized and deployed
- [ ] `FRONTEND_URL` updated in Railway Variables
- [ ] Google OAuth redirect URIs updated
- [ ] Admin password changed from default
- [ ] Product listing shows Islamic products (not clothing)
- [ ] Flash sale, hero slider, collections page all working

---

## Troubleshooting

| Problem | Solution |
|---|---|
| API returns 500 | `railway logs` — check for DB or config errors |
| Images not loading | Verify hostname in `next.config.mjs` remotePatterns |
| CORS error in browser | `FRONTEND_URL` in Railway must exactly match Firebase origin |
| Products show clothing items | Re-seed: `railway run php artisan db:seed --class=ProductSeeder --force` |
| Firebase build fails | Check `apphosting.yaml` has the env variable names listed |
| Google sign-in fails | Add Firebase URL to OAuth authorized origins in Google Console |
