<?php
function complaint_department_scope($admin)
{
    global $conn;
    $role = $_SESSION['role'] ?? '';
    $adminType = $_SESSION['admintype'] ?? '';
    $departmentId = (int)($_SESSION['department_id'] ?? ($admin['department_id'] ?? 0));
    $divisionId = (int)($_SESSION['division_id'] ?? ($admin['division_id'] ?? 0));
    $adminId = (int)($_SESSION['admin'] ?? ($admin['id'] ?? 0));

    if($role === 'ge_admin' && $divisionId > 0) {
        return array(
            'enabled' => true,
            'building_where' => "building_list.division_id='".$divisionId."'",
            'plain_where' => "users_complaint.b_list IN (SELECT bid FROM building_list WHERE division_id='".$divisionId."')",
            'building_filter' => " AND division_id='".$divisionId."'",
            'engineer_filter' => ''
        );
    }

    if (in_array($role, array('department_admin', 'je', 'contractor'), true)) {
        $complaintType = '';
        $departmentQ = $departmentId > 0
            ? $conn->query("SELECT code FROM departments WHERE id='".$departmentId."' LIMIT 1")
            : false;
        if($departmentQ && $departmentQ->num_rows > 0 && $role !== 'contractor') {
            $departmentCode = strtoupper($departmentQ->fetch_assoc()['code']);
            $complaintType = ($departmentCode === 'EM') ? 'EM' : 'BR';
        }
        $colonyFilter = "colony_id IN (SELECT colony_id FROM admin_colonies WHERE admin_id='".$adminId."')";
        $typeJoinFilter = $complaintType !== '' ? " AND complaint_list.complaint_type='".$complaintType."'" : '';
        $typePlainFilter = $complaintType !== ''
            ? " AND users_complaint.c_list IN (SELECT cid FROM complaint_list WHERE complaint_type='".$complaintType."')"
            : '';
        $buildingWhere = "building_list.division_id='".$divisionId."' AND building_list.".$colonyFilter.$typeJoinFilter;
        $plainWhere = "users_complaint.b_list IN (SELECT bid FROM building_list WHERE division_id='".$divisionId."' AND ".$colonyFilter.")".$typePlainFilter;

        return array(
            'enabled' => true,
            'building_where' => $buildingWhere,
            'plain_where' => $plainWhere,
            'building_filter' => " AND division_id='".$divisionId."' AND ".$colonyFilter,
            'engineer_filter' => " AND dept_id='".$adminId."'"
        );
    }

    if ($adminType === 'dept' && !empty($admin['subdivision'])) {
        $subdivision = (int)$admin['subdivision'];
        return array(
            'enabled' => true,
            'building_where' => "users_complaint.subdivision='".$subdivision."'",
            'plain_where' => "users_complaint.subdivision='".$subdivision."'",
            'building_filter' => '',
            'engineer_filter' => " AND dept_id='".(int)($_SESSION['admin'] ?? 0)."'"
        );
    }

    return array(
        'enabled' => false,
        'building_where' => '1=1',
        'plain_where' => '1=1',
        'building_filter' => '',
        'engineer_filter' => ''
    );
}
?>
