# Supplier URL Configuration Guide

## Overview

The supplier websites in this application support dynamic URL generation based on the deployment environment:

- **Local Development**: `http://127.0.0.1:8080/web/{supplier-slug}`
- **Production**: `http://{supplier-slug}.archimarketplace.com`

## Configuration

### Local Development Setup

In your `.env` file, **do NOT set `SUPPLIER_DOMAIN`**:

```env
# .env (Local)
APP_ENV=local
APP_URL=http://localhost:8080

# Keep SUPPLIER_DOMAIN commented out or not set
# This ensures URLs use path-based routing: /web/{subdomain}
```

### Production Setup

In your production `.env` file, **set `SUPPLIER_DOMAIN`**:

```env
# .env.production
APP_ENV=production
APP_URL=https://archimarketplace.com

# Set the supplier domain for subdomain routing
SUPPLIER_DOMAIN=archimarketplace.com
```

## How It Works

### Helper Functions & Methods

1. **`supplier_url($subdomain, $path = '/')`** - Global helper function in `app/helpers.php`
   - Generates the correct URL based on `SUPPLIER_DOMAIN` environment variable
   - Used for generating URLs in emails, redirects, and API responses

2. **`$website->getUrl($path = '')`** - Model method in `SupplierWebsite`
   - Wrapper around `supplier_url()` that uses the model's subdomain
   - Example: `$website->getUrl('/products')` → `/web/{subdomain}/products` (local) or `/{subdomain}.archimarketplace.com/products` (prod)

3. **`$website->getDisplayUrl()`** - Model method in `SupplierWebsite`
   - Returns display-friendly URL for UI
   - Shows the full domain/path without scheme (e.g., `127.0.0.1:8080/web/my-store`)
   - Used in footer text, email footers, and settings pages

## Routing

### Local Path-Based Routes
```
GET /web/{subdomain}                      → Show supplier site home
GET /web/{subdomain}/products             → Show products page
GET /web/{subdomain}/categories           → Show categories
GET /web/{subdomain}/contact              → Show contact form
GET /web/{subdomain}/consultation         → Show consultation page
POST /web/{subdomain}/enquiry             → Submit site enquiry
```

### Production Subdomain Routes
```
GET {subdomain}.archimarketplace.com              → Show supplier site home
GET {subdomain}.archimarketplace.com/products    → Show products page
GET {subdomain}.archimarketplace.com/categories  → Show categories
GET {subdomain}.archimarketplace.com/contact     → Show contact form
GET {subdomain}.archimarketplace.com/consultation → Show consultation page
POST {subdomain}.archimarketplace.com/enquiry    → Submit site enquiry
```

Routes are defined in `routes/web.php` (lines 521-544).

## Updated Views

The following views have been updated to use dynamic URL generation:

- `resources/views/supplier/website-domain.blade.php` - Custom domain setup page
- `resources/views/supplier/website.blade.php` - Website overview
- `resources/views/supplier/website-outlet.blade.php` - Outlet website settings
- `resources/views/supplier/website-blog.blade.php` - Blog management
- `resources/views/supplier/website-content.blade.php` - Content editor (SEO preview)
- `resources/views/supplier/website-scripts.blade.php` - Script injection settings
- `resources/views/supplier/website-outlet-request.blade.php` - Outlet request form
- `resources/views/supplier/websites.blade.php` - Website listing
- `resources/views/crm/supplier-sites/show.blade.php` - CRM supplier site view
- `resources/views/web/_supplier-layout.blade.php` - Footer in supplier websites
- `resources/views/web/supplier.blade.php` - Public supplier page footer

## Example Usage

### In Blade Templates

```blade
{{-- Display current supplier URL --}}
{{ $website->getDisplayUrl() }}
{# Output: "127.0.0.1:8080/web/my-store" or "my-store.archimarketplace.com" #}

{{-- Generate a full link to supplier products page --}}
<a href="{{ $website->getUrl('/products') }}">View Products</a>

{{-- Generate a full link to a specific blog post --}}
<a href="{{ $website->getUrl('/blog/my-first-post') }}">Read Post</a>
```

### In Controllers/Services

```php
// Generate URL for email links
$url = supplier_url($website->subdomain_slug, '/contact');

// Generate URL using model method
$url = $website->getUrl('/products');

// Display URL for UI
$displayUrl = $website->getDisplayUrl();
```

## Migration from Hardcoded URLs

All hardcoded `archimarketplace.com` URLs in the codebase have been replaced with dynamic helpers. If you find any remaining hardcoded URLs:

1. If it's a display URL (showing user what their domain is), use `$website->getDisplayUrl()`
2. If it's a functional URL (links to pages), use `$website->getUrl($path)`
3. If you need the domain alone, use `config('archi.supplier_domain')` or the helper methods

## DNS Configuration (Production)

When a supplier requests a custom domain, they need to add a DNS CNAME record:

```
Type:  CNAME
Name:  www
Value: {subdomain_slug}.archimarketplace.com
```

This is handled by the custom domain system in `resources/views/supplier/website-domain.blade.php`.

## Testing

To test locally:
1. Ensure `.env` does NOT have `SUPPLIER_DOMAIN` set
2. Visit `http://127.0.0.1:8080/web/my-store-slug`
3. Check that all links and display URLs show the `/web/` path format

To test production URLs locally:
1. Temporarily add `SUPPLIER_DOMAIN=archimarketplace.com` to `.env`
2. Visit `http://localhost:8080/` (subdomain routing won't work locally without DNS setup)
3. Links should show `.archimarketplace.com` format
4. Don't forget to remove `SUPPLIER_DOMAIN` from `.env` after testing!
