# ClickRecorder

## Phase 1: Core Recording, Playback, and SQLite Storage

**Status: Completed**

This phase implemented the core functionality for ClickRecorder:
- Recording left mouse clicks across the entire screen
- Storing click coordinates (x, y) and timestamp
- Automatic screenshot capture at each click
- Storing screenshot paths in an SQLite database
- Simulating mouse clicks at recorded coordinates with original timing
- Creating the SQLite database with required schema (Playlists, Clicks, Screenshots tables)
- Saving screenshots as PNG in playlist-specific folders

## Phase 2: GUI with Playlist Management and JSON Export

**Status: Completed**

This phase implemented:
- A draggable, always-visible GUI (Tkinter)
- Always-on-top behavior during recording
- Transparent, minimalistic design
- Start/Stop Recording buttons
- Play/Pause Playlist buttons
- Add/Edit/Delete Playlist buttons
- Manual Screenshot button
- Playlist Dropdown for selection
- Export Playlist to JSON (with all required fields)
- Status Label for current state

## Phase 3: Error Handling, Loop Prevention, and Logging

**Status: Completed**

This phase implemented:
- Loop prevention (detect 3 consecutive identical clicks, alert, and stop recording)
- On-screen alerts for errors (permissions, disk, database, loop detection)
- Logging errors to clickrecorder.log
- User override for loop alert via confirmation dialog

## Phase 4: User Testing and Refinements

**Status: In Progress**

This phase will implement:
- Testing all major functionality (record, playback, playlist management, export)
- Optimizing for minimal CPU/memory usage
- Ensuring compatibility with Windows 10/11
- Usability review (intuitive GUI, clear feedback)
- Security review (no network/file access outside allowed dirs)

## Update: Playback Supports Keystrokes
- Playback now replays both mouse clicks and keyboard events (key presses/releases) in the original order and timing as recorded.
- No changes needed to recording; just play a playlist as usual.

## Tech Stack
- Python 3.8+
- Tkinter (GUI)
- pyautogui (mouse automation)
- pillow (screenshot processing)
- sqlite3 (database)
- json (data export)

## Installing Requirements

Before running the application, install the required Python packages using pip. Run the following command in your project directory:

```bash
pip install -r requirements.txt
```

This will install all necessary external dependencies (pyautogui, pillow, pynput). Make sure you are using Python 3.8 or higher.

## Project Structure (to be created)
- `main.py` – Application entry point
- `db.py` – Database schema and operations
- `recorder.py` – Mouse click and screenshot recording logic
- `playback.py` – Playback logic
- `utils.py` – Utility functions
- `screenshots/` – Folder for saved screenshots
- `clicks.db` – SQLite database file

## Next Steps
- Set up project structure and initialize database
- Implement mouse click recording and screenshot capture
- Implement playback of recorded clicks
- Store and retrieve data from SQLite database

## 2024-06-09

## Chrome Downloads Manager

The Chrome Downloads Manager automatically monitors the `~/Downloads` folder and uploads new files to an S3 bucket for backup and processing.

### Features

- **High-Frequency Monitoring**: Checks the downloads folder every 5 seconds for new files
- **Smart File Detection**: Avoids uploading temporary files or files still being downloaded
- **S3 Integration**: Automatically uploads files to configured S3 bucket with metadata
- **Change Detection**: Only uploads files that have changed or are new
- **UI Integration**: Logs upload activities to the main application interface

### Configuration

Set the following environment variables to configure the Chrome Downloads Manager:

```bash
# S3 Bucket for Chrome downloads (default: auditwhizz-chrome-downloads)
CHROME_DOWNLOADS_S3_BUCKET=your-bucket-name

# AWS Region (default: ap-southeast-2)
AWS_REGION=your-aws-region

# Local development mode (disables S3 operations)
LOCAL_DEV=true
```

### S3 File Structure

Files are uploaded to S3 with the following structure:
```
s3://your-bucket/chrome_downloads/YYYYMMDD_HHMMSS_filename.ext
```

Each file includes metadata:
- `original_path`: Original file path on local system
- `upload_timestamp`: When the file was uploaded
- `file_size`: Size of the uploaded file
- `source`: Source identifier (chrome_downloads_manager)

### Performance Notes

