# Check Model Availability Function

This function checks whether a model is available and in a RUNNING state before proceeding with image analysis in the Rekognition workflow.

## Functionality

The check_model_availability function performs the following operations:
- Retrieves the most recent model version in STARTING or RUNNING state from Supabase
- Checks the actual status of this model in AWS Rekognition
- Updates the model's status in the database if it has changed
- Returns whether the model is available for use

## Environment Variables

This function requires the following parameters from AWS Parameter Store:
- `/supabase/url`: The URL for the Supabase instance
- `/supabase/anon`: The anonymous key for Supabase access
- `/datafy-rekognition-stack/project-arn`: The ARN for the Rekognition project

## Required Project ID

The function uses the Project ARN to identify the correct Rekognition project. The ARN must include the project ID `1725357683732`.

## AWS Resources
- AWS Rekognition: To check the real-time status of models
- SSM Parameter Store: To retrieve configuration values
- Supabase: To read/update model version information

## Usage in Step Functions

This function is used in the main Rekognition state machine:
1. After a model is started using the `start_model` function
2. The state machine waits for 30 seconds
3. This function checks if the model is available
4. If not, the state machine loops back to wait again before rechecking
5. Once the model is running, the workflow proceeds to analyze images

## Response Structure

The function returns a response with the following structure:
```json
{
  "statusCode": 200,
  "body": {
    "message": "Model is RUNNING",
    "model_available": true,
    "status": "RUNNING",
    "version_name": "model-v1",
    "project_version_arn": "arn:aws:rekognition:..."
  }
}
```

## Error Handling

The function handles several error cases:
- No active or starting model found (404 status code)
- Model exists but is not in a RUNNING state (200 status code with model_available=false)
- AWS Rekognition errors (500 status code)

## Troubleshooting

Common issues:
- Model not found: Check if there is a model with STARTING or RUNNING status in the database
- Model stuck in STARTING: AWS Rekognition may be experiencing delays in model startup
- Project ARN format issues: Ensure the project ARN uses the correct project ID 