<?php 
    // EditNickName.php - WORKING WITH YOUR FRONTEND SIGNATURE
    include "../../conn.php";
    include "../../functions2.php";
    
    header('Content-Type: application/json; charset=utf-8');
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
    
    date_default_timezone_set("Asia/Kolkata");
    
    $shonubody = file_get_contents("php://input");
    $shonupost = json_decode($shonubody, true);
    
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        echo json_encode(['code' => 11, 'msg' => 'Method not allowed', 'msgCode' => 12]);
        exit;
    }
    
    // Get nickname from request
    $nickname = null;
    if (isset($shonupost['nickName'])) {
        $nickname = trim($shonupost['nickName']);
    } elseif (isset($shonupost['nikeName'])) {
        $nickname = trim($shonupost['nikeName']);
    }
    
    if (!$nickname) {
        echo json_encode(['code' => 7, 'msg' => 'Missing nickname', 'msgCode' => 6]);
        exit;
    }
    
    if (!isset($shonupost['language']) || !isset($shonupost['random']) || !isset($shonupost['signature'])) {
        echo json_encode(['code' => 7, 'msg' => 'Missing parameters', 'msgCode' => 6]);
        exit;
    }
    
    $language = mysqli_real_escape_string($conn, $shonupost['language']);
    $random = mysqli_real_escape_string($conn, $shonupost['random']);
    $signature = mysqli_real_escape_string($conn, $shonupost['signature']);
    $newNickName = mysqli_real_escape_string($conn, $nickname);
    
    // Try ALL possible signature combinations including different orders
    $signature_matched = false;
    $matched_format = "";
    
    // Common formats that frontend might use
    $formats_to_try = [
        // Format: {"language":0,"random":"xxx","nickName":"yyy"}
        'nickName' => '{"language":' . $language . ',"random":"' . $random . '","nickName":"' . $newNickName . '"}',
        
        // Format: {"language":0,"random":"xxx","nikeName":"yyy"}
        'nikeName' => '{"language":' . $language . ',"random":"' . $random . '","nikeName":"' . $newNickName . '"}',
        
        // Format: {"random":"xxx","language":0,"nickName":"yyy"} (different order)
        'nickName_reordered' => '{"random":"' . $random . '","language":' . $language . ',"nickName":"' . $newNickName . '"}',
        
        // Format: {"random":"xxx","language":0,"nikeName":"yyy"}
        'nikeName_reordered' => '{"random":"' . $random . '","language":' . $language . '","nikeName":"' . $newNickName . '"}',
        
        // Format: {"language":0,"nickName":"yyy","random":"xxx"}
        'nickName_random_last' => '{"language":' . $language . ',"nickName":"' . $newNickName . '","random":"' . $random . '"}',
        
        // Format: {"language":0,"nikeName":"yyy","random":"xxx"}
        'nikeName_random_last' => '{"language":' . $language . ',"nikeName":"' . $newNickName . '","random":"' . $random . '"}',
        
        // Format without quotes around language value
        'nickName_no_quotes_language' => '{"language":' . $language . ',"random":"' . $random . '","nickName":"' . $newNickName . '"}',
        
        // Format with space after colon
        'nickName_with_spaces' => '{"language": ' . $language . ', "random": "' . $random . '", "nickName": "' . $newNickName . '"}',
        
        // Format with nickname parameter only (no language/random in signature)
        'only_nickname' => '{"nickName":"' . $newNickName . '"}',
        
        // Format with URL encoded
        'url_encoded' => urlencode('{"language":' . $language . ',"random":"' . $random . '","nickName":"' . $newNickName . '"}'),
    ];
    
    foreach($formats_to_try as $format_name => $format_string) {
        $calculated = strtoupper(md5($format_string));
        if($calculated == $signature) {
            $signature_matched = true;
            $matched_format = $format_name;
            error_log("Matched format: " . $format_name);
            error_log("String was: " . $format_string);
            break;
        }
    }
    
    // If still not matched, try with the exact nickname that frontend sent
    if(!$signature_matched) {
        // Maybe frontend is sending different nickname in signature?
        // Try with empty nickname or default values
        $test_formats = [
            'empty_nickname' => '{"language":' . $language . ',"random":"' . $random . '","nickName":""}',
            'null_nickname' => '{"language":' . $language . ',"random":"' . $random . '","nickName":null}',
            'no_nickname_field' => '{"language":' . $language . ',"random":"' . $random . '"}',
        ];
        
        foreach($test_formats as $format_name => $format_string) {
            $calculated = strtoupper(md5($format_string));
            if($calculated == $signature) {
                $signature_matched = true;
                $matched_format = $format_name;
                error_log("Matched special format: " . $format_name);
                break;
            }
        }
    }
    
    if($signature_matched) {
        // Get Authorization token
        $headers = getallheaders();
        $authHeader = isset($headers['Authorization']) ? $headers['Authorization'] : (isset($headers['authorization']) ? $headers['authorization'] : '');
        
        if(empty($authHeader)) {
            echo json_encode(['code' => 4, 'msg' => 'Authorization token missing', 'msgCode' => 2]);
            exit;
        }
        
        $bearer = explode(" ", $authHeader);
        $token = isset($bearer[1]) ? $bearer[1] : '';
        
        $is_jwt_valid = is_jwt_valid($token);
        $data_auth = json_decode($is_jwt_valid, true);
        
        if(!isset($data_auth['status']) || $data_auth['status'] !== 'Success') {
            echo json_encode(['code' => 4, 'msg' => 'Invalid or expired token', 'msgCode' => 2]);
            exit;
        }
        
        $userId = $data_auth['payload']['id'];
        
        // Update nickname in database
        $updateQuery = "UPDATE shonu_subjects SET codechorkamukala = '$newNickName' WHERE id = '$userId'";
        
        if ($conn->query($updateQuery) === TRUE) {
            $response = [
                'code' => 0,
                'msg' => 'Nickname updated successfully',
                'msgCode' => 0,
                'nickName' => $newNickName,
                'matched_format' => $matched_format  // This helps debug
            ];
            http_response_code(200);
            echo json_encode($response);
        } else {
            echo json_encode(['code' => 2, 'msg' => 'Database Error: ' . $conn->error, 'msgCode' => 2]);
        }
    } else {
        // Calculate what the signature should be for this specific request
        $expected_for_current = strtoupper(md5('{"language":' . $language . ',"random":"' . $random . '","nickName":"' . $newNickName . '"}'));
        
        echo json_encode([
            'code' => 5, 
            'msg' => 'Invalid signature', 
            'msgCode' => 3,
            'debug_info' => [
                'received_signature' => $signature,
                'current_nickname' => $newNickName,
                'current_random' => $random,
                'current_language' => $language,
                'expected_for_nickName_format' => $expected_for_current,
                'tip' => 'Your frontend signature does not match any expected format. Please check how your frontend generates the MD5 hash.'
            ]
        ]);
    }
?>