# SDM Receipt Service

Standalone FastAPI microservice for generating Gujarati Nimit Sevak receipt PDFs, uploading them to Firebase Storage, and returning the signed PDF URL.

This service is intentionally not connected to `sdm-backend` yet.

## Endpoints

- `GET /health`
- `POST /receipts/nimit-sevak`

## Setup

Use Python 3.13 for this service on Windows. Python 3.14 currently causes dependency build failures for `uharfbuzz` and `Pillow`.

1. Create a Python virtual environment.
2. Install dependencies:

```bash
py -3.13 -m venv .venv
```

```bash
.venv\Scripts\activate
```

```bash
pip install -r requirements.txt
```

3. Copy `.env.example` to `.env`.
4. Provide a Gujarati-capable TTF font directly on the machine or place it in `assets/fonts/`.

```text
- Use an installed system font such as Nirmala UI or Shruti
- Or place a font file in assets/fonts/
```

5. Start the service:

```bash
uvicorn app.main:app --reload
```

## Firebase credentials

The service uses a Firebase service-account JSON file directly from disk.

Supported options, in order:

- Auto-discovered local JSON in `app/services/`
- `GOOGLE_APPLICATION_CREDENTIALS`

## Request example

```json
{
  "applicant_name": "જયેશભાઈ",
  "address": "સુરત, ગુજરાત",
  "aadhar_card_no": "123412341234",
  "mobile_no": "9876543210",
  "occupation": "સેવા",
  "age": "32",
  "gender": "પુરુષ",
  "pradesh": "સુરત",
  "mandal_name": "અડાજણ",
  "reference_person_name": "મુકેશભાઈ",
  "reference_person_phone_no": "9898989898",
  "available_sevas": ["રસોઈ", "સાફ સફાઈ", "વાહન સેવા"],
  "other_seva_details": "જરૂર પડે ત્યાં સેવા કરી શકું છું.",
  "seva_available_from": "2026-10-10",
  "seva_available_to": "2026-10-20",
  "available_vehicles": ["કાર"],
  "drivable_vehicles": ["ટ્રેક્ટર", "કાર"],
  "vehicle_available_from": "2026-10-12",
  "vehicle_available_to": "2026-10-18",
  "night_shift_available": true
}
```

## Response example

```json
{
  "success": true,
  "receipt_number": "NS-20260426-142355",
  "pdf_url": "https://...",
  "storage_path": "sdm/svayamsevak-receipts/20260426/ns-20260426-142355.pdf",
  "generated_at": "2026-04-26T14:23:55.123456+00:00"
}
```

## Notes

- The PDF layout is designed to resemble the Gujarati prayer form sample.
- Gujarati text shaping is handled with `fpdf2` + HarfBuzz instead of a basic PDF canvas renderer.
- If no Gujarati font is available, the service raises a clear startup/runtime error instead of generating broken glyphs.
- Optional base64 image input is supported for the applicant photo box.
- `.env` only needs `HOST` and `PORT`.

## Deployment

The repository includes a GitHub Actions workflow at `.github/workflows/receipt.yml` that deploys to the same OM2 server used by `sdm-backend` and `sdm-frontend`.

- Trigger: push to the `production` branch or manual workflow dispatch
- Server: `165.232.186.218`
- Remote path: `/var/www/html/sdm-receipt`
- Process manager: `pm2`
- Local-only bind: `127.0.0.1:8000`

### Required GitHub secrets

- `SSH_ROOT`: root SSH password for the server
- `GITHUB_TOKEN`: used by the workflow to pull the repository on the server
- `SLACK_WEBHOOK_RECEIPT_DEPLOY`: optional Slack webhook for deploy notifications

### Runtime expectations on OM2

- The workflow installs `python3` and `python3-venv` if missing.
- It creates or refreshes `.venv`, installs `requirements.txt`, and writes `.env` with `HOST=127.0.0.1` and `PORT=8000`.
- It starts the service under PM2 as `sdm-receipt` and verifies `GET /health` locally after restart.
- Gujarati font files must remain present under `assets/fonts/` in the deployed checkout.
