# Simple Deployment Guide - Keep Current Structure

This guide maintains your `backend` and `frontend` folder structure, making updates easier.

## Deployment Structure (Simple)

```
public_html/
├── backend/              ← All Laravel files
│   ├── app/
│   ├── public/          ← Laravel's public folder
│   │   └── index.php
│   ├── .env
│   └── ...
│
└── frontend/            ← Built frontend files
    ├── index.html
    ├── css/
    ├── js/
    └── ...
```

---

## Step 1: Deploy Backend

### Upload Backend Files

1. **Zip your backend folder** (entire folder)
2. **Upload to** `public_html/backend/`
3. **Extract** the files

### Configure Backend

1. **Create/Edit** `public_html/backend/.env`:
   ```env
   APP_NAME="Aria Herat ERP"
   APP_ENV=production
   APP_DEBUG=false
   APP_URL=https://yourdomain.com/backend/public
   FRONTEND_URL=https://yourdomain.com/frontend
   
   DB_CONNECTION=mysql
   DB_HOST=localhost
   DB_DATABASE=your_database_name
   DB_USERNAME=your_db_user
   DB_PASSWORD=your_db_password
   
   SESSION_DOMAIN=yourdomain.com
   SANCTUM_STATEFUL_DOMAINS=yourdomain.com,www.yourdomain.com
   ```

2. **Run setup commands** (via SSH or cPanel Terminal):
   ```bash
   cd public_html/backend
   composer install --optimize-autoloader --no-dev
   php artisan key:generate
   php artisan migrate --force
   php artisan config:cache
   php artisan route:cache
   chmod -R 755 storage bootstrap/cache
   ```

3. **Test backend**:
   - Visit: `https://yourdomain.com/backend/public/`
   - Should see Laravel welcome or API response

---

## Step 2: Deploy Frontend

### Update Frontend API URL

Before building, update your axios configuration:

**File**: `frontend/src/boot/axios.js`

```javascript
// Change this line:
axios.defaults.baseURL = 'https://yourdomain.com/backend/public/api'

// Or use environment variable
axios.defaults.baseURL = process.env.API_URL || 'https://yourdomain.com/backend/public/api'
```

### Build Frontend

```bash
cd frontend
npm run build
```

### Upload Frontend

⚠️ **Important**: Upload **contents** of `dist/spa/`, not the folders!

1. **Go inside** `frontend/dist/spa/` on your computer
2. **Select all files** (index.html, css/, js/, fonts/, icons/, etc.)
3. **Create a zip**: `frontend-build.zip`
4. **Upload to** `public_html/frontend/`
5. **Extract** in that folder
6. **Verify** `index.html` is at `public_html/frontend/index.html`

---

## Step 3: Configure Access

### Option A: Access via Subfolders (Simplest)

Just access directly:
- **Frontend**: `https://yourdomain.com/frontend/`
- **Backend**: `https://yourdomain.com/backend/public/`

✅ **No configuration needed!**

### Option B: Frontend at Root (Better)

If you want `https://yourdomain.com/` to show frontend:

**Create** `public_html/.htaccess`:

```apache
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Redirect root to frontend
    RewriteRule ^$ /frontend/ [L]
    
    # Redirect backend API requests
    RewriteCond %{REQUEST_URI} ^/api/
    RewriteRule ^api/(.*)$ /backend/public/api/$1 [QSA,L]
    
    # Frontend routes (if accessed from root)
    RewriteCond %{REQUEST_URI} !^/(backend|frontend)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /frontend/$1 [L]
</IfModule>
```

**OR create** `public_html/index.html` (redirect file):

```html
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="refresh" content="0; url=/frontend/">
    <script>window.location.href='/frontend/';</script>
</head>
<body>Redirecting...</body>
</html>
```

---

## Step 4: Update CORS (Backend)

**Edit**: `backend/config/cors.php`

```php
return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],
    
    'allowed_origins' => [
        'https://yourdomain.com',
        'https://www.yourdomain.com',
    ],
    
    'supports_credentials' => true,
    
    // ... rest of config
];
```

**After editing**, run:
```bash
cd public_html/backend
php artisan config:cache
```

---

## Quick Update Process (After First Deploy)

### Update Backend Only:
1. Make changes locally
2. Zip `backend` folder
3. Upload and extract to `public_html/backend/`
4. Run: `php artisan config:cache`

### Update Frontend Only:
1. Make changes locally
2. Update API URL if changed
3. Run: `npm run build`
4. Upload contents of `dist/spa/` to `public_html/frontend/`

### Update Both:
1. Upload backend → `public_html/backend/`
2. Build frontend → Upload to `public_html/frontend/`

---

## Testing Your Deployment

### ✅ Check Backend:
```
Visit: https://yourdomain.com/backend/public/
Should see: Laravel response

Visit: https://yourdomain.com/backend/public/api/health
Should see: API response (if route exists)
```

### ✅ Check Frontend:
```
Visit: https://yourdomain.com/frontend/
Should see: Aria Herat ERP login page

Check browser console (F12):
- No 404 errors
- API calls go to /backend/public/api/
```

### ✅ Test Login:
```
1. Open: https://yourdomain.com/frontend/#/login
2. Try to login
3. Check if API calls work
4. Check Network tab (F12) for API responses
```

---

## Alternative: Subdomain Deployment

If your host supports subdomains, this is cleaner:

### Setup:
- **Frontend**: `https://app.yourdomain.com` → points to `public_html/frontend/`
- **Backend**: `https://api.yourdomain.com` → points to `public_html/backend/public/`

### Advantages:
- Cleaner URLs
- Easier to manage
- Better separation

### Setup in cPanel:
1. Create subdomain: `app` → points to `public_html/frontend/`
2. Create subdomain: `api` → points to `public_html/backend/public/`
3. Update frontend axios baseURL to: `https://api.yourdomain.com/api`
4. Update backend .env:
   ```
   APP_URL=https://api.yourdomain.com
   FRONTEND_URL=https://app.yourdomain.com
   ```

---

## Troubleshooting

### Frontend shows blank page
- Check browser console (F12)
- Verify API URL is correct
- Check if files uploaded correctly

### API CORS errors
- Update `backend/config/cors.php` with your domain
- Run: `php artisan config:cache`
- Clear browser cache

### 500 Error on backend
```bash
cd public_html/backend
chmod -R 755 storage bootstrap/cache
php artisan cache:clear
php artisan config:cache
```

### Login not working
- Check `.env` has correct `FRONTEND_URL`
- Check `SANCTUM_STATEFUL_DOMAINS` includes your domain
- Check `SESSION_DOMAIN` is correct

---

## Production Checklist

Before going live:

Backend:
- [ ] `.env` has `APP_ENV=production`
- [ ] `.env` has `APP_DEBUG=false`
- [ ] Database credentials are correct
- [ ] Migrations ran successfully
- [ ] Storage permissions set (755)
- [ ] Config cached

Frontend:
- [ ] API URL points to production backend
- [ ] Built with `npm run build`
- [ ] All files uploaded correctly
- [ ] Can access login page
- [ ] API calls work

Both:
- [ ] HTTPS enabled (SSL certificate)
- [ ] CORS configured correctly
- [ ] Session/Sanctum domains set

---

## Summary

**Your URLs:**
- Frontend: `https://yourdomain.com/frontend/` or `https://yourdomain.com/`
- Backend: `https://yourdomain.com/backend/public/api/`

**Folder structure:**
```
public_html/
├── backend/
│   └── public/
│       └── index.php
└── frontend/
    └── index.html
```

**To update:** Just upload new files to respective folders!
