<?php
// admin/subscriber_edit_save_lib.php
// Stage 14: shared builder for subscriber edit UPDATE fields.

require_once __DIR__ . '/helpers.php';

if (!function_exists('subscriber_edit_sql_value')) {
  function subscriber_edit_sql_value(mysqli $conn, $value): string {
    if ($value === null) return 'NULL';
    return "'" . $conn->real_escape_string((string)$value) . "'";
  }
}

if (!function_exists('subscriber_edit_add_col_set')) {
  function subscriber_edit_add_col_set(mysqli $conn, array &$sets, string $col, $value, bool $nullable=false): void {
    if (!col_exists($conn, 'subscribers', $col)) return;
    if ($nullable && ($value === null || $value === '')) {
      $sets[] = "`$col`=NULL";
      return;
    }
    $sets[] = "`$col`=" . subscriber_edit_sql_value($conn, $value);
  }
}

if (!function_exists('subscriber_edit_add_int_col_set')) {
  function subscriber_edit_add_int_col_set(mysqli $conn, array &$sets, string $col, $value, bool $nullable=false): void {
    if (!col_exists($conn, 'subscribers', $col)) return;
    if ($nullable && ($value === null || $value === '')) {
      $sets[] = "`$col`=NULL";
      return;
    }
    $sets[] = "`$col`=" . (int)$value;
  }
}

/**
 * Build the exact subscriber UPDATE fields used by subscribers_edit.php.
 * Keeping this in one source prevents future drift between displayed fields and saved fields.
 *
 * @param array $data    Normalized form values.
 * @param array $uploads Optional uploaded paths: front, back, pdf. Null values are ignored.
 * @return string[]      SQL SET fragments.
 */
if (!function_exists('subscriber_edit_build_update_sets')) {
  function subscriber_edit_build_update_sets(mysqli $conn, array $data, array $uploads=[]): array {
    $sets = [];

    // Base columns historically expected by the edit page.
    $sets[] = "`username`=" . subscriber_edit_sql_value($conn, $data['username'] ?? '');
    $sets[] = "`full_name`=" . subscriber_edit_sql_value($conn, $data['full_name'] ?? '');

    subscriber_edit_add_col_set($conn, $sets, 'first_name',     $data['first_name'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'father_name',    $data['father_name'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'last_name',      $data['last_name'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'mother_name',    $data['mother_name'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'birth_date',     $data['birth_date'] ?? '', true);
    subscriber_edit_add_col_set($conn, $sets, 'national_id',    $data['national_id'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'record_number',  $data['record_number'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'document_type',  $data['document_type'] ?? 'هوية شخصية');
    subscriber_edit_add_col_set($conn, $sets, 'nationality',    $data['nationality'] ?? 'سوري');
    subscriber_edit_add_col_set($conn, $sets, 'document_number',$data['document_number'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'notes',          $data['notes'] ?? '');
    subscriber_edit_add_int_col_set($conn, $sets, 'sector_id',  $data['sector_id'] ?? null, true);
    subscriber_edit_add_int_col_set($conn, $sets, 'vlan_id',    $data['vlan_id'] ?? null, true);
    subscriber_edit_add_col_set($conn, $sets, 'phone',          $data['phone'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'ip_address',     $data['ip_address'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'address_area',   $data['address_area'] ?? '');

    // Keep legacy password_plain synced with PPPoE password for older screens.
    subscriber_edit_add_col_set($conn, $sets, 'password_plain', $data['password_plain'] ?? '');

    subscriber_edit_add_col_set($conn, $sets, 'package_name',   $data['package_name'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'speed_name',     $data['speed_name'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'ppp_username',   $data['ppp_username'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'ppp_password',   $data['ppp_password'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'ppp_profile',    $data['ppp_profile'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'pppoe_profile',  $data['ppp_profile'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'mt_profile',     $data['ppp_profile'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'ppp_rate_limit', $data['ppp_rate_limit'] ?? '');
    subscriber_edit_add_col_set($conn, $sets, 'mt_rate_limit',  $data['ppp_rate_limit'] ?? '');

    $frontPath = $uploads['front'] ?? null;
    if ($frontPath !== null && $frontPath !== '') {
      subscriber_edit_add_col_set($conn, $sets, 'id_card_front', $frontPath);
      subscriber_edit_add_col_set($conn, $sets, 'id_front',      $frontPath);
    }

    $backPath = $uploads['back'] ?? null;
    if ($backPath !== null && $backPath !== '') {
      subscriber_edit_add_col_set($conn, $sets, 'id_card_back', $backPath);
      subscriber_edit_add_col_set($conn, $sets, 'id_back',      $backPath);
    }

    $pdfPath = $uploads['pdf'] ?? null;
    if ($pdfPath !== null && $pdfPath !== '') {
      subscriber_edit_add_col_set($conn, $sets, 'id_pdf', $pdfPath);
    }

    return $sets;
  }
}
