# Solarise API integration

Add Solarise roof analyses to this project: one request returns the roof and, for each preset, a sized and priced solar system with its savings, payback and financing.

- OpenAPI 3.1 document: https://www.solarise.app/docs/api/openapi.json. Generate the request and response types from it; it also holds the defaults, the examples and every error code (`x-error-codes`).
- Documentation: https://www.solarise.app/en-US/docs/api

## Rules

- POST https://app.solarise.app/api/v1/roof-analyses with a JSON body and `Authorization: Bearer <key>`. Read the key from the environment variable SOLARISE_API_KEY and use it only in server-side code: never hard-code, log or expose it.
- Only `location` is required, with one of: `latitude` and `longitude`, `address` (a full street address with the house number), `eircode` or `plus_code`. Send `sunnyform_id` to use the installer's products and prices (otherwise country defaults apply), and `annual_consumption_kwh` or `average_monthly_energy_bill` when known (otherwise a typical building is assumed).
- Each successful analysis uses one measurement of the account's plan; errors are free. Never call the live API from tests.
- Analyses usually take 15 to 45 seconds. Use an HTTP timeout of at least 180 seconds and run the call in a background job if the project has one.
- Send an `Idempotency-Key` header with a new UUID per analysis and reuse it for every retry of that analysis: a finished analysis is returned instead of run again, and never charged twice.
- Success is HTTP 201 with `"status": "OK"`; HTTP 200 with `Idempotent-Replayed: true` is a stored result. Errors have `"status": "ERROR"`, `error.code`, `error.message`, `error.request_id` and, for invalid input, `error.details`. Branch on `error.code`:
  - Network error, 500, 503 or 504: retry the same request with the same key.
  - `request_in_progress` or 429: wait `Retry-After` seconds, then retry with the same key.
  - `insufficient_measurements`: stop; an administrator must add measurements.
  - `location_not_found`, `location_not_specific`: ask for a full address or coordinates.
  - `location_not_supported`, `no_imagery_available`, `roof_not_found`, `no_suitable_system`: this property cannot be analyzed.
  - Any other 4xx: fix the request or the configuration; do not retry it unchanged.
- `presets` holds one system per preset; show `default_preset` first. Amounts are in `currency`; units are metric.
- `save_to_crm` and `send_webhook` default to false; set them only when the user asks.

## Steps

1. Find where the project needs roof or solar analyses and confirm with me which fields to store and show.
2. Add one small server-side client with the generated types and the rules above.
3. Map the response into the project's models, keeping `analysis_id`.
4. Turn each error into a clear message or action.
5. Test with mocked HTTP and the documented examples (success, 422, 402, 409).
6. Document SOLARISE_API_KEY: an administrator creates the key in the Solarise dashboard under API, and it is shown only once.

Ask me before choosing a SunnyForm, sending customer contact details, or enabling `save_to_crm` or `send_webhook`.
