# Asset Settings Feature — Implementation Report

## Summary

Two admin features added to Optiqo:
- **Per-asset payout percentage control** — admin can set payout % (50–95) per symbol
- **Per-asset market open/close toggle** — admin can open or close any market; frontend blocks trading

---

## Files Created

| File | Purpose |
|------|---------|
| `database/migrations/2024_01_08_000001_create_asset_settings_table.php` | Creates `asset_settings` table |
| `app/Models/AssetSetting.php` | Eloquent model with `getForSymbol()` helper |
| `database/seeders/AssetSettingSeeder.php` | Seeds all 50 assets with default payouts |
| `app/Http/Controllers/API/V1/AssetSettingsController.php` | Public API: `GET /api/v1/assets/settings` |

## Files Modified

| File | Change |
|------|--------|
| `app/Http/Controllers/Admin/AdminSettingsController.php` | Added `updateAssets()`, updated `index()` to pass `$assetSettings` |
| `app/Http/Controllers/API/V1/TradeController.php` | Checks `AssetSetting::is_open` before opening trade (422 if closed) |
| `app/Services/TradeService.php` | Uses `AssetSetting::payout_percent` instead of hardcoded `Asset::payout_percentage` |
| `routes/web.php` | Added `PUT /ponir/settings/assets` → `AdminSettingsController@updateAssets` |
| `routes/api.php` | Added `GET /api/v1/assets/settings` (public, no auth) |
| `resources/views/admin/settings/index.blade.php` | Full asset management table with toggle buttons |
| `resources/js/store/tradingStore.js` | Added `fetchAssetSettings()`, market-closed guard in `placeTrade()` |
| `resources/js/Components/AppInit.jsx` | Calls `fetchAssetSettings()` on mount |
| `resources/js/Components/ui/TradeControls.jsx` | `UpButton`/`DownButton` disabled when closed; `MarketClosedOverlay` exported |
| `resources/js/Components/layout/MobileLayout.jsx` | `MControls` shows `MarketClosedOverlay` when `is_open === false` |
| `resources/js/Components/layout/DesktopLayout.jsx` | `QRight` shows `MarketClosedOverlay` when `is_open === false` |
| `resources/js/Components/AssetPicker.jsx` | Shows 🔒 lock icon next to closed assets |

---

## Default Payouts Seeded

| Category    | Assets | Default Payout |
|-------------|--------|---------------|
| Forex       | 20     | 85%           |
| Crypto      | 15     | 80%           |
| Commodities | 8      | 83%           |
| Indices     | 7      | 79%           |
| **Total**   | **50** |               |

---

## How It Works

### Admin Side
1. Visit `/ponir/settings`
2. Scroll to **Asset Management** section
3. Edit payout % inline (50–95, step 0.5)
4. Click **✓ OPEN** / **✕ CLOSED** button to toggle market status
5. Click **Save Asset Settings** — bulk `PUT /ponir/settings/assets`

### Frontend
- On app mount, `fetchAssetSettings()` calls `GET /api/v1/assets/settings`
- Merges `payout_percent` and `is_open` into every asset in the store
- If `is_open === false`:
  - Red lock overlay covers the trade panel (mobile `m-controls`, desktop `q-right`)
  - UP/DOWN buttons are disabled (`opacity: 0.4`, `cursor: not-allowed`)
  - AssetPicker shows 🔒 icon next to the symbol
  - `placeTrade()` shows toast and returns early before hitting the API

### Backend Guard
- `TradeController::open()` checks `AssetSetting::is_open` — returns HTTP 422 with `"This market is currently closed."` if the market is off
- `TradeService::openMarketTrade()` reads payout from `AssetSetting::payout_percent` (falls back to `Asset::payout_percentage` if no row exists)

---

## Commands Run

```bash
php artisan migrate --path=database/migrations/2024_01_08_000001_create_asset_settings_table.php
php artisan db:seed --class=AssetSettingSeeder
php artisan config:clear
```

---

## Test Checklist

- [ ] Visit `/ponir/settings` → Asset Management section visible with 50 rows
- [ ] Change BTCUSDT payout to 75% → click Save → refresh → value persists
- [ ] Toggle BTCUSDT to **CLOSED** → Save
- [ ] Open trading platform, select BTC/USDT → red lock overlay appears
- [ ] UP/DOWN buttons are disabled (greyed out)
- [ ] AssetPicker shows 🔒 next to BTC/USDT
- [ ] Attempt to place trade via API directly → 422 "This market is currently closed."
- [ ] Re-open BTCUSDT → overlay disappears, buttons re-enable
