<?php
/**
 * Template Name: Signup Page
 * Description: A secure user signup form with welcome email.
 */

if (!defined('ABSPATH')) exit;

get_header();
?>

<div class="container my-5">
  <div class="row justify-content-center">
    <div class="col-md-6">

      <?php
      // Handle Registration
      if (isset($_POST['howcan_register_nonce']) && wp_verify_nonce($_POST['howcan_register_nonce'], 'howcan_register_action')) {
          $username = sanitize_user($_POST['username']);
          $email    = sanitize_email($_POST['email']);
          $password = sanitize_text_field($_POST['password']);

          $errors = new WP_Error();

          if (empty($username) || empty($email) || empty($password)) {
              $errors->add('empty_fields', __('Please fill in all fields.', 'howcan'));
          }

          if (username_exists($username)) {
              $errors->add('username_exists', __('This username is already taken.', 'howcan'));
          }

          if (email_exists($email)) {
              $errors->add('email_exists', __('This email is already registered.', 'howcan'));
          }

          if (!is_email($email)) {
              $errors->add('invalid_email', __('Please enter a valid email address.', 'howcan'));
          }

          if (empty($errors->get_error_codes())) {
              $user_id = wp_create_user($username, $password, $email);
              if (!is_wp_error($user_id)) {

                  // Send Welcome Email
                  $subject = sprintf(__('Welcome to %s!', 'howcan'), get_bloginfo('name'));
                  $message  = sprintf(__('Hi %s,', 'howcan'), $username) . "\r\n\r\n";
                  $message .= __('Thanks for signing up! You can now log in and start using our site.', 'howcan') . "\r\n\r\n";
                  $message .= home_url() . "\r\n\r\n";
                  $message .= __('– The Team', 'howcan');
                  wp_mail($email, $subject, $message);

                  echo '<div class="alert alert-success">' . esc_html__('Registration successful! Please your Acoount.', 'howcan') . '</div>';
              } else {
                  echo '<div class="alert alert-danger">' . esc_html($user_id->get_error_message()) . '</div>';
              }
          } else {
              foreach ($errors->get_error_messages() as $error) {
                  echo '<div class="alert alert-danger">' . esc_html($error) . '</div>';
              }
          }
      }
      ?>

      <div class="card shadow p-4">
        <h3 class="mb-4 text-center"><?php esc_html_e('Create an Account', 'howcan'); ?></h3>
        <form method="POST" action="">
          <div class="mb-3">
            <label class="form-label"><?php esc_html_e('Username', 'howcan'); ?></label>
            <input type="text" name="username" class="form-control" required>
          </div>

          <div class="mb-3">
            <label class="form-label"><?php esc_html_e('Email Address', 'howcan'); ?></label>
            <input type="email" name="email" class="form-control" required>
          </div>

          <div class="mb-3">
            <label class="form-label"><?php esc_html_e('Password', 'howcan'); ?></label>
            <input type="password" name="password" class="form-control" required>
          </div>

          <?php wp_nonce_field('howcan_register_action', 'howcan_register_nonce'); ?>

          <button type="submit" class="btn btn-dark w-100">
            <?php esc_html_e('Sign Up', 'howcan'); ?>
          </button>
        </form>
      </div>

    </div>
  </div>
</div>

<?php
get_footer();