# Insurance

> Reference for Insurance in ambulancejob.

[All server exports](/docs/jobs/ambulancejob/api/server-exports)

## Insurance

Read and write access to the health-insurance system used by the ambulance billing flow. Read exports let other resources check coverage, calculate co-pays, and enumerate providers. Write exports let admin menus, hospital welcome flows, or custom integrations grant, remove, or extend coverage **without charging the player** (the in-game `/insurance` command remains the normal purchase flow).

### `isInsuranceEnabled()`

- **Purpose**: Reports whether the insurance subsystem is enabled in `Config.HealthInsurance`.
- **Returns**: `boolean` — `true` when enabled.

```lua
if exports["sky_ambulancejob"]:isInsuranceEnabled() then
    -- insurance features are available
end
```

### `hasInsurance(playerId)`

- **Purpose**: Quick check whether a player currently has active insurance coverage.
- **Arguments**:

  - `playerId` (`number`): The server ID of the player.
- **Returns**: `boolean` — `true` when insured, `false` otherwise (also `false` when insurance is disabled).

```lua
local insured = exports["sky_ambulancejob"]:hasInsurance(playerId)
```

### `getCoverage(playerId)`

- **Purpose**: Returns a compact summary of the player's current coverage.
- **Arguments**:

  - `playerId` (`number`): The server ID of the player.
- **Returns**: `table | nil`:

  - `providerId` (`string`) — Provider ID (e.g. `"standard_care"`).
  - `label` (`string`) — Display name (e.g. `"Standard Care"`).
  - `copayPercent` (`number`) — Patient co-pay percentage.
  - `price` (`number`) — Configured base premium.
  - `expiresAt` (`number`) — Unix timestamp when coverage expires; `0` when no expiration.

```lua
local coverage = exports["sky_ambulancejob"]:getCoverage(playerId)
if coverage then
    print(coverage.label, coverage.copayPercent, coverage.expiresAt)
end
```

### `getProvider(playerId)`

- **Purpose**: Returns the full provider record (including `description`) assigned to the player.
- **Arguments**:

  - `playerId` (`number`): The server ID of the player.
- **Returns**: `table | nil` — `{ id, label, description, price, copayPercent }`.

```lua
local provider = exports["sky_ambulancejob"]:getProvider(playerId)
```

### `getCopayPercent(playerId)`

- **Purpose**: Returns the percentage the player will pay on a medical bill.
- **Arguments**:

  - `playerId` (`number`): The server ID of the player.
- **Returns**: `number` — Co-pay percentage (`100` when uninsured or insurance disabled).

```lua
local percent = exports["sky_ambulancejob"]:getCopayPercent(playerId)
```

### `getExpiresAt(playerId)`

- **Purpose**: Returns the Unix timestamp at which the player's coverage expires.
- **Arguments**:

  - `playerId` (`number`): The server ID of the player.
- **Returns**: `number` — Unix timestamp; `0` when coverage has no expiration or the player is uninsured.

```lua
local expiresAt = exports["sky_ambulancejob"]:getExpiresAt(playerId)
```

### `calculateCopay(playerId, billAmount)`

- **Purpose**: Calculates how much a patient would pay for a given bill amount, taking their active health insurance into account. If the player has no insurance or insurance is disabled, the full bill amount is returned.
- **Arguments**:

  - `playerId` (`number`): The server ID of the player whose insurance should be checked.
  - `billAmount` (`number`): The original bill amount in dollars.
- **Returns**: `table` with:

  - `copayAmount` (`number`) — The amount the patient pays after insurance.
  - `originalAmount` (`number`) — The original bill amount passed in.
  - `hasInsurance` (`boolean`) — Whether the player has active insurance.
  - `provider` (`table?`) — Insurance provider details (only when insured):
  
    - `id` (`string`) — Provider ID (e.g. `"standard_care"`).
    - `label` (`string`) — Display name (e.g. `"Standard Care"`).
    - `copayPercent` (`number`) — The percentage the patient pays (e.g. `30`).

```lua
-- Example: Patient with "Standard Care" (30% copay) and a $900 bill
local result = exports["sky_ambulancejob"]:calculateCopay(playerId, 900)

print(result.copayAmount)   -- 270 (patient pays 30%)
print(result.originalAmount) -- 900
print(result.hasInsurance)   -- true
print(result.provider.label) -- "Standard Care"

-- Example: Patient without insurance
local result = exports["sky_ambulancejob"]:calculateCopay(playerId, 900)

print(result.copayAmount)   -- 900 (full amount)
print(result.hasInsurance)   -- false
```

### `calculateInsuranceCopay(playerId, billAmount)`

- **Purpose**: Alias for [`calculateCopay`](#calculatecopayplayerid-billamount), kept for backward compatibility. Returns identical results.

```lua
local result = exports["sky_ambulancejob"]:calculateInsuranceCopay(playerId, 900)
```

### `getAllProviders()`

- **Purpose**: Returns every insurance provider configured in `Config.HealthInsurance.providers`, sorted alphabetically by label.
- **Returns**: `table` — array of `{ id, label, description, price, copayPercent }` entries (cloned, safe to mutate).

```lua
for _, provider in ipairs(exports["sky_ambulancejob"]:getAllProviders()) do
    print(provider.id, provider.label, provider.price)
end
```

### `getProviderById(providerId)`

- **Purpose**: Returns a single configured provider by its `id`.
- **Arguments**:

  - `providerId` (`string`): The provider identifier (e.g. `"basic_care"`).
- **Returns**: `table | nil`.

```lua
local provider = exports["sky_ambulancejob"]:getProviderById("basic_care")
```

### `getInsuranceConfig()`

- **Purpose**: Returns a read-only snapshot of the high-level insurance configuration.
- **Returns**: `table`:

  - `enabled` (`boolean`)
  - `command` (`string`) — Chat command used to open the UI.
  - `account` (`string`) — Account premiums are charged from.
  - `expirationOptions` (`table`) — Sorted array of valid `durationDays` values.

```lua
local cfg = exports["sky_ambulancejob"]:getInsuranceConfig()
print(cfg.command, cfg.account)
```

<callout color="warning" icon="i-lucide-alert-triangle">

The following write exports **do not charge the player**. They are integration hooks intended for admin menus, hospital welcome flows, or custom scripts. Use the in-game `/insurance` command for the normal player-purchase flow.

</callout>

### `setInsurance(playerId, providerId, durationDays?)`

- **Purpose**: Assigns or refreshes coverage for the player. When called with the same provider that the player already holds, the duration is appended to the existing expiration (mirrors the in-game purchase flow).
- **Arguments**:

  - `playerId` (`number`): The server ID of the player.
  - `providerId` (`string`): One of the configured provider IDs.
  - `durationDays` (`number?`): Number of days the coverage lasts. Must be one of `Config.HealthInsurance.expiration.availableDays`. Defaults to the configured default duration when omitted.
- **Returns**: `boolean, string?` — `true` on success, otherwise `false` and one of:

  - `"disabled"` — insurance is disabled in config.
  - `"invalid_provider"` — unknown `providerId`.
  - `"invalid_player"` — player not online.
  - `"invalid_identifier"` — could not resolve identifier.
  - `"invalid_duration"` — duration not in `availableDays`.

```lua
local ok, err = exports["sky_ambulancejob"]:setInsurance(playerId, "basic_care", 7)
if not ok then
    print("setInsurance failed:", err)
end
```

### `removeInsurance(playerId)`

- **Purpose**: Removes the player's coverage and clears the cache.
- **Arguments**:

  - `playerId` (`number`): The server ID of the player.
- **Returns**: `boolean` — `true` when a coverage row was removed, `false` when the player had no coverage.

```lua
exports["sky_ambulancejob"]:removeInsurance(playerId)
```

### `extendInsurance(playerId, additionalDays)`

- **Purpose**: Extends an existing coverage's expiration by the given number of days. Base is the later of `now` and the current `expiresAt`.
- **Arguments**:

  - `playerId` (`number`): The server ID of the player.
  - `additionalDays` (`number`): Days to add (must be `> 0`).
- **Returns**: `boolean, string?` — `true` on success, otherwise `false` and one of:

  - `"disabled"` — insurance is disabled in config.
  - `"invalid_player"` — player not online.
  - `"invalid_duration"` — `additionalDays` is missing or non-positive.
  - `"not_insured"` — player has no active coverage.

```lua
local ok, err = exports["sky_ambulancejob"]:extendInsurance(playerId, 3)
```
