# Juris Platform - Backend Setup Complete Script

## STEP 1: Install Laravel and Dependencies

```powershell
cd backend

# Create Laravel project (if not already initialized via composer create-project)
# composer create-project laravel/laravel . "11.*"

# Install dependencies
composer require laravel/sanctum:^4.0
composer require spatie/laravel-permission:^6.0
composer require stripe/stripe-php:^13.0

# Copy environment file
cp .env.example .env

# Generate application key
php artisan key:generate
```

## STEP 2: Publish Package Configurations

```powershell
# Publish Sanctum configuration
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"

# Publish Spatie Permission configuration
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
```

## STEP 3: Configure Database

Edit `.env` file:

```env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=juris_db
DB_USERNAME=root
DB_PASSWORD=
```

Create database:

```powershell
# Using MySQL command line
mysql -u root -p -e "CREATE DATABASE juris_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

# Or using HeidiSQL/phpMyAdmin/MySQL Workbench
```

## STEP 4: Run Migrations and Seed Data

```powershell
# Run all migrations
php artisan migrate

# Seed the database with default data
php artisan db:seed
```

This will create:
- Super Admin: admin@juris.local / password
- CMS Module Admin: cms.admin@juris.local / password
- CMS User: cms.user@juris.local / password
- LMS Module Admin: lms.admin@juris.local / password
- Payments Admin: payments.admin@juris.local / password
- Multi-Module User: multi.user@juris.local / password

## STEP 5: Configure Sanctum for Local Subdomains

The `.env` file should already have these values:

```env
SESSION_DOMAIN=.juris.local
SANCTUM_STATEFUL_DOMAINS=admin.juris.local:5173,lms.juris.local:5174,cms.juris.local:5175,content.juris.local:5176,immigopro.juris.local:5177,public.juris.local:5178,payments.juris.local:5179
```

## STEP 6: Start Laravel Development Server

```powershell
# Start server on all interfaces so local domains can access it
php artisan serve --host=0.0.0.0 --port=8000
```

Backend API will be available at: `http://api.juris.local:8000`

## STEP 7: Test API Endpoints

```powershell
# Health check
curl http://api.juris.local:8000/api/health

# Login (set cookie)
curl -X POST http://api.juris.local:8000/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{"email":"admin@juris.local","password":"password"}' \
  --cookie-jar cookies.txt

# Get authenticated user
curl http://api.juris.local:8000/api/v1/auth/me \
  -H "Accept: application/json" \
  --cookie cookies.txt
```

## Additional Files to Create

### Config Files

Create these configuration files if they don't exist:

**config/cors.php** - Already configured in Laravel 11
**config/sanctum.php** - Published via artisan command
**config/permission.php** - Published via artisan command

### Additional Controllers (Create as needed)

The following controllers are referenced in routes but need to be created:

1. **Admin Controllers:**
   - `app/Http/Controllers/Api/V1/Admin/UserController.php`
   - `app/Http/Controllers/Api/V1/Admin/RoleController.php`
   - `app/Http/Controllers/Api/V1/Admin/ModuleController.php`
   - `app/Http/Controllers/Api/V1/Admin/PaymentGatewayController.php`

2. **Payment Controllers:**
   - `app/Http/Controllers/Api/V1/Payments/CheckoutController.php`
   - `app/Http/Controllers/Api/V1/Payments/StripeWebhookController.php`

3. **Services:**
   - `app/Services/PaymentGatewayInterface.php`
   - `app/Services/Gateways/StripeGateway.php`

### Policy for CMS Clients

**app/Policies/Cms/ClientPolicy.php**

I will create these files next.

## Alternative Server Setup (Using Apache/Nginx)

If you prefer to use Apache or Nginx instead of `php artisan serve`:

### Option A: Laravel Valet (Windows - Acrylic DNS Proxy)

```powershell
# Install Acrylic DNS Proxy
# Download from: http://mayakron.altervista.org/wikibase/show.php?id=AcrylicHome

# Configure Acrylic to resolve *.juris.local to 127.0.0.1
# Add to AcrylicHosts.txt:
127.0.0.1 *.juris.local

# Set DNS server to 127.0.0.1 in Windows Network Settings
```

### Option B: Laragon

```powershell
# Download Laragon: https://laragon.org/download/
# Install and start Laragon
# Place backend folder in C:\laragon\www\juris\backend
# Configure virtual host for api.juris.local
```

### Option C: Manual Apache Configuration

Create `C:\wamp64\bin\apache\apache2.4.xx\conf\extra\httpd-vhosts.conf`:

```apache
<VirtualHost *:80>
    ServerName api.juris.local
    DocumentRoot "C:/path/to/app/backend/public"
    <Directory "C:/path/to/app/backend/public">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
```

## Troubleshooting

### Issue: CORS Errors

**Solution:** Ensure `SANCTUM_STATEFUL_DOMAINS` includes all frontend domains with ports.

### Issue: Session Cookie Not Set

**Solution:** 
1. Check `SESSION_DOMAIN=.juris.local`
2. Access via domain (not localhost)
3. Clear browser cookies
4. Ensure frontend calls `/sanctum/csrf-cookie` first

### Issue: 419 CSRF Token Mismatch

**Solution:**
1. Call `GET /sanctum/csrf-cookie` before login
2. Ensure `withCredentials: true` in frontend API client
3. Check `X-XSRF-TOKEN` header is sent

### Issue: Module Access Denied

**Solution:**
1. Verify user assigned to module in `module_user` table
2. Check `role_scope` is correct
3. Verify module is enabled

### Issue: Migration Errors

**Solution:**
1. Drop database and recreate: `php artisan migrate:fresh --seed`
2. Check MySQL version (8.0+ recommended)
3. Ensure proper character set: utf8mb4

## Next Steps

1. ✅ Backend API is ready
2. ⏭️ Create shared packages (ui + shared)
3. ⏭️ Scaffold React frontend apps
4. ⏭️ Implement frontend authentication flow
5. ⏭️ Build UI components with shadcn/ui
