# Betfair Backend API

Node.js + TypeScript backend for the Betfair gaming platform.

## Setup

1. Install dependencies:
```bash
npm install
```

2. Configure environment variables in `.env`:
```env
PORT=3000
DB_HOST=localhost
DB_USER=root
DB_PASS=
DB_NAME=betfair
DB_PORT=3306
JWT_SECRET=your_secret_key
```

3. Build the project:
```bash
npm run build
```

4. Start the server:
```bash
npm start
```

Or for development with auto-reload:
```bash
npm run watch
```

## API Endpoints

### Public APIs
- `GET /api/games.php` - Get all active games
- `POST /api/auth/login.php` - Login
- `POST /api/auth/register.php` - Register
- `POST /api/game-launch.php` - Launch a game
- `GET /api/promo.php` - Get promotions
- `GET /api/mail.php` - Get user mails
- `POST /api/mail.php` - Send mail

### User APIs (require token)
- `POST /api/wallet.php` - Wallet operations
- `POST /api/bet.php` - Place bet
- `POST /api/deposit.php` - Create deposit request
- `POST /api/withdraw.php` - Create withdrawal request
- `GET /api/rebate.php` - Get rebates
- `POST /api/rebate.php/claim` - Claim rebate
- `GET /api/referral.php` - Get referrals
- `POST /api/result.php` - Submit game result
- `GET /api/security.php` - Get security info
- `POST /api/security.php/change-password` - Change password
- `GET /api/suggestion.php` - Get suggestions
- `POST /api/suggestion.php` - Submit suggestion
- `GET /api/vip.php` - Get VIP info

### Admin APIs
- `GET /api/admin/settings.php` - Get settings
- `POST /api/admin/settings.php` - Update settings
- `GET /api/admin/users.php` - Get all users
- `POST /api/admin/users.php/:id/block` - Block user
- `POST /api/admin/users.php/:id/unblock` - Unblock user
- `GET /api/admin/deposit_handler.php` - Get deposit requests
- `POST /api/admin/deposit_handler.php` - Handle deposit request
- `GET /api/admin/withdraw_handler.php` - Get withdrawal requests
- `POST /api/admin/withdraw_handler.php` - Handle withdrawal request
- `GET /api/admin/overview.php` - Dashboard overview
- `GET /api/admin/payment_methods.php` - Get payment methods
- `POST /api/admin/payment_methods.php` - Add payment method
- `GET /api/admin/plugins.php` - Get all games
- `POST /api/admin/plugins.php/:id/toggle` - Toggle game status
- `GET /api/admin/promo.php` - Get all promos
- `POST /api/admin/promo.php` - Create promo
- `GET /api/admin/referral_management.php` - Get referrals
- `GET /api/admin/transaction_history.php` - Get transactions
- `GET /api/admin/monitor.php` - Monitor active sessions
- `GET /api/admin/home_management.php/banners` - Get banners
- `POST /api/admin/home_management.php/banners` - Create banner
- `GET /api/admin/home_management.php/announcements` - Get announcements
- `POST /api/admin/home_management.php/announcements` - Create announcement
- `GET /api/admin/mail.php` - Get all mails
- `POST /api/admin/mail.php/:id/reply` - Reply to mail
- `GET /api/admin/suggestion.php` - Get all suggestions
- `GET /api/admin/vip_management.php` - Get VIP settings
- `POST /api/admin/vip_management.php` - Update VIP settings
- `GET /api/admin/turnover_management.php` - Get turnover plans
- `POST /api/admin/turnover_management.php` - Create turnover plan
- `POST /api/admin/lottery_draw.php` - Record lottery draw
- `GET /api/admin/lottery_draw.php` - Get lottery draws
- `GET /api/admin/lottery_management.php` - Get lottery games

## Database

Requires MySQL database with the following main tables:
- players
- games
- game_sessions
- bets
- transactions
- deposit_requests
- withdrawal_requests
- mail
- suggestions
- rebates
- promos
- banners
- announcements
- system_settings
- clients
- payment_methods
- vip_settings
- turnover_plans
- lottery_draws

## Project Structure

```
backend/
├── src/
│   ├── api/
│   │   ├── admin/          # Admin routes
│   │   ├── auth/           # Authentication routes
│   │   ├── games.ts
│   │   ├── wallet.ts
│   │   ├── bet.ts
│   │   ├── deposit.ts
│   │   ├── withdraw.ts
│   │   └── ...
│   ├── db/
│   │   └── database.ts     # Database connection
│   ├── includes/
│   │   ├── merchantAPI.ts  # Balance operations
│   │   └── pluginManager.ts # Plugin management
│   ├── scripts/
│   │   └── update_games_table.ts
│   └── index.ts            # Main entry point
├── uploads/                # Uploaded files
├── plugins/                # Game plugins
├── .env                    # Environment variables
├── package.json
└── tsconfig.json