<?php
session_start();

header("Access-Control-Allow-Origin: https://enerliths.com");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Content-Type: application/json");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}
require "../config/db.php";

$user_id = $_SESSION["user_id"];

$file = $_FILES["avatar"];

$path = "../uploads/avatars/" . time() . "_" . $file["name"];

move_uploaded_file($file["tmp_name"],$path);

$stmt = $conn->prepare("
UPDATE user_profiles
SET avatar=?
WHERE user_id=?
");

$stmt->bind_param("si",$path,$user_id);
$stmt->execute();

echo json_encode(["success"=>true]);