<?php
/**
 * Seed default WooCommerce product on WordPress initialization
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function repetito_seed_default_product() {
    // Only seed if REPETITO_SEED is enabled (via environment variable or WordPress constant)
    $should_seed = defined('REPETITO_SEED') && REPETITO_SEED === true;
    if (!$should_seed) {
        return;
    }

    // Check if WooCommerce is active
    if (!class_exists('WooCommerce')) {
        return;
    }

    // Check if any product already exists
    $query = new WC_Product_Query( array(
        'limit' => 1,
        'return' => 'ids',
    ) );
    $existing_products = $query->get_products();

    if (!empty($existing_products)) {
        return;
    }

    // Create new product
    $product = new WC_Product_Simple();
    $product->set_name('Repetito test product');
    $product->set_description('This is a sample test product created by the initialization script of Repetito plugin.');
    $product->set_short_description('Sample Repetito test product');
    $product->set_regular_price('10.00');
    // $product->set_manage_stock(true);
    // $product->set_stock_quantity(10);
    $product->set_stock_status('instock');
    // $product->set_weight(0.1); // 100g in kg
    // $product->set_length(3); // 3cm in meters
    // $product->set_width(3); // 3cm in meters
    // $product->set_height(3); // 3cm in meters
    $product->set_status('publish');
    $product->set_catalog_visibility('visible');
    $product->set_sku('REPETITO-TEST-001');

    // Save the product
    $product->save();
}

// Hook into WordPress initialization
add_action('init', 'repetito_seed_default_product');