<?php
require_once __DIR__ . '/../_pdo_db_connect.php';
require_once __DIR__ . '/../_jwt.php';
require_once __DIR__ . '/../_response.php';
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) json_res(['error'=>'invalid_input'],400);
$identifier = trim($input['identifier'] ?? '');
$password = $input['password'] ?? '';
if (!$identifier || !$password) json_res(['error'=>'missing_fields'],400);
$pdo = get_db();
$stmt = $pdo->prepare("SELECT id,username,password_hash,role FROM as_team_auth WHERE username=:u OR email=:u LIMIT 1");
$stmt->execute([':u'=>$identifier]);
$user = $stmt->fetch();
if (!$user || !password_verify($password, $user['password_hash'])){
    json_res(['error'=>'invalid_credentials'],401);
}
$payload = ['sub'=>$user['id'],'role'=>$user['role']];
$token = make_jwt($payload);
json_res(['token'=>$token,'role'=>$user['role']]);
