# Client Portal - Will Format Display Fix

## Problem
The client portal was showing all will types (individual, islamic, joint, trust) in the same generic format. Clients should see will types in their appropriate formats, just like admin does.

## Solution Implemented

### 1. Controller Method Routing (ClientController.php)
Updated the `viewWill()` method to route to type-specific display methods:

```php
// Route to different views based on will type (matching admin format)
if ($will->will_type === 'islamic' || $will->will_type === 'islamic_will') {
    return $this->viewIslamicWill($will, $client, $step, $onboardingStep);
}

if ($will->will_type === 'trust') {
    return $this->viewTrustWill($will, $client, $step, $onboardingStep);
}

if ($will->will_type === 'joint') {
    return $this->viewJointWill($will, $client, $step, $onboardingStep);
}

// Default to individual/standard will view
return $this->viewIndividualWill($will, $client, $step, $onboardingStep);
```

**Location:** `app/Http/Controllers/ClientController.php` - viewWill method

### 2. Type-Specific Display Methods

#### A. Individual Will Format
```php
private function viewIndividualWill($will, $client, $step, $onboardingStep)
```
- Shows standard individual will sections
- Testator, beneficiaries, assets, liabilities, witnesses
- Uses existing `client.will.view` template

#### B. Islamic Will Format
```php
private function viewIslamicWill($will, $client, $step, $onboardingStep)
```
- Shows Islamic-specific sections
- Testator, Islamic Info, Wasi, Funeral Instructions, Wasiyyah, Hibah
- Uses new `client.will.islamic-view` template

#### C. Joint Will Format
```php
private function viewJointWill($will, $client, $step, $onboardingStep)
```
- Shows joint testator details
- Both primary and secondary testator information
- Uses new `client.will.joint-view` template

#### D. Trust Will Format
```php
private function viewTrustWill($will, $client, $step, $onboardingStep)
```
- Shows trust-specific sections
- Trust creator, trustees, beneficiaries
- Uses new `client.will.trust-view` template

### 3. Document Rendering Feature

Added two new methods to allow clients to view and download properly formatted will documents (using admin's DocumentTemplateRenderer):

#### A. Render Will Document
```php
public function renderWillDocument(Will $will)
```
- Uses the same DocumentTemplateRenderer service as admin
- Renders will in professional format
- Displays in formatted document view
- Can be printed or downloaded as PDF

**Route:** `GET /will/{will}/render` named `client.will.render`

#### B. Download Will as PDF
```php
public function downloadWillPDF(Will $will)
```
- Converts rendered document to PDF
- Downloads as `Will_<id>_<date>.pdf`
- Uses DomPDF library

**Route:** `GET /will/{will}/download` named `client.will.download`

### 4. New Routes

Added to `routes/web.php` in the client middleware group:

```php
Route::get('/will/{will}/render', [ClientController::class, 'renderWillDocument'])->name('client.will.render');
Route::get('/will/{will}/download', [ClientController::class, 'downloadWillPDF'])->name('client.will.download');
```

### 5. New Views Created

#### A. client/will/islamic-view.blade.php
- Displays Islamic will with proper sections
- Shows Wasi, Religious Obligations, Funeral Instructions, Wasiyyah, Hibah
- Professional Islamic will layout

#### B. client/will/rendered-document.blade.php
- Shows professionally rendered will document
- Includes Print and Download buttons
- Professional formatting with typography
- Print-friendly styling

## How It Works

### User Flow:

1. **Client Navigates to Will:**
   - Clicks on a will from their dashboard
   - Route: `GET /will/{will}`

2. **Route Detection:**
   - `viewWill()` method receives the will
   - Checks `$will->will_type`

3. **Format-Specific Display:**
   - Islamic wills → Islamic format view
   - Trust wills → Trust format view
   - Joint wills → Joint format view
   - Individual wills → Individual format view (default)

4. **Document Rendering (Optional):**
   - Client can click "View Rendered Document"
   - Route: `GET /will/{will}/render`
   - Displays professionally formatted document
   - Can print or download as PDF

## Files Modified/Created

### Modified Files:
1. `app/Http/Controllers/ClientController.php`
   - Updated `viewWill()` method
   - Added 4 type-specific display methods
   - Added `renderWillDocument()` method
   - Added `downloadWillPDF()` method

2. `routes/web.php`
   - Added two new routes for document rendering/download

### New Files Created:
1. `resources/views/client/will/islamic-view.blade.php`
   - Islamic will display template

2. `resources/views/client/will/rendered-document.blade.php`
   - Professional document rendering view

## Benefits

1. **Consistent UX:** Clients see same formats as admin
2. **Type-Appropriate Display:** Each will type shows relevant sections
3. **Professional Documents:** Can render and download formatted documents
4. **Print-Friendly:** Documents can be printed directly from browser
5. **Secure:** Authorization checks ensure clients only see their own wills
6. **Maintainable:** Reuses existing DocumentTemplateRenderer service

## Testing Checklist

- [ ] Create or find an Islamic will owned by logged-in client
- [ ] Navigate to client will view
- [ ] Verify Islamic format displays (Wasi, Religious Obligations, etc.)
- [ ] Click "View Rendered Document" button (if available)
- [ ] Verify professional document renders without errors
- [ ] Click "Download PDF" - verify PDF downloads
- [ ] Click "Print" - verify print dialog appears
- [ ] Create/find trust will - verify trust format displays
- [ ] Create/find joint will - verify joint format displays
- [ ] Create/find individual will - verify individual format displays
- [ ] Verify unauthorized users cannot access other clients' wills

## Security Notes

- All methods check `$will->client_id !== $client->id` for authorization
- Clients can only view their own wills
- Document rendering respects existing will authorization
- Routes protected by auth middleware

## Future Enhancements

- Add email delivery of rendered documents
- Add version history of rendered documents
- Add digital signature support
- Add document sharing with specific users
- Add document watermarking for draft documents
