# Contract PDF Generation Setup

The contracts app includes PDF generation capabilities using WeasyPrint. However, WeasyPrint requires system-level dependencies that need to be installed in the Docker container.

## Current Status

✅ **PDF Service Code**: Installed and ready
✅ **Celery Tasks**: Installed and functional  
✅ **HTML Templates**: Installed and ready
❌ **WeasyPrint Dependencies**: **NOT INSTALLED**

## To Enable PDF Generation

### 1. Update Docker Image

Edit `compose/local/django/Dockerfile` (or production equivalent) and add the following before pip installs:

```dockerfile
# Install WeasyPrint dependencies
RUN apt-get update && apt-get install -y \
    libpango-1.0-0 \
    libpangoft2-1.0-0 \
    libgobject-2.0-0 \
    libcairo2 \
    libpangocairo-1.0-0 \
    fonts-liberation \
    && rm -rf /var/lib/apt/lists/*
```

### 2. Add to Requirements

Add to your requirements file (e.g., `requirements/base.txt` or `requirements/local.txt`):

```
weasyprint>=67.0
```

### 3. Rebuild Container

```bash
cd /home/poduck/Desktop/smoothschedule2/smoothschedule
docker compose -f docker-compose.local.yml up -d --build django
```

### 4. Verify Installation

```bash
docker compose -f docker-compose.local.yml exec django python -c "from contracts.pdf_service import ContractPDFService; print('Available:', ContractPDFService.is_available())"
```

You should see: `Available: True`

## What Works Without WeasyPrint

Even without WeasyPrint installed, the following functionality works:

- ✅ Contract creation and management
- ✅ Contract signing workflow
- ✅ Email notifications (signing requests, reminders, confirmations)
- ✅ Signature audit trail
- ✅ All Celery tasks (except PDF generation)

The only limitation is that `generate_contract_pdf` task will fail with a clear error message indicating WeasyPrint is not available.

## Files Created

- `/app/contracts/pdf_service.py` - PDF generation service
- `/app/contracts/tasks.py` - Celery tasks for contracts
- `/app/templates/contracts/pdf_template.html` - PDF template
- `/app/templates/contracts/emails/` - Email templates (8 files)

## Testing PDF Generation

Once WeasyPrint is installed, you can test PDF generation:

```python
from contracts.models import Contract
from contracts.pdf_service import ContractPDFService

# Get a signed contract
contract = Contract.objects.filter(status='SIGNED').first()

# Generate PDF
pdf_bytes = ContractPDFService.generate_pdf(contract)

# Or save to storage
pdf_path = ContractPDFService.save_contract_pdf(contract)
print(f"Saved to: {pdf_path}")
```

## Celery Tasks

All tasks are registered and will be autodiscovered by Celery:

1. `send_contract_email(contract_id)` - Send signing request
2. `send_contract_reminder(contract_id)` - Send reminder
3. `send_contract_signed_emails(contract_id)` - Send confirmation emails
4. `generate_contract_pdf(contract_id)` - Generate PDF (requires WeasyPrint)
5. `check_expired_contracts()` - Periodic task to mark expired contracts
6. `send_pending_reminders()` - Periodic task to send reminders 3 days before expiry

## Periodic Tasks

Add to your Celery beat schedule:

```python
# In config/settings/base.py or local.py
CELERY_BEAT_SCHEDULE = {
    'check-expired-contracts': {
        'task': 'contracts.tasks.check_expired_contracts',
        'schedule': crontab(hour=2, minute=0),  # Daily at 2 AM
    },
    'send-contract-reminders': {
        'task': 'contracts.tasks.send_pending_reminders',
        'schedule': crontab(hour=10, minute=0),  # Daily at 10 AM
    },
}
```
