# CloudVault 2.0 – REST API Reference

The CloudVault 2.0 Centralized Backend provides RESTful JSON endpoints authenticated via JWT Bearer tokens.

Base URL: `http://localhost:5000` (or `http://<LAN_IP>:5000`)

---

## 🔒 Authentication Headers
For protected endpoints, supply:
```
Authorization: Bearer <jwt_session_token>
```

---

## 1. Authentication Endpoints

### `POST /api/auth/register`
Creates a new user account with deterministic UUIDv5 identifier.

**Request Body:**
```json
{
  "name": "Aman Sen",
  "email": "amansen2710@gmail.com",
  "password": "MyPassword123",
  "deviceId": "hardware_uuid_101",
  "deviceName": "Xiaomi Redmi Note 13",
  "deviceType": "android"
}
```

**Response (201 Created):**
```json
{
  "message": "User registered successfully",
  "user": {
    "id": "59a84376-45df-5bf5-84ed-9335c573a70d",
    "name": "Aman Sen",
    "email": "amansen2710@gmail.com",
    "accountStatus": "active"
  },
  "token": "eyJhbGciOi...",
  "refreshToken": "sec_refresh_...",
  "expiresAt": "2026-12-16T10:00:00Z"
}
```

---

### `POST /api/auth/login`
Authenticates user, registers device, and returns full cloud sync payload for instant restore.

**Request Body:**
```json
{
  "email": "amansen2710@gmail.com",
  "password": "MyPassword123",
  "deviceId": "hardware_uuid_202",
  "deviceName": "Phone B or Laptop Browser",
  "deviceType": "web"
}
```

**Response (200 OK):**
```json
{
  "message": "Login successful",
  "user": { "id": "...", "name": "...", "email": "..." },
  "token": "eyJhbGci...",
  "connectedAccounts": [...],
  "backupHistory": [...],
  "settings": {...}
}
```

---

### `POST /api/auth/refresh`
Issues a new JWT access token using a valid refresh token.

---

### `POST /api/auth/logout`
Revokes active device session.

---

### `GET /api/auth/me`
Retrieves current profile, active devices, settings, and linked accounts.

---

## 2. Devices Endpoints

### `GET /api/devices`
Lists all devices registered under the user's account.

### `POST /api/devices`
Registers a new client device or updates active timestamp.

### `DELETE /api/devices/<device_id>`
Revokes a device's session.

---

## 3. Google Drive & Accounts Endpoints

### `GET /api/drive/accounts`
Lists all linked Google Drive storage accounts with live quota and active status.

### `POST /api/drive/accounts`
Links a new Google account for storage automation.

### `DELETE /api/drive/accounts/<account_id>`
Unlinks a Google Drive account.

### `POST /api/drive/accounts/<account_id>/set-active`
Sets the specified account as the active primary backup target.

---

## 4. Storage Quota Endpoints

### `GET /api/storage/quota`
Returns aggregated and per-account Google Drive quota metrics:
- `totalQuota` (e.g. 15 GB)
- `totalUsed` (actual space occupied)
- `totalAvailable` (free bytes remaining)
- `totalCloudVaultUsage` (bytes used by CloudVault files)
- `percentUsed` (percentage of total quota utilized)

### `GET /api/storage/targets` & `POST /api/storage/targets`
Inspects and updates target storage limits and reserve buffer per account.

---

## 5. Files & Upload Endpoints

### `GET /api/drive/files`
Query backed-up files with search, category filtering, and sorting.

**Query Parameters:**
- `category`: `all`, `photos`, `videos`, `documents`, `other`
- `search`: Case-insensitive search on filename
- `sort`: `date`, `name`, `size`
- `order`: `desc`, `asc`
- `page`: Page index (default: 1)
- `limit`: Number of items per page (default: 50)

### `POST /api/drive/upload-file`
Direct multipart/form-data upload uploader used by laptop browsers and mobile clients. Automatically categorizes into `My Drive/CloudVault/Photos`, `Videos`, or `Documents`.

### `GET /api/drive/download/<file_id>`
Downloads file content or redirects to authoritative Google Drive content stream.

### `POST /api/drive/sync`
Performs cloud synchronization and remote file discovery audit.

---

## 6. Backup History Endpoints

### `GET /api/backup/history`
Returns chronological audit trail of all upload, download, and sync events with source device identification.

---

## 7. Settings Endpoints

### `GET /api/settings` & `PUT /api/settings`
Retrieves and updates automated background backup rules, Wi-Fi only restrictions, file types, and quality settings.
