<?php
// Include the config file which should establish the connection
include 'config.php';

// Create connection
$con = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}

$msg= "";

if($_SERVER["REQUEST_METHOD"] == "POST") {

    if(isset($_POST['register'])) {
        // Validation and registration logic...
        if(empty(trim($_POST["username"]))){
            $username_err = "Please enter a username.";
        } elseif(!preg_match('/^[a-zA-Z0-9]+$/', trim($_POST["username"]))){
            $username_err = "Username can only contain letters, numbers, and underscores, retard.";
        } else{
            $sql = "SELECT id FROM users WHERE username = ?";
            if($stmt = mysqli_prepare($con, $sql)){
                mysqli_stmt_bind_param($stmt, "s", $param_username);
                $param_username = trim($_POST["username"]);
                if(mysqli_stmt_execute($stmt)){
                    mysqli_stmt_store_result($stmt);

                    if(mysqli_stmt_num_rows($stmt) == 1){
                        $username_err = "Username is already taken. Be original, skid.";
                    } else{
                        $username = trim($_POST["username"]);
                    }
                } else{
                    $msg = "Oops! Something went wrong. Please try again later... or not.";
                }

                mysqli_stmt_close($stmt);
            }
        }

        if(empty(trim($_POST["password"]))){
            $password_err = "Please enter a password, dumbass.";
        } elseif(strlen(trim($_POST["password"])) < 8){
            $password_err = "Password must have at least 8 characters, #OPSECLikeGravy";
        } else{
            $password = trim($_POST["password"]);
        }

        if(empty(trim($_POST["confirm_password"]))){
            $confirm_password_err = "Please confirm your fucking password.";
        } else{
            $confirm_password = trim($_POST["confirm_password"]);
            if(empty($password_err) && ($password != $confirm_password)){
                $confirm_password_err = "Password did not match, fucking idiot.";
            }
        }

        if(empty(trim($_POST["email"]))){
            $email_err = "Please enter an email, larper.";
        } else {
            $email = trim($_POST["email"]);
        }

        if(empty($username_err) && empty($password_err) && empty($confirm_password_err) && empty($email_err)){
            $param_username = $username;
            $param_password = password_hash($password, PASSWORD_DEFAULT);
            $param_email = $email;

            $sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
            if($stmt = mysqli_prepare($con, $sql)){
                mysqli_stmt_bind_param($stmt, "sss", $param_username, $param_email, $param_password);
                
                if(mysqli_stmt_execute($stmt)){
                    header("Location: ./login.php");
                } else{
                    $msg = "Oops! Something went wrong. Please try again later... or not.";
                }

                mysqli_stmt_close($stmt);
            }
        }
    }
}

$conn->close();
?>

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        body {
            font-family: 'Courier New', Courier, monospace;
            background-color: #060606;
            color: #cccccc;
            text-align: center;
        }
        .container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .register-box {
            background-color: #0c0c0c;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
            width: 300px;
        }
        h2 {
            color: #fff;
            margin-bottom: 20px;
        }
        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: none;
            border-radius: 4px;
            background-color: #2b2b2b;
            color: #cccccc;
        }
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: #1b1b1b;
            font-weight: bold;
        }
        input[type="submit"]:hover {
            background-color: #fff;
        }
        .message {
            color: #ff0000;
            margin-top: 10px;
        }
        a {
            color: #fff;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="register-box">
            <h2>Register</h2>
            <form action="" method="POST">
                <div>
                    <span class="input-group-text" id="basic-addon1">Username</span>
                    <input type="text" placeholder="Username" name="username" aria-label="Username" aria-describedby="basic-addon1">
                </div>
                <div class="input-group mb-1">
                    <span class="input-group-text" id="basic-addon1">Email</span>
                    <input type="text" placeholder="Email" name="email" aria-label="Email" aria-describedby="basic-addon1">
                </div>
                <div>
                    <span class="input-group-text" id="basic-addon1">Password</span>
                    <input type="password" placeholder="Password" name="password" aria-label="password" aria-describedby="basic-addon1">
                </div>
                <div>
                    <span class="input-group-text" id="basic-addon1">Confirm Password</span>
                    <input type="password" placeholder="Password" name="confirm_password" aria-label="confirm_password" aria-describedby="basic-addon1">
                </div>
                <input type="submit" name="register" value="Register">
            </form>
            <p>Already have an account? <a href="login.php">Login</a></p>
        </div>
    </div>
</body>

<script protect="" src="js/protect.js"></script>

</html>
