<?php

namespace PortalPeople\netsuite\models;

/**
 * NetSuite Sales Order Record
 * 
 * @package PortalPeople\netsuite\models
 */
class SalesOrder extends NetSuiteRecord
{
    /**
     * @inheritdoc
     */
    public static function getRecordType()
    {
        return 'salesorder';
    }
    
    /**
     * Get order entity (customer)
     * 
     * @return array|null
     */
    public function getEntity()
    {
        return $this->getField('entity');
    }
    
    /**
     * Set order entity (customer)
     * 
     * @param int $customerId
     */
    public function setEntity($customerId)
    {
        $this->setField('entity', ['internalId' => $customerId]);
    }
    
    /**
     * Get order date
     * 
     * @return string|null
     */
    public function getTranDate()
    {
        return $this->getField('tranDate');
    }
    
    /**
     * Set order date
     * 
     * @param string $tranDate Date in YYYY-MM-DD format
     */
    public function setTranDate($tranDate)
    {
        $this->setField('tranDate', $tranDate);
    }
    
    /**
     * Get order ID
     * 
     * @return string|null
     */
    public function getTranId()
    {
        return $this->getField('tranId');
    }
    
    /**
     * Set order ID
     * 
     * @param string $tranId
     */
    public function setTranId($tranId)
    {
        $this->setField('tranId', $tranId);
    }
    
    /**
     * Get order memo
     * 
     * @return string|null
     */
    public function getMemo()
    {
        return $this->getField('memo');
    }
    
    /**
     * Set order memo
     * 
     * @param string $memo
     */
    public function setMemo($memo)
    {
        $this->setField('memo', $memo);
    }
    
    /**
     * Get order total
     * 
     * @return float|null
     */
    public function getTotal()
    {
        return $this->getField('total');
    }
    
    /**
     * Get order subtotal
     * 
     * @return float|null
     */
    public function getSubTotal()
    {
        return $this->getField('subTotal');
    }
    
    /**
     * Get order items
     * 
     * @return array|null
     */
    public function getItems()
    {
        return $this->getField('item');
    }
    
    /**
     * Set order items
     * 
     * @param array $items Array of item objects
     */
    public function setItems($items)
    {
        $this->setField('item', $items);
    }
    
    /**
     * Add item to order
     * 
     * @param int $itemId Item internal ID
     * @param int $quantity Quantity
     * @param float|null $rate Rate/price per unit
     */
    public function addItem($itemId, $quantity, $rate = null)
    {
        $items = $this->getItems() ?: [];
        
        $itemData = [
            'item' => ['internalId' => $itemId],
            'quantity' => $quantity
        ];
        
        if ($rate !== null) {
            $itemData['rate'] = $rate;
        }
        
        $items[] = $itemData;
        $this->setItems($items);
    }
    
    /**
     * Get order status
     * 
     * @return string|null
     */
    public function getOrderStatus()
    {
        return $this->getField('orderStatus');
    }
    
    /**
     * Get order subsidiary
     * 
     * @return array|null
     */
    public function getSubsidiary()
    {
        return $this->getField('subsidiary');
    }
    
    /**
     * Set order subsidiary
     * 
     * @param int $subsidiaryId
     */
    public function setSubsidiary($subsidiaryId)
    {
        $this->setField('subsidiary', ['internalId' => $subsidiaryId]);
    }
    
    /**
     * Get shipping address
     * 
     * @return array|null
     */
    public function getShippingAddress()
    {
        return $this->getField('shippingAddress');
    }
    
    /**
     * Set shipping address
     * 
     * @param array $address Address data
     */
    public function setShippingAddress($address)
    {
        $this->setField('shippingAddress', $address);
    }
    
    /**
     * Get billing address
     * 
     * @return array|null
     */
    public function getBillingAddress()
    {
        return $this->getField('billingAddress');
    }
    
    /**
     * Set billing address
     * 
     * @param array $address Address data
     */
    public function setBillingAddress($address)
    {
        $this->setField('billingAddress', $address);
    }
    
    /**
     * Get custom fields
     * 
     * @return array|null
     */
    public function getCustomFields()
    {
        return $this->getField('customFieldList');
    }
    
    /**
     * Set custom fields (header level)
     * 
     * @param array $customFields Array of custom field key-value pairs
     */
    public function setCustomFields($customFields)
    {
        $this->setField('customFieldList', $customFields);
    }
    
    /**
     * Add single custom field (header level)
     * 
     * @param string $fieldId Custom field ID (e.g., 'custbody_exp_ship_date_so')
     * @param mixed $value Field value
     */
    public function addCustomField($fieldId, $value)
    {
        $customFields = $this->getCustomFields() ?: [];
        $customFields[$fieldId] = $value;
        $this->setCustomFields($customFields);
    }
    
    /**
     * Add item to order with custom fields
     * 
     * @param int $itemId Item internal ID
     * @param int $quantity Quantity
     * @param float|null $rate Rate/price per unit
     * @param array $customFields Line-level custom fields
     * @param array $additionalFields Additional line item fields
     */
    public function addItemWithCustomFields($itemId, $quantity, $rate = null, $customFields = [], $additionalFields = [])
    {
        $items = $this->getItems() ?: ['items' => []];
        if (!isset($items['items'])) {
            $items = ['items' => []];
        }
        
        $itemData = array_merge([
            'item' => ['id' => $itemId],
            'quantity' => $quantity
        ], $additionalFields);
        
        if ($rate !== null) {
            $itemData['rate'] = $rate;
        }
        
        if (!empty($customFields)) {
            $itemData['customFieldList'] = $customFields;
        }
        
        $items['items'][] = $itemData;
        $this->setItems($items);
    }
}