# Hudl automation

## File structure (har kaam = alag file)

```
app/automation/
  selectors.py          ← saare XPath yahan (ek jagah update)
  tasks/
    popup.py            ← modal band karna
    login.py            ← login
    exchange.py         ← exchange open + data read
    (baad me) search.py, roster.py, ...
  routes/hudl_routes.py ← APIs
```

Naya feature = nayi file `tasks/xyz.py` + route + `runner.py` me naam add.

## Setup

```bash
cd backend
source venv/bin/activate
pip install playwright
playwright install chromium
uvicorn app.main:app --reload
```

## JWT required (sari Hudl APIs)

Pehle app login → token lo:

`POST /api/v1/auth/login` → `{ "access_token": "...", "token_type": "bearer" }`

Har Hudl API par header:

`Authorization: Bearer <access_token>`

## API flow (abhi ke liye)

| Step | API | Kaam |
|------|-----|------|
| 0 | `POST /api/v1/auth/login` | JWT token |
| 1 | `POST /api/v1/hudl/browser/start` | Browser kholo |
| 2 | `POST /api/v1/hudl/login` | Hudl website login |
| 3 | `POST /api/v1/hudl/exchange/open` | Hover Exchanges → Direct |
| 4 | `GET /api/v1/hudl/exchange/data` | Suggested teams + Active exchanges JSON |

## Frontend example

```javascript
const API = "http://127.0.0.1:8000/api/v1";

// 0) App login → JWT
const loginRes = await fetch(`${API}/auth/login`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ email: "you@mail.com", password: "yourpass" }),
});
const { access_token } = await loginRes.json();

const headers = {
  Authorization: `Bearer ${access_token}`,
  "Content-Type": "application/json",
};

// 1) Open exchange on real Hudl
await fetch(`${API}/hudl/exchange/open`, { method: "POST", headers });

// 2) Get data for clone UI
const res = await fetch(`${API}/hudl/exchange/data`, { headers });
const data = await res.json();

data.suggested_teams.forEach((t) => {
  console.log(t.school_name, t.team_name);
});

data.active_exchanges.forEach((e) => {
  console.log(e.time, e.name, e.process);
});
```

Search / select team — baad me `tasks/search.py` add karenge.
