<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
header('Content-Type: application/json; charset=utf-8');

if (isset($_POST['user_id']) && isset($_POST['auth'])) {
    include 'config.php';

    function check_input($par) {
        return htmlspecialchars(stripslashes(trim($par)));
    }

    $user_id = mysqli_real_escape_string($con, $_POST['user_id']);
    $auth = mysqli_real_escape_string($con, $_POST['auth']);
    $company_name = check_input($_POST['company_name'] ?? '');
    $address = check_input($_POST['address'] ?? '');
    $city = check_input($_POST['city'] ?? '');
    $state = check_input($_POST['state'] ?? '');
    $email = check_input($_POST['email'] ?? '');
    $country = check_input($_POST['country'] ?? '');
    $whatsapp = check_input($_POST['whatsapp'] ?? '');
    $phone = check_input($_POST['phone'] ?? '');
    $email = check_input($_POST['email'] ?? '');
    $website = check_input($_POST['website'] ?? '');
    $reg_no = check_input($_POST['reg_no'] ?? '');

    // Validate all required fields
    $required_fields = [
        'user_id', 'auth', 'company_name', 'address', 'city', 'email',
        'state', 'country', 'whatsapp', 'phone', 'email', 'website', 'reg_no'
    ];

    foreach ($required_fields as $field) {
        if (empty($$field)) {
            echo json_encode(['status' => false, 'success' => ucfirst($field) . ' is required']);
            exit();
        }
    }

    // Check if the user exists
    $check_user_query = $con->prepare("SELECT * FROM users WHERE user_id = ? AND auth = ?");
    if (!$check_user_query) {
        echo json_encode(['status' => false, 'success' => 'Database error: ' . $con->error]);
        exit();
    }
    $check_user_query->bind_param("ss", $user_id, $auth);
    $check_user_query->execute();
    $user_result = $check_user_query->get_result();

    if ($user_result->num_rows === 0) {
        echo json_encode(['status' => false, 'success' => 'User not found']);
        exit();
    }

    // Prepare the SQL insert query
    $insert_query = $con->prepare(
        "INSERT INTO company (user_id, auth, company_name, email, address, city, state, country, whatsapp, phone, reg_no, website) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    );

    if (!$insert_query) {
        echo json_encode(['status' => false, 'success' => 'Database error: ' . $con->error]);
        exit();
    }

    // Bind parameters
    $insert_query->bind_param(
        "ssssssssssss",
        $user_id,
        $auth,
        $company_name,
        $email,
        $address,
        $city,
        $state,
        $country,
        $whatsapp,
        $phone,
        $reg_no,
        $website
    );

    // Execute the insert query
    if ($insert_query->execute()) {
        $status = true;
        $suc = "Company profile successfully created";
    } else {
        $status = false;
        $suc = "Insert failed: " . $insert_query->error;
    }

    $check_user_query->close();
    $insert_query->close();
    $con->close();

    // Output JSON response
    echo json_encode([
        'status' => $status,
        'success' => $suc,
        'user_id' => $user_id,
        'auth' => $auth,
    ]);
} else {
    echo json_encode(['status' => false, 'success' => 'Invalid request']);
}
?>
