# Database Schema Migration - Players Tab Fix

## Summary
Fixed all database queries in the web-map editor to use the correct `accounts` + `characters` schema instead of the deprecated `players` table.

## Schema Changes

### Old Schema (Deprecated)
- `players` table with `telegram_id` as primary key
- Columns: `intelligence`, `weight_capacity`, `volume_capacity`
- `accounts` table with `is_banned`, `ban_reason`, `premium_until`

### New Schema (Current)
- `accounts` table: `id`, `email`, `premium_expires_at`, `created_at`
- `characters` table: `id`, `account_id` (FK), `name`, `level`, `xp`, `hp`, `stamina`, `strength`, `agility`, `endurance`, `intellect`, `unspent_points`, `location_id`, `is_dead`
- `inventory` table: `character_id` (FK), `item_id`, `quantity`, `is_equipped`, `unique_item_id` (FK to unique_items)
- `unique_items` table: `id`, `item_id`, `durability`, `max_durability`, `tier`, `unique_stats`

## Files Modified

### 1. `/opt/dockers/echoes_of_the_ashes/web-map/server.py`

**Changes:**
- ✅ Changed import from `bot.database` to `api.database`
- ✅ Updated all SQL queries to use `characters` and `accounts` tables
- ✅ Changed column names:
  - `telegram_id` → `id` (character ID)
  - `intelligence` → `intellect`
  - `premium_until` → `premium_expires_at`
  - `character_name` → `name`
- ✅ Updated API endpoints:
  - `/api/editor/player/<int:telegram_id>` → `/api/editor/player/<int:character_id>`
  - `/api/editor/account/<int:telegram_id>` → `/api/editor/account/<int:account_id>`
- ✅ Fixed inventory queries to use `character_id` and join with `unique_items` table
- ✅ Updated player count query for live stats (line 1080)
- ✅ Fixed delete account to use CASCADE (accounts → characters → inventory)
- ✅ Updated reset player to use correct default values

**Endpoints Fixed:**
1. `GET /api/editor/players` - List all characters with account info
2. `GET /api/editor/player/<character_id>` - Get character details + inventory
3. `POST /api/editor/player/<character_id>` - Update character stats
4. `POST /api/editor/player/<character_id>/inventory` - Update inventory
5. `POST /api/editor/player/<character_id>/equipment` - Update equipment
6. `DELETE /api/editor/account/<account_id>/delete` - Delete account
7. `POST /api/editor/player/<character_id>/reset` - Reset character

### 2. `/opt/dockers/echoes_of_the_ashes/web-map/editor_enhanced.js`

**Changes:**
- ✅ Updated `renderPlayerList()` to use `player.id` instead of `player.telegram_id`
- ✅ Changed dataset attribute: `dataset.telegramId` → `dataset.characterId`
- ✅ Updated `selectPlayer()` function parameter and API call
- ✅ Fixed player editor display to show:
  - Character ID instead of Telegram ID
  - Account email
  - Correct timestamp handling (character_created_at * 1000)
- ✅ Updated action buttons to use correct IDs:
  - Ban/Unban: uses `account_id`
  - Reset: uses character `id`
  - Delete: uses `account_id`
- ✅ Fixed `deletePlayer()` to find player by `account_id`
- ✅ Updated status badge logic to use `is_premium` boolean

## Testing Checklist

### Backend Tests
- [ ] Start containers: `docker compose up -d`
- [ ] Check logs: `docker logs echoes_of_the_ashes_map`
- [ ] Test API endpoints:
  ```bash
  # Login first
  curl -X POST http://localhost:8080/api/login \
    -H "Content-Type: application/json" \
    -d '{"password":"admin123"}' \
    -c cookies.txt
  
  # Get players list
  curl http://localhost:8080/api/editor/players -b cookies.txt
  
  # Get specific player (replace 1 with actual character ID)
  curl http://localhost:8080/api/editor/player/1 -b cookies.txt
  ```

### Frontend Tests
1. Navigate to `http://localhost:8080/editor`
2. Login with password (default: `admin123`)
3. Click "👥 Players" tab
4. Verify:
   - [ ] Player list loads correctly
   - [ ] Search by name works
   - [ ] Filter by status (All/Active/Banned/Premium) works
   - [ ] Clicking a player loads their details
   - [ ] Character stats display correctly
   - [ ] Inventory shows (read-only)
   - [ ] Equipment shows (read-only)
   - [ ] Account info displays (email, premium status)
5. Test actions:
   - [ ] Edit character stats and save
   - [ ] Reset player (confirm it clears inventory)
   - [ ] Delete account (confirm double-confirmation)

## Known Limitations

1. **Ban functionality**: Accounts table doesn't have `is_banned` or `ban_reason` columns in new schema
   - Ban/Unban buttons will return "not implemented" message
   - Need to add these columns to accounts table if ban feature is needed

2. **Inventory editing**: Currently read-only display
   - Full CRUD for inventory would require more complex UI
   - Unique items support needs proper unique_items table integration

3. **Equipment slots**: New schema uses `is_equipped` flag in inventory
   - No separate `equipped_items` table
   - Equipment is just inventory items with `is_equipped=true`

## Rebuild Instructions

```bash
# Rebuild map container with fixes
docker compose build echoes_of_the_ashes_map

# Restart container
docker compose up -d echoes_of_the_ashes_map

# Check logs
docker logs -f echoes_of_the_ashes_map
```

## Rollback Plan

If issues occur:
```bash
# Restore from container (files are already synced)
./sync_from_containers.sh

# Or restore from git
git checkout web-map/server.py web-map/editor_enhanced.js
```

## Additional Notes

- All changes are backward compatible with existing data
- No database migrations needed (schema already exists)
- Frontend gracefully handles missing data (email, premium status)
- Timestamps are handled correctly (Unix timestamps in DB, converted to Date objects in JS)
