# Check Pending Images

This Lambda function checks if there are any unprocessed images in the database that need to be analyzed by a Rekognition model. It's designed to be used as a gate in Step Functions workflows to determine whether to start a model or skip that step.

## Functionality

The function performs the following operations:

1. Connects to Supabase to query the product_images table
2. Counts the number of records with `processed=false`
3. Returns a flag `should_start_model` based on whether there are pending images
4. Provides the count of pending images for reporting and decision-making

## Environment Variables

The function does not use direct environment variables but instead retrieves configuration from SSM Parameter Store:

- `/supabase/url`: URL for the Supabase instance
- `/supabase/anon`: Anonymous API key for Supabase

## Step Functions Integration

### Return Format

The function returns a response that includes a `should_start_model` flag at the root level for easy integration with Step Functions Choice states:

```json
{
  "statusCode": 200,
  "body": {
    "message": "Found 5 pending images for processing",
    "pending_images_count": 5
  },
  "should_start_model": true
}
```

### Step Functions Example

The function is designed to be used in a Step Functions workflow like:

```json
{
  "States": {
    "CheckPendingImages": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT:function:check_pending_images",
      "Next": "ShouldStartModel"
    },
    "ShouldStartModel": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.should_start_model",
          "BooleanEquals": true,
          "Next": "StartModel"
        }
      ],
      "Default": "SkipModelStart"
    },
    "StartModel": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT:function:start_model",
      "Next": "ProcessImages"
    },
    "SkipModelStart": {
      "Type": "Pass",
      "Next": "WorkflowComplete"
    }
  }
}
```

## Database Schema

The function requires the `product_images` table in Supabase with:
- A boolean column `processed` that indicates whether the image has been processed

## Error Handling

- If the Supabase connection fails, the function returns an error and `should_start_model: false`
- If the query fails, it defaults to `should_start_model: false` (safer to skip starting a model)
- Detailed error logs are provided for troubleshooting

## Usage

This function is typically used:
- As a preliminary step before starting a model in a Step Functions workflow
- To optimize costs by avoiding starting a model when there are no images to process

## Troubleshooting

Common issues:
- Database connectivity problems: Check Supabase credentials and network access
- Missing product_images table: Ensure the table exists with the required schema
- Empty result but images exist: Verify the processed column is correctly set 