<?php
function insertStaffLog($conn, $institution_id, $branch_id, $faculty_id, $account_name, $account_no, $ifsc_code, $branch_address, $account_type, $basic_salary, $designation, $status) {
    try {
        // Check the last recorded entry for this faculty
        $query = "SELECT account_name, account_no, ifsc_code, branch_address, account_type, basic_salary, designation, status 
                  FROM staff_logs 
                  WHERE institution_id = ? AND branch_id = ? AND faculty_id = ? 
                  ORDER BY date_created DESC, time_created DESC 
                  LIMIT 1";

        $stmt = $conn->prepare($query);
        $stmt->execute([$institution_id, $branch_id, $faculty_id]);
        $lastLog = $stmt->fetch(PDO::FETCH_ASSOC);

        // If no previous log exists, insert new record
        if (!$lastLog) {
            return insertNewLogAndUpdateSalary($conn, $institution_id, $branch_id, $faculty_id, $account_name, $account_no, $ifsc_code, $branch_address, $account_type, $basic_salary, $designation, $status);
        }

        // Compare new data with last logged data, treating NULL values properly
        if (
            ($lastLog['account_name'] ?? '') == ($account_name ?? '') &&
            ($lastLog['account_no'] ?? '') == ($account_no ?? '') &&
            ($lastLog['ifsc_code'] ?? '') == ($ifsc_code ?? '') &&
            ($lastLog['branch_address'] ?? '') == ($branch_address ?? '') &&
            ($lastLog['account_type'] ?? '') == ($account_type ?? '') &&
            ($lastLog['basic_salary'] ?? '') == ($basic_salary ?? '') &&
            ($lastLog['designation'] ?? '') == ($designation ?? '') &&
            ($lastLog['status'] ?? '') == ($status ?? '')
        ) {
            return false; // No changes, do not insert a new log
        }

        // Insert new log if there are changes and update the salary
        return insertNewLogAndUpdateSalary($conn, $institution_id, $branch_id, $faculty_id, $account_name, $account_no, $ifsc_code, $branch_address, $account_type, $basic_salary, $designation, $status);

    } catch (PDOException $e) {
        return false;
    }
}

// Helper function to insert a new log and update the basic_salary in other_staff_details
function insertNewLogAndUpdateSalary($conn, $institution_id, $branch_id, $faculty_id, $account_name, $account_no, $ifsc_code, $branch_address, $account_type, $basic_salary, $designation, $status) {
    try {
        // Insert into staff_logs
        $query = "INSERT INTO staff_logs (institution_id, branch_id, faculty_id, account_name, account_no, ifsc_code, branch_address, account_type, basic_salary, designation, status, date_created, time_created) 
                  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURDATE(), CURTIME())";
        $stmt = $conn->prepare($query);
        $logInserted = $stmt->execute([
            $institution_id, $branch_id, $faculty_id,
            $account_name, $account_no, $ifsc_code, $branch_address, $account_type,
            $basic_salary, $designation, $status
        ]);

        if ($logInserted) {
            // Update the basic_salary field in other_staff_details table
            $updateQuery = "UPDATE other_staff_details SET basic_salary = ? WHERE faculty_id = ?";
            $updateStmt = $conn->prepare($updateQuery);
            return $updateStmt->execute([$basic_salary, $faculty_id]);
        }

        return false;
    } catch (PDOException $e) {
        return false;
    }
}
