# Local Supabase Setup Guide

This guide will help you set up and run Supabase locally for development.

## Prerequisites

1. **Docker Desktop** - Supabase runs on Docker
   - Download: https://docs.docker.com/desktop
   - Make sure Docker Desktop is running before proceeding

2. **Supabase CLI** - Already installed via Homebrew

## Quick Start

### 1. Start Local Supabase

```bash
cd old
./scripts/setup-local-supabase.sh
```

This script will:
- Check if Docker is running
- Initialize Supabase (if not already done)
- Start all Supabase services locally
- Apply all database migrations
- Display your local credentials

### 2. Run Flutter App with Local Supabase

```bash
./scripts/run-flutter-local.sh
```

Or specify a device:
```bash
./scripts/run-flutter-local.sh chrome
./scripts/run-flutter-local.sh web-server
```

### 3. Get Credentials Manually

If you need to see the credentials again:

```bash
./scripts/get-local-credentials.sh
```

Or use the Supabase CLI directly:
```bash
supabase status
```

## Manual Setup

If you prefer to set up manually:

### Step 1: Start Supabase

```bash
cd old
supabase start
```

### Step 2: Get Credentials

After starting, `supabase status` will show:
- **API URL**: Your local Supabase URL (usually `http://127.0.0.1:54321`)
- **anon key**: Your local anon key

### Step 3: Run Flutter with Credentials

```bash
flutter run -d chrome \
  --dart-define=SUPABASE_URL="http://127.0.0.1:54321" \
  --dart-define=SUPABASE_ANON_KEY="<your-anon-key>"
```

## Useful Commands

### Supabase Management

```bash
# Start Supabase
supabase start

# Stop Supabase
supabase stop

# View Supabase status and credentials
supabase status

# View logs
supabase logs

# Reset database (applies all migrations fresh)
supabase db reset

# Apply new migrations
supabase db reset
```

### Database Migrations

Migrations are automatically applied when you run `supabase start` or `supabase db reset`.

To create a new migration:
```bash
supabase migration new <migration_name>
```

## Local Supabase Services

When running locally, Supabase provides:

- **API**: `http://127.0.0.1:54321`
- **Studio (Admin UI)**: `http://127.0.0.1:54323`
- **Database**: `postgresql://postgres:postgres@127.0.0.1:54322/postgres`
- **Auth**: Handled by the API
- **Storage**: Handled by the API

## Accessing Supabase Studio

Supabase Studio is a web-based admin interface for managing your local database:

1. Start Supabase: `supabase start`
2. Open Studio URL from `supabase status` output (usually `http://127.0.0.1:54323`)
3. Use it to:
   - View and edit tables
   - Run SQL queries
   - Manage authentication
   - View API documentation

## Troubleshooting

### Docker Not Running

```
Error: Cannot connect to the Docker daemon
```

**Solution**: Start Docker Desktop and wait for it to fully start, then try again.

### Port Already in Use

If ports 54321, 54322, or 54323 are already in use:

1. Stop the conflicting service
2. Or modify `supabase/config.toml` to use different ports

### Database Reset Issues

If migrations fail:

```bash
# Stop Supabase
supabase stop

# Reset everything
supabase db reset

# Start again
supabase start
```

### Flutter Can't Connect

Make sure:
1. Supabase is running (`supabase status`)
2. You're using the correct URL and anon key
3. The `--dart-define` flags are correctly formatted

## Switching Between Local and Production

### Use Local Supabase
```bash
./scripts/run-flutter-local.sh
```

### Use Production Supabase
Update `env.json` with production credentials and run:
```bash
flutter run -d chrome \
  --dart-define-from-file=env.json
```

Note: The current app uses `String.fromEnvironment`, so you need `--dart-define` flags.

## Next Steps

1. ✅ Start Docker Desktop
2. ✅ Run `./scripts/setup-local-supabase.sh`
3. ✅ Run `./scripts/run-flutter-local.sh`
4. ✅ Open Supabase Studio to view your database
5. ✅ Start developing!