- **5-Second Monitoring**: The manager checks every 5 seconds for optimal responsiveness
- **Efficient Processing**: Only processes files that have changed or are new
- **Background Operation**: Runs in a daemon thread to avoid blocking the main application
- **Smart Filtering**: Automatically skips temporary files and files still being downloaded

### Security

- Uses AWS credentials from environment or IAM roles
- Files are uploaded with appropriate metadata for tracking
- Supports row-level security through S3 bucket policies

## 🚀 New Features

### OpenAI-Powered Supporting Document Analyzer
- **Analyze Docs Button**: New button that analyzes any screen for supporting document links
- **No Playlist Required**: Works independently on any screen without needing to select a playlist
- **GPT-5 Integration**: Uses OpenAI's latest GPT-5 model for accurate link detection
- **Automated Clicking**: Automatically clicks on found supporting document links
- **Configurable Wait Time**: Adjustable wait time after each click (default: 10 seconds)
- **S3 Integration**: Uploads post-click screenshots to S3 for documentation
- **Comprehensive Logging**: Detailed logging to `logs/openai.log` for debugging
- **Smart Navigation**: Automatically navigates back after each click to continue processing

#### How to Use:
1. Navigate to any screen you want to analyze
2. Click the **"Analyze Docs"** button
3. The system will:
   - Capture a screenshot of the current screen
   - Send it to OpenAI GPT-5 for analysis
   - Identify supporting document links (PDFs, receipts, attachments)
   - Automatically click each link
   - Wait the configured time
   - Take screenshots after each click
   - Upload screenshots to S3
   - Navigate back and continue to the next link

#### Configuration:
- Set `OPENAI_API_KEY` in your environment variables
- Configure `POST_CLICK_WAIT_TIME` in constants.py (default: 10 seconds)
- Set S3 bucket names in constants.py for production use

#### Setup Instructions:
1. **Get OpenAI API Key:**
   - Visit [OpenAI API Keys](https://platform.openai.com/api-keys)
   - Create a new API key
   - Copy the key (starts with `sk-` or `sk-proj-`)

2. **Set API Key (Choose one method):**
   
   **Option A: Environment Variable (Recommended)**
   ```bash
   # Windows PowerShell
   $env:OPENAI_API_KEY='your-api-key-here'
   
   # Windows Command Prompt
   set OPENAI_API_KEY=your-api-key-here
   
   # Linux/Mac
   export OPENAI_API_KEY='your-api-key-here'
   ```
   
   **Option B: Create .env file**
   ```bash
   # Create .env file in project root
   echo "OPENAI_API_KEY=your-api-key-here" > .env
   ```
   
   **Option C: Run setup script**
   ```bash
   python setup_openai.py
   ```

3. **Restart Application:**
   - Restart the application after setting the API key
   - The "Analyze Docs" button should now work

#### Troubleshooting:
- **401 Unauthorized Error**: API key is missing, invalid, or expired
- **Check logs/openai.log** for detailed error messages
- **Verify API key format** starts with `sk-` or `sk-proj-`
- **Ensure API key has sufficient credits** in your OpenAI account

## Changelog

- Added a screenshot (📸) icon button to the minimized view, allowing manual screenshot capture directly from the compact UI.
- Playlist loading now uses only Supabase; SQLite is disabled for playlists.
- Playlists are now loaded exclusively from Supabase (cloud-first); local SQLite is disabled for playlist loading.
- Adding a playlist now checks for duplicates and inserts only in Supabase (cloud-only); all SQLite logic is disabled for this action.
- Adding a playlist is now cloud-only: duplicate checks and creation are performed exclusively in Supabase.
- Disabled popup debug screens and removed debug button from the UI for a cleaner user experience.
- Disabled all Supabase-related popups (info, error, and text popups). All notifications for Supabase actions are now silent or logged only.

- 2025-11-13: Fixed clicking of wrapped (two-line) filenames in the Xero Files dialog by enabling a cached partial-LINE fallback for all files. This improves reliability when long names split across lines.
- 2025-12-10: Fixed MySQL password rotation handling so that refreshed credentials are used on new connections without restarting the app (see `mysql/mysql_client.py` and `mysql/config.py`).

## 2024-06-09
- Minimized view buttons made smaller for a more compact UI.
- App now starts in the middle of the left side of the screen for both minimized and full views.
- Added a blue indicator bar at the top of the minimized view when the app is playing.

---

_This README will be updated as development progresses through each phase._ 