<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class SBPAI_Module_Users extends SBPAI_Module_Base {
    public function generate() {
        if ( ! current_user_can( 'manage_options' ) ) return false;
        $role     = isset( $this->args['role'] )     ? sanitize_key( $this->args['role'] )     : 'subscriber';
        $password = isset( $this->args['password'] ) ? $this->args['password']                 : wp_generate_password();
        $avatar   = isset( $this->args['avatar'] )   ? intval( $this->args['avatar'] )         : 1;
        $billing  = isset( $this->args['billing'] )  ? intval( $this->args['billing'] )        : 1;
        $bio      = isset( $this->args['bio'] )      ? intval( $this->args['bio'] )            : 1;

        if ( empty($password) ) {
            $password = wp_generate_password();
        }

        $username = strtolower( $this->faker->userName ) . wp_rand( 100, 999 );

        // Use yopmail.com so all dummy emails are real inboxes and easy to identify
        $email = $username . '@yopmail.com';

        // Ensure unique — append extra random suffix if taken
        while ( email_exists( $email ) ) {
            $email = $username . wp_rand( 10, 999 ) . '@yopmail.com';
        }

        $user_id = wp_create_user( $username, $password, $email );

        if ( is_wp_error( $user_id ) ) {
            return false;
        }

        $this->track_dummy( $user_id, 'user' );

        $first_name = $this->faker->firstName;
        $last_name  = $this->faker->lastName;

        update_user_meta( $user_id, 'first_name', $first_name );
        update_user_meta( $user_id, 'last_name',  $last_name );

        // Store the known password so admins can look it up
        update_user_meta( $user_id, '_sbpai_default_password', $password );

        if ( $bio ) {
            update_user_meta( $user_id, 'description', $this->faker->text(200) );
            update_user_meta( $user_id, 'user_url',    $this->faker->url );
        }

        if ( $avatar ) {
            $avatar_id = SBPAI_Service_Media::fetch_image( 150, 150, 'face' );
            if ( $avatar_id ) {
                update_user_meta( $user_id, '_sbpai_avatar_id', $avatar_id );
            }
        }

        if ( $billing ) {
            $address_data = [
                'billing_first_name' => $first_name,
                'billing_last_name'  => $last_name,
                'billing_company'    => $this->faker->company,
                'billing_address_1'  => $this->faker->streetAddress,
                'billing_city'       => $this->faker->city,
                'billing_postcode'   => $this->faker->postcode,
                'billing_country'    => $this->faker->countryCode,
                'billing_state'      => $this->faker->stateAbbr,
                'billing_phone'      => $this->faker->phoneNumber,
                'billing_email'      => $email,
                'shipping_first_name'=> $first_name,
                'shipping_last_name' => $last_name,
                'shipping_company'   => $this->faker->company,
                'shipping_address_1' => $this->faker->streetAddress,
                'shipping_city'      => $this->faker->city,
                'shipping_postcode'  => $this->faker->postcode,
                'shipping_country'   => $this->faker->countryCode,
                'shipping_state'     => $this->faker->stateAbbr,
            ];
            foreach ( $address_data as $key => $val ) {
                update_user_meta( $user_id, $key, $val );
            }
        }

        // Assign the configured role
        $u = new WP_User( $user_id );
        if ( ! empty( $role ) && get_role( $role ) ) {
            $u->set_role( $role );
        }

        return $user_id;
    }
}
