<?php
include 'config.php';
header('Content-Type: application/json');

$response = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $type = $_POST['type'] ?? '';

    // ================= LOGIN =================
    if ($type === 'login') {
        $username = $_POST['username'] ?? '';
        $password = $_POST['password'] ?? '';

        $stmt = $conn->prepare("SELECT username, password FROM admin WHERE username = ?");
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $admin = $result->fetch_assoc();

            if ($admin['password'] === $password) {
                $response = [
                    "status" => "success",
                    "message" => "Login Success",
                    "username" => $admin['username'],
                    "password" => $admin['password']
                ];
            } else {
                $response = ["status" => "error", "message" => "Invalid Password"];
            }
        } else {
            $response = ["status" => "error", "message" => "No Admin Found"];
        }

        $stmt->close();
    }

    // ================= CHANGE PASSWORD =================
    elseif ($type === 'change') {
        $old_pass = $_POST['old_pass'] ?? '';
        $new_pass = $_POST['new_pass'] ?? '';

        $stmt = $conn->prepare("SELECT username FROM admin WHERE password = ?");
        $stmt->bind_param("s", $old_pass);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $admin = $result->fetch_assoc();
            $username = $admin['username'];

            $update = $conn->prepare("UPDATE admin SET password = ? WHERE username = ?");
            $update->bind_param("ss", $new_pass, $username);

            if ($update->execute()) {
                $response = [
                    "status" => "success",
                    "message" => "Changed Successfully",
                    "username" => $username,
                    "password" => $new_pass
                ];
            } else {
                $response = ["status" => "error", "message" => "Error Changing: " . $conn->error];
            }

            $update->close();
        } else {
            $response = ["status" => "error", "message" => "Old Password Is Incorrect"];
        }

        $stmt->close();
    }

    // ================= MODERATORS LOGIN =================
    elseif ($type === 'Moderators') {
        $username = $_POST['username'] ?? '';
        $password = $_POST['password'] ?? '';

        // শুধুমাত্র admin টেবিলের id = 2 চেক করবে
        $stmt = $conn->prepare("SELECT username, password FROM admin WHERE id = 2");
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $admin = $result->fetch_assoc();

            if ($admin['username'] === $username && $admin['password'] === $password) {
                $response = [
                    "status" => "success",
                    "message" => "Moderator Login Success",
                    "username" => $admin['username'],
                    "password" => $admin['password']
                ];
            } else {
                $response = ["status" => "error", "message" => "Moderator Login Failed"];
            }
        } else {
            $response = ["status" => "error", "message" => "Moderator Account Not Found"];
        }

        $stmt->close();
    }

    // ================= INVALID TYPE =================
    else {
        $response = ["status" => "error", "message" => "Invalid Type"];
    }

    $conn->close();
} else {
    $response = ["status" => "error", "message" => "Invalid Request Method"];
}

echo json_encode($response);
?>