# 🚀 Quick Start: Database Autosync

## Step-by-Step Guide

### Step 1: Download Backup Files from Google Drive

1. Open the Google Drive folder:
   ```
   https://drive.google.com/drive/folders/1tI1BS24dudGTmMSQ1s_Bv8-bpQ5obv9U?usp=sharing
   ```

2. Download these files:
   - `tiffany_backup.sql` (Sudirman branch)
   - `cvreyga_backup.sql` (Gambir branch)

### Step 2: Save Files to Local Folder

Create a folder named `synced_backups` in your project:

```
E:\VIBECODING\db_compare\
├── autosync.js
├── autosync-menu.js
├── server.js
└── synced_backups/          ← Create this folder
    ├── tiffany_backup.sql    ← Save here
    └── cvreyga_backup.sql    ← Save here
```

### Step 3: Run the Autosync Menu

Open PowerShell/Terminal and run:

```bash
cd E:\VIBECODING\db_compare
node autosync-menu.js
```

### Step 4: Select Sync Option

```
╔════════════════════════════════════════════════════╗
║  📦 DATABASE AUTOSYNC MANAGER                      ║
╚════════════════════════════════════════════════════╝

MENU OPTIONS:

  1. 🔄 Sync All Databases (tiffany + cvreyga)
  2. 🏠 Sync Tiffany (Sudirman) Only
  3. 🏪 Sync CVReyga (Gambir) Only
  4. 📂 Open Backup Folder
  5. ℹ️  View Status
  6. ❌ Exit
```

**Press 1** to sync both databases.

### Step 5: Wait for Sync to Complete

The system will:
1. ✅ Find backup files
2. ✅ Validate SQL syntax
3. ✅ Restore to local databases
4. ✅ Verify data integrity
5. ✅ Show results

Example output:
```
🔄 Restoring to Tiffany (Sudirman)...
   Database: tiffany_local
   File size: 1100.50 MB
   Statements: 1,547,894
   Starting restore...

   [████████████████████] 100% (1,547,894/1,547,894)

✅ Restore completed!
   Successful: 1,547,894
   Errors: 0

📊 Verifying Tiffany (Sudirman)...
   ✓ Tables: 303
   ✓ Key tables:
     - item: 34,405
     - sale: 199,550
     - rcv: 245,748
     - jnl: 573,944
     - stocklog: 685,300
   ✓ Total sample records: 1,738,947
```

### Step 6: Start the Application

```bash
npm start
```

Then open in browser:
```
http://localhost:3000
```

### Step 7: Verify Everything Works

✅ Check database connections
✅ Search for products
✅ View analytics dashboard
✅ Export data

---

## Alternative: Command Line Only

If you prefer not to use the interactive menu:

```bash
# Sync all databases
node autosync.js all

# Or sync individually
node autosync.js tiffany
node autosync.js cvreyga
```

---

## Troubleshooting

### Problem: "No backup file found"
- Check files are in `synced_backups/` folder
- Verify filenames: `tiffany_backup.sql` and `cvreyga_backup.sql`

### Problem: "Connection failed"
- Ensure PostgreSQL is running
- Check with: `psql -U root -l` (should show databases)

### Problem: Restore is very slow
- This is normal for large databases
- Tiffany: 5-15 minutes
- CVReyga: 2-5 minutes
- Let it complete

### Problem: Restore fails with SQL errors
- Verify backup files are complete (not corrupted)
- Check PostgreSQL version matches
- Try syncing one database at a time

---

## What Gets Synced?

### Tiffany (Sudirman) - tiffany_local
- 303 tables
- 34,405 products (item)
- 199,550 sales transactions
- 245,748 receiving documents
- 573,944 journal entries
- 685,300 stock movement logs
- **Total**: ~12.2 million records

### CVReyga (Gambir) - cvreyga_local
- 456 tables
- 43,009 products (item)
- 34,561 sales transactions
- 34,507 receiving documents
- 81,909 journal entries
- 165,512 stock movement logs
- **Total**: ~7.8 million records

---

## Next Features

Once synced, you can:

1. **🔍 Search Products**
   - Across both branches simultaneously
   - By code or description
   - With AI-powered semantic search

2. **📊 Compare Branches**
   - Stock levels
   - Pricing differences
   - Sales trends
   - Performance metrics

3. **📈 Analytics Dashboard**
   - Sales charts
   - Inventory analysis
   - Fast/slow moving items
   - Zero stock alerts

4. **📥 Data Export**
   - Export to CSV
   - Export to Excel
   - Generate reports

---

## Need Help?

1. **Read**: [AUTOSYNC_README.md](AUTOSYNC_README.md)
2. **Check**: Google Drive for latest backups
3. **Review**: Error messages in terminal
4. **Verify**: PostgreSQL is running

---

**Ready?** Run: `node autosync-menu.js` 🚀
