<?php
session_start();
include 'connect.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

if (isset($_POST['update'])) {
    // Get form data
    $first_name = trim($_POST['first_name']);
    $last_name = trim($_POST['last_name']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $username = trim($_POST['username']);
    $address = trim($_POST['address']);

 
    try {
        $conn->begin_transaction();

        // 1. First update the users table
        $query_users = "UPDATE users 
                       SET firstname = ?, lastname = ?, email = ?, phone = ? ,username =?
                       WHERE user_id = ?";
        $stmt_users = $conn->prepare($query_users);
        if (!$stmt_users) {
            throw new Exception("Prepare failed for users table: " . $conn->error);
        }
        $stmt_users->bind_param("sssssi", $first_name, $last_name, $email, $phone, $username,$user_id);
        

        if (!$stmt_users->execute()) {
            throw new Exception("Error updating users table: " . $stmt_users->error);
        }

        // 2. Then handle the Employers table
        // First check if a record exists
        $check_query = "SELECT COUNT(*) as count FROM employers WHERE user_id = ?";
        $check_stmt = $conn->prepare($check_query);
        $check_stmt->bind_param("i", $user_id);
        $check_stmt->execute();
        $result = $check_stmt->get_result();
        $row = $result->fetch_assoc();

        if ($row['count'] > 0) {
            // Update existing record
            $employer_query = "UPDATE employers 
                                SET 
                                    address = ?
                        
                                WHERE user_id = ?";
        } else {
            // Insert new record
            $employer_query = "INSERT INTO employers 
                                ( address, user_id) 
                                VALUES (?, ?)";
        }

        $stmt_employers = $conn->prepare($employer_query);
        if (!$stmt_employers) {
            throw new Exception("Prepare failed for Employer table: " . $conn->error);
        }

        // Same binding for both UPDATE and INSERT
        $stmt_employers->bind_param("si", 
 $address, 
            $user_id
        );

        if (!$stmt_employers->execute()) {
            throw new Exception("Error with E ployers table: " . $stmt_employers->error);
        }

        $conn->commit();
        echo "<script>alert('Profile updated successfully! Relogin to see the changes'); window.location.href = 'login';</script>";

        exit();

    } catch (Exception $e) {
        $conn->rollback();
        $_SESSION['error_message'] = "Error: " . $e->getMessage();
        error_log("Profile update error: " . $e->getMessage());
        header("Location: employerprofile");
        exit();
    } finally {
        if (isset($stmt_users)) $stmt_users->close();
        if (isset($stmt_employers)) $stmt_employers->close();
        if (isset($check_stmt)) $check_stmt->close();
        $conn->close();
    }
}
?>