<?php

/**
 * Example: Basic NetSuite Operations with OAuth 2.0 JWT
 * 
 * This example demonstrates basic CRUD operations with NetSuite using the Yii2 extension
 * with OAuth 2.0 JWT authentication
 */

use PortalPeople\netsuite\models\Customer;
use PortalPeople\netsuite\models\Item;
use PortalPeople\netsuite\models\SalesOrder;

// Make sure you have configured the NetSuite component in your application config with OAuth 2.0 JWT settings

// Example 1: Create a new customer
function createCustomerExample()
{
    $customer = new Customer();
    $customer->setEntityId('CUST' . date('YmdHis'));
    $customer->setCompanyName('Example Tech Company');
    $customer->setEmail('contact@exampletech.com');
    $customer->setPhone('555-123-4567');
    
    try {
        $response = Yii::$app->netsuite->createRecord(
            Customer::getRecordType(),
            $customer->toArray()
        );
        
        echo "Customer created successfully!\n";
        echo "Internal ID: " . $response['internalId'] . "\n";
        return $response['internalId'];
        
    } catch (Exception $e) {
        echo "Error creating customer: " . $e->getMessage() . "\n";
        return false;
    }
}

// Example 2: Retrieve a customer
function getCustomerExample($customerId)
{
    try {
        $customer = Yii::$app->netsuite->getRecord('customer', $customerId);
        
        echo "Customer retrieved:\n";
        echo "Entity ID: " . $customer['entityId'] . "\n";
        echo "Company: " . $customer['companyName'] . "\n";
        echo "Email: " . $customer['email'] . "\n";
        
        return $customer;
        
    } catch (Exception $e) {
        echo "Error retrieving customer: " . $e->getMessage() . "\n";
        return false;
    }
}

// Example 3: Update a customer
function updateCustomerExample($customerId)
{
    $updateData = [
        'companyName' => 'Updated Tech Company Name',
        'phone' => '555-987-6543'
    ];
    
    try {
        $response = Yii::$app->netsuite->updateRecord('customer', $customerId, $updateData);
        
        echo "Customer updated successfully!\n";
        return true;
        
    } catch (Exception $e) {
        echo "Error updating customer: " . $e->getMessage() . "\n";
        return false;
    }
}

// Example 4: Create an inventory item
function createItemExample()
{
    $item = new Item();
    $item->setItemId('PROD' . date('YmdHis'));
    $item->setDisplayName('Example Product');
    $item->setDescription('This is an example product created via API');
    $item->setBasePrice(99.99);
    $item->setCostEstimate(50.00);
    
    try {
        $response = Yii::$app->netsuite->createRecord(
            Item::getRecordType(),
            $item->toArray()
        );
        
        echo "Item created successfully!\n";
        echo "Internal ID: " . $response['internalId'] . "\n";
        return $response['internalId'];
        
    } catch (Exception $e) {
        echo "Error creating item: " . $e->getMessage() . "\n";
        return false;
    }
}

// Example 5: Create a sales order
function createSalesOrderExample($customerId, $itemId)
{
    $salesOrder = new SalesOrder();
    $salesOrder->setEntity($customerId);
    $salesOrder->setTranDate(date('Y-m-d'));
    $salesOrder->setMemo('Sales order created via API');
    
    // Add items to the order
    $salesOrder->addItem($itemId, 2, 99.99);
    
    try {
        $response = Yii::$app->netsuite->createRecord(
            SalesOrder::getRecordType(),
            $salesOrder->toArray()
        );
        
        echo "Sales order created successfully!\n";
        echo "Internal ID: " . $response['internalId'] . "\n";
        return $response['internalId'];
        
    } catch (Exception $e) {
        echo "Error creating sales order: " . $e->getMessage() . "\n";
        return false;
    }
}

// Run examples
echo "=== NetSuite Basic Operations Example (OAuth 2.0 JWT) ===\n\n";

// Test connection first
if (Yii::$app->netsuite->testConnection()) {
    echo "✓ NetSuite connection successful!\n\n";
    
    // Create customer
    echo "1. Creating customer...\n";
    $customerId = createCustomerExample();
    echo "\n";
    
    if ($customerId) {
        // Retrieve customer
        echo "2. Retrieving customer...\n";
        getCustomerExample($customerId);
        echo "\n";
        
        // Update customer
        echo "3. Updating customer...\n";
        updateCustomerExample($customerId);
        echo "\n";
        
        // Create item
        echo "4. Creating item...\n";
        $itemId = createItemExample();
        echo "\n";
        
        if ($itemId) {
            // Create sales order
            echo "5. Creating sales order...\n";
            createSalesOrderExample($customerId, $itemId);
            echo "\n";
        }
    }
    
} else {
    echo "✗ NetSuite connection failed! Please check your configuration.\n";
}