<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/helpers/csrf.php';
require_once __DIR__ . '/../../app/middleware/auth.php';

session_boot();
require_login();

$conn = require __DIR__ . '/../../app/config/database.php';
require_admin($conn);

if (is_post()) {
  csrf_verify();

  $userId = (int)$_POST['user_id'];
  $currency = $_POST['currency'] ?? 'GHS';

  $accountNo = 'SV' . random_int(10000000, 99999999);

  $st = $conn->prepare("
    INSERT INTO accounts
      (user_id, account_no, account_type, currency, balance, is_primary)
    VALUES (?,?,?,?,0,0)
  ");
  $st->bind_param("isss", $userId, $accountNo, $currency, $type);
  $type = 'savings';
  $st->execute();

  flash('msg','Savings account created.');
  redirect('customer_view.php?id='.$userId);
}
