<?php
/**
 * eTIMS Mock Integration for Development
 * Use this when the eTIMS sandbox is unavailable
 * File: includes/EtimsMock.php
 */

class EtimsMock {
    
    /**
     * Check if mock mode is enabled
     */
    public static function isMockMode($conn) {
        $sql = "SELECT setting_value FROM system_settings WHERE setting_key = 'etims_mock_mode'";
        $result = $conn->query($sql);
        if ($result && $row = $result->fetch_assoc()) {
            return $row['setting_value'] === '1';
        }
        return false;
    }
    
    /**
     * Mock Initialize Device
     */
    public static function mockInitializeDevice() {
        return [
            'success' => true,
            'message' => 'Device initialized successfully (MOCK MODE)',
            'result_code' => '000',
            'data' => [
                'tin' => 'P052077586R',
                'bhfId' => '00',
                'bhfNm' => 'Main Branch',
                'prvncNm' => 'Nairobi',
                'dstrtNm' => 'Westlands',
                'sctrNm' => 'Healthcare',
                'lastSaleInvcNo' => 1000,
                'lastPurchInvcNo' => 500,
                'lastStockMoveNo' => 100
            ]
        ];
    }
    
    /**
     * Mock Save Item
     */
    public static function mockSaveItem($item_data) {
        return [
            'success' => true,
            'message' => 'Item registered successfully (MOCK MODE)',
            'result_code' => '000',
            'data' => [
                'itemCd' => $item_data['item_code'],
                'itemNm' => $item_data['item_name'],
                'resultDt' => date('YmdHis')
            ]
        ];
    }
    
    /**
     * Mock Submit Invoice
     */
    public static function mockSubmitInvoice($invoice_data) {
        // Generate mock receipt signature
        $receipt_signature = strtoupper(bin2hex(random_bytes(32)));
        
        // Generate mock internal data
        $internal_data = date('YmdHis') . rand(1000, 9999);
        
        // Generate mock QR code data
        $qr_data = base64_encode(json_encode([
            'tin' => 'P052077586R',
            'bhfId' => '00',
            'invcNo' => $invoice_data['invoice_number'],
            'totAmt' => $invoice_data['total_amount'],
            'rcptSign' => $receipt_signature
        ]));
        
        return [
            'success' => true,
            'message' => 'Invoice submitted successfully (MOCK MODE)',
            'result_code' => '000',
            'data' => [
                'invcNo' => $invoice_data['invoice_number'],
                'intrlData' => $internal_data,
                'rcptSign' => $receipt_signature,
                'totRcptNo' => rand(100000, 999999),
                'rcptDt' => date('YmdHis'),
                'qrCodeUrl' => 'data:image/png;base64,' . $qr_data
            ]
        ];
    }
    
    /**
     * Mock Get Customer
     */
    public static function mockGetCustomer($customer_tin) {
        return [
            'success' => true,
            'message' => 'Customer found (MOCK MODE)',
            'result_code' => '000',
            'data' => [
                'custTin' => $customer_tin,
                'custNm' => 'Mock Customer',
                'adrs' => 'Nairobi, Kenya',
                'email' => 'customer@example.com',
                'telNo' => '+254700000000'
            ]
        ];
    }
    
    /**
     * Mock Save Customer
     */
    public static function mockSaveCustomer($customer_data) {
        return [
            'success' => true,
            'message' => 'Customer saved successfully (MOCK MODE)',
            'result_code' => '000',
            'data' => [
                'custNo' => 'CUST-' . rand(1000, 9999),
                'custTin' => $customer_data['customer_tin'],
                'custNm' => $customer_data['customer_name']
            ]
        ];
    }
    
    /**
     * Mock Get Item Classes
     */
    public static function mockGetItemClasses() {
        return [
            'success' => true,
            'message' => 'Item classes retrieved (MOCK MODE)',
            'result_code' => '000',
            'data' => [
                [
                    'itemClsCd' => '50101502',
                    'itemClsNm' => 'Ear nose and throat medical services',
                    'itemClsLvl' => 4,
                    'useYn' => 'Y'
                ],
                [
                    'itemClsCd' => '51101500',
                    'itemClsNm' => 'Medical laboratory services',
                    'itemClsLvl' => 4,
                    'useYn' => 'Y'
                ]
            ]
        ];
    }
    
    /**
     * Mock Get Notices
     */
    public static function mockGetNotices() {
        return [
            'success' => true,
            'message' => 'Notices retrieved (MOCK MODE)',
            'result_code' => '000',
            'data' => [
                [
                    'noticeNo' => 'NOT-' . date('Ymd') . '-001',
                    'title' => 'System Maintenance Notice (MOCK)',
                    'cont' => 'This is a mock notice for testing purposes.',
                    'dtlUrl' => 'https://etims.kra.go.ke/notices/001',
                    'regrDt' => date('YmdHis')
                ]
            ]
        ];
    }
}
?>