<?php
require_once __DIR__ . '/db_config.php';

function get_districts() {
    global $conn;
    
    $districts = [];
    
    // Fetch all districts and their DS divisions
    $sql = "SELECT d.name as district, ds.name as division 
            FROM districts d 
            JOIN divisional_secretariats ds ON d.id = ds.district_id 
            ORDER BY d.name, ds.name";
            
    $result = $conn->query($sql);
    
    if ($result && $result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $district = $row['district'];
            $division = $row['division'];
            
            if (!isset($districts[$district])) {
                $districts[$district] = [];
            }
            
            $districts[$district][] = $division;
        }
    }
    
    return $districts;
}
?>
