<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class SBPAI_Module_Coupons extends SBPAI_Module_Base {
    public function generate() {
        if ( ! class_exists( 'WooCommerce' ) ) return false;

        $code = strtoupper( $this->faker->word ) . wp_rand( 100, 999 );
        
        $coupon = array(
            'post_title'   => $code,
            'post_content' => apply_filters( 'sbpai_generated_content', $this->faker->sentence ),
            'post_status'  => 'publish',
            'post_author'  => 1,
            'post_type'    => 'shop_coupon'
        );

        $post_id = wp_insert_post( $coupon );
        if ( is_wp_error( $post_id ) ) return false;

        $this->track_dummy( $post_id, 'post' );

        $type = wp_rand( 1, 2 ) === 1 ? 'percent' : 'fixed_cart';
        update_post_meta( $post_id, 'discount_type', $type );
        update_post_meta( $post_id, 'coupon_amount', wp_rand( 5, 30 ) );
        update_post_meta( $post_id, 'individual_use', 'yes' );
        update_post_meta( $post_id, 'usage_limit', wp_rand( 10, 500 ) );
        update_post_meta( $post_id, 'usage_limit_per_user', wp_rand( 1, 2 ) );
        update_post_meta( $post_id, 'expiry_date', gmdate( 'Y-m-d', strtotime( '+' . wp_rand( 10, 60 ) . ' days' ) ) );
        update_post_meta( $post_id, 'minimum_amount', wp_rand( 20, 50 ) );
        
        return $post_id;
    }
}
