# PES League — Setup Guide

## Akses Lokal (XAMPP)

URL: `http://localhost/pes-league/public`

---

## Langkah Setup

### 1. Install PHP Dependencies

```bash
composer install
```

### 2. Konfigurasi Environment

```bash
cp .env.example .env
php artisan key:generate
```

Edit `.env` sesuai environment kamu:

```env
# Database (MySQL XAMPP default)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pes_league
DB_USERNAME=root
DB_PASSWORD=

# Discord OAuth
DISCORD_CLIENT_ID=your_client_id
DISCORD_CLIENT_SECRET=your_client_secret
DISCORD_REDIRECT_URI=http://localhost/pes-league/public/auth/discord/callback
DISCORD_BOT_TOKEN=your_bot_token
DISCORD_GUILD_ID=your_server_id
```

### 3. Setup Database

```bash
# Buat database (MySQL)
mysql -u root -e "CREATE DATABASE pes_league CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

# Jalankan semua migration
php artisan migrate

# Seed data awal (16 sample players + Season 1)
php artisan db:seed

# Setup permissions & roles
php artisan db:seed --class=PermissionSeeder
```

### 4. Build Assets (butuh Node.js)

Jika Node.js tersedia:
```bash
npm install
npm run build
```

Jika Node.js tidak tersedia, aplikasi sudah menggunakan **CDN fallback** otomatis
(Bootstrap 5 + Tabler Icons + Alpine.js via CDN).

### 5. Jalankan Server

```bash
php artisan serve
# Akses: http://localhost:8000
```

Atau via XAMPP: `http://localhost/pes-league/public`

---

## Discord Developer Portal Setup

1. Buka https://discord.com/developers/applications
2. Buat New Application → beri nama "PES League"
3. Buka **OAuth2** → copy Client ID & Client Secret ke `.env`
4. Tambah Redirect URI: `http://localhost/pes-league/public/auth/discord/callback`
5. Pastikan scope: `identify`, `email`
6. Buka **Bot** → Add Bot → copy Token ke `DISCORD_BOT_TOKEN`
7. Invite bot ke server dengan permission:
   - `Manage Channels` — untuk membuat match rooms
   - `Send Messages` — untuk notifikasi
   - `Move Members` — untuk voice channel

---

## Jalankan Matchmaking Scheduler

Untuk matchmaking bekerja otomatis, jalankan scheduler:

```bash
# Windows (manual setiap menit)
php artisan matchmaking:tick

# Atau jalankan scheduler (butuh cron/task scheduler)
php artisan schedule:run
```

**Windows Task Scheduler** — tambahkan task:
- Program: `C:\xampp\php\php.exe`
- Arguments: `C:\xampp\htdocs\pes-league\artisan matchmaking:tick`
- Interval: Every 1 minute

---

## Assign Admin Role

Setelah login pertama kali dengan Discord, jalankan:

```bash
php artisan tinker
>>> $user = App\Models\User::where('username', 'YourUsername')->first();
>>> $user->assignRole('admin');
```

---

## Struktur URL

| URL | Deskripsi |
|-----|-----------|
| `/` | Halaman beranda |
| `/login` | Halaman login Discord |
| `/dashboard` | Dashboard pemain (auth) |
| `/leaderboard` | Ranking global |
| `/play` | Cari lawan (matchmaking) |
| `/profile` | Profil sendiri |
| `/player/{id}` | Profil pemain lain |
| `/match/{id}` | Detail match room |
| `/match/history` | Riwayat match |
| `/tournament` | Daftar turnamen |
| `/admin` | Admin panel |
| `/auth/discord` | Login Discord OAuth |

---

## API Endpoints (Desktop Launcher)

| Method | Endpoint | Auth | Deskripsi |
|--------|----------|------|-----------|
| POST | `/api/login` | — | Login dengan Discord token |
| GET | `/api/profile` | Bearer | Profil sendiri |
| GET | `/api/leaderboard` | — | Top 100 pemain |
| GET | `/api/history` | Bearer | Riwayat match |
| POST | `/api/matchmaking/join` | Bearer | Masuk antrian |
| POST | `/api/matchmaking/leave` | Bearer | Keluar antrian |
| GET | `/api/matchmaking/status` | Bearer | Status antrian |
| POST | `/api/submit-result` | Bearer | Submit skor |
| GET | `/api/season` | — | Season aktif |
| GET | `/api/tournament` | — | Daftar turnamen |
