# CX9 Health Check Bundle

Comprehensive health check bundle for CX9 Symfony applications with multi-tenant support.

## Features

- **Multiple Health Checks**:
  - API Health Check (memory usage, PHP version)
  - System Resources Check (disk space, directory permissions)
  - License Server Check (external dependency)
  - Tenant Check (database + API dependencies)

- **Multiple Endpoints**:
  - `/health` - Summary of all checks
  - `/health/detailed` - Detailed report with system info
  - `/health/live` - Simple liveness check
  - `/health/ready` - Readiness check
  - `/health/check/{name}` - Individual check by name

- **Multi-Tenant Support**:
  - Uses AUTHCODE constant for tenant-specific checks
  - Database and API URL resolution via license server
  - Tenant-aware logging and metrics

- **Bundle-Ready Architecture**:
  - Auto-discovery of HealthCheckInterface implementations
  - Configurable health checks
  - Easy integration across multiple APIs

## Installation

```bash
composer require cx9/health-check-bundle
```

### Enable the Bundle

Add to `config/bundles.php`:

```php
return [
    // ...
    Cx9\HealthCheckBundle\Cx9HealthCheckBundle::class => ['all' => true],
];
```

## Configuration

Create `config/packages/cx9_health_check.yaml`:

```yaml
cx9_health_check:
  endpoints:
    base_path: '/health'
    detailed: true
    live: true
    ready: true
  
  health_checks:
    api: true
    system_resources: true
    license_server: true
    tenant: true  # Requires AUTHCODE constant
```

## Usage

### Basic Health Check

```bash
curl http://localhost:8000/health
```

**Response:**
```json
{
  "status": "healthy",
  "timestamp": "2024-01-01T12:00:00+00:00",
  "checks": {
    "api": {
      "status": "healthy",
      "response_time_ms": 5.2
    },
    "system_resources": {
      "status": "healthy", 
      "response_time_ms": 12.1
    },
    "license_server": {
      "status": "healthy",
      "response_time_ms": 120.5
    },
    "tenant": {
      "status": "healthy",
      "response_time_ms": 250.3
    }
  }
}
```

### Detailed Health Check

```bash
curl http://localhost:8000/health/detailed
```

Includes system information, individual check details, and diagnostic data.

### Tenant-Specific Check

The tenant health check requires an active AUTHCODE constant (set by cx9/api-authcode-bundle):

```bash
# This will check database + API dependencies for the current tenant
curl http://localhost:8000/health/check/tenant
```

## Custom Health Checks

Create custom health checks by implementing `HealthCheckInterface`:

```php
<?php

namespace App\HealthCheck;

use Cx9\HealthCheckBundle\HealthCheck\HealthCheckInterface;
use Cx9\HealthCheckBundle\HealthCheck\HealthCheckResult;

class CustomHealthCheck implements HealthCheckInterface
{
    public function check(): HealthCheckResult
    {
        // Your health check logic
        return HealthCheckResult::healthy('custom', 'Everything is fine');
    }

    public function getName(): string
    {
        return 'custom';
    }

    public function getTimeout(): int
    {
        return 10;
    }

    public function isCritical(): bool
    {
        return false;
    }
}
```

The bundle will automatically discover and register your health check.

## Health Check Types

### API Health Check
- Memory usage monitoring
- PHP version check
- Basic API functionality

### System Resources Health Check  
- Disk space monitoring (critical < 5%, degraded < 15%)
- Directory permissions (cache, log, temp)
- PHP configuration validation
- Memory limit checks

### License Server Health Check
- External license server connectivity
- Response time monitoring
- Status validation

### Tenant Health Check
- Database connectivity per tenant
- API dependencies (Mail, Print, CDN, Payment)
- Critical vs non-critical service classification
- Combined health assessment

## Status Types

- **healthy**: All checks passed
- **degraded**: Non-critical issues detected
- **unhealthy**: Critical failures detected

## Integration with CX9 APIs

This bundle is designed to work seamlessly with:
- `cx9/api-authcode-bundle` (for tenant resolution)
- Multi-tenant database setup
- CX9 service architecture (Mail-API, Print-API, CDN, etc.)

## License

Proprietary - CX9 Systems