# Workspace Transfer Ownership

Transfer a workspace to another user. The original owner is demoted to editor; the new owner gains full control.

## How to Use

1. Open a workspace you own.
2. In the header bar, click **Transfer** (next to the Share button). Only visible to workspace owners.
3. Enter the new owner's email address.
4. Click **Transfer** to confirm.

After transfer:
- The new owner has full control (rename, delete, manage members, share, etc.)
- You become an **editor** and can still send messages and modify sessions, but can no longer manage workspace settings or delete the workspace.

## API Reference

### `PUT /api/workspaces/{workspace_id}/transfer`

Transfer workspace ownership to another registered user.

**Auth:** Bearer token (workspace owner only)

**Request body:**

```json
{
  "new_owner_email": "newowner@example.com"
}
```

**Responses:**

| Status | Description |
|--------|-------------|
| 200 | `{"status": "ok", "new_owner_email": "newowner@example.com"}` |
| 400 | User not found by email, or already the owner |
| 403 | Caller is not the workspace owner |
| 404 | Workspace not found |

## What Happens Internally

1. `workspaces.user_id` is updated to the new owner's user ID.
2. The old owner's role in `workspace_members` is changed from `owner` to `editor`.
3. The new owner is upserted into `workspace_members` with role `owner`.

All three changes happen in a single transaction.

## Frontend Integration

```typescript
import { transferWorkspaceOwner } from '@/lib/workspaces';

await transferWorkspaceOwner(workspaceId, 'newowner@example.com');
```

After a successful transfer, the frontend reloads workspace metadata so `isOwner` / `canEdit` flags update and the UI reflects the caller's new role.
