# Airtable Integration Setup

This guide will help you set up Airtable integration for your plant care application to store data remotely.

## Prerequisites

1. An Airtable account (free tier available)
2. Your Airtable API key and Base ID

## Step 1: Create an Airtable Base

1. Go to [Airtable.com](https://airtable.com) and sign in
2. Click "Add a base" and choose "Start from scratch"
3. Name your base something like "Plant Care App"

## Step 2: Create Tables

Create the following tables in your Airtable base:

### 1. Plants Table
Create a table named "Plants" with these fields:
- **Name** (Single line text) - The plant's name
- **Plant Type** (Single line text) - Type of plant (e.g., "Snake Plant")
- **Watering Frequency** (Number) - Days between watering
- **Last Watered** (Date) - Last watering date
- **Next Watering** (Date) - Next scheduled watering
- **Notes** (Long text) - Optional notes
- **Photo** (Attachment) - Plant photo (optional)
- **Care Instructions** (Long text) - JSON string of care instructions array
- **Growth Stages** (Long text) - JSON string of growth stages array
- **Additional Images** (Long text) - JSON string of additional image URLs

### 2. WateringLogs Table
Create a table named "WateringLogs" with these fields:
- **Pot ID** (Single line text) - ID of the plant pot
- **Pot Name** (Single line text) - Name of the plant pot
- **Watered At** (Date & time) - When the plant was watered
- **Notes** (Long text) - Optional watering notes

### 3. Users Table
Create a table named "Users" with these fields:
- **Username** (Single line text) - User's username
- **Password** (Single line text) - User's password (hashed in production)

### 4. Settings Table
Create a table named "Settings" with these fields:
- **User ID** (Single line text) - ID of the user
- **Dashboard Layout** (Long text) - JSON string of dashboard layout preferences

## Step 3: Get Your API Credentials

1. Go to your [Airtable Account page](https://airtable.com/developers/web/api/introduction)
2. Click "Create token" or use an existing one
3. Give your token a name like "Plant Care App"
4. Grant access to your base (select your base from the dropdown)
5. Copy the API token

## Step 4: Configure Environment Variables

1. Open the `.env` file in your project root
2. Replace the placeholder values:

```env
VITE_AIRTABLE_API_KEY=your_actual_api_key_here
VITE_AIRTABLE_BASE_ID=your_actual_base_id_here
```

You can find your Base ID in the URL when viewing your base: `https://airtable.com/{BASE_ID}/...`

## Step 5: Update Your Code

To use the Airtable-enabled stores, import from the new stores file:

```typescript
// Instead of:
import { flowerPots, wateringLog } from '$lib/stores';

// Use:
import { flowerPotsAirtable as flowerPots, wateringLogAirtable as wateringLog } from '$lib/stores-airtable';
```

## Step 6: Test the Integration

1. Start your development server: `npm run dev`
2. Try adding a plant - it should now sync with Airtable
3. Check your Airtable base to verify the data is being stored

## Troubleshooting

### Common Issues:

1. **"Airtable API key and base ID must be configured"**
   - Make sure your `.env` file has the correct values
   - Ensure the environment variables are prefixed with `VITE_` for Vite

2. **"Failed to sync with Airtable" errors**
   - Check that your API key has the correct permissions
   - Verify your Base ID is correct
   - Ensure table names match exactly ("Plants", "WateringLogs", "Users", "Settings")

3. **Data not appearing in Airtable**
   - Check that your table field names match the API expectations
   - Verify the data types in Airtable match what's being sent

### Manual Testing:

You can test your API endpoints directly:
- `GET /api/plants` - Should return all plants
- `POST /api/plants` - Should create a new plant
- `GET /api/watering-logs` - Should return all watering logs

## Security Notes

- Never commit your `.env` file to version control
- In production, consider using server-side environment variables instead of `VITE_` prefixed ones
- The current implementation stores passwords in plain text - implement proper hashing for production use
- Consider implementing user authentication and authorization for multi-user scenarios

## Next Steps

Once Airtable integration is working, you can:
- Add offline sync capabilities
- Implement data validation
- Add backup/restore functionality
- Set up automated backups of your Airtable data