<?php
/**
 * Test script for Collection Agent API
 * This script demonstrates how to use the new collection agent fetch API
 */

// Example usage of the API endpoints

echo "<h2>Collection Agent API Test</h2>";

echo "<h3>1. Standalone API Endpoint (collection_agent_fetch.php)</h3>";
echo "<p><strong>URL:</strong> collection_agent_fetch.php?line=main line</p>";
echo "<p><strong>Method:</strong> GET</p>";
echo "<p><strong>Headers:</strong> Authorization: Bearer [JWT_TOKEN]</p>";
echo "<p><strong>Description:</strong> Fetches collection agents, line entries, and loan entries for a specific line with today's due date.</p>";

echo "<h3>2. Integrated Routes API (collection_agent_auth.php)</h3>";
echo "<p><strong>URL:</strong> collection_agent_auth.php?fetch_agent_data=1&line=main line</p>";
echo "<p><strong>Method:</strong> GET</p>";
echo "<p><strong>Headers:</strong> Authorization: Bearer [JWT_TOKEN]</p>";
echo "<p><strong>Description:</strong> Same functionality but integrated into the existing routes system.</p>";

echo "<h3>3. Example Response Structure</h3>";
echo "<pre>";
echo json_encode([
    'status' => 'success',
    'data' => [
        'agents' => [
            [
                'id' => '1',
                'name' => 'baby',
                'mobile_no' => '7675675652',
                'address' => 'komarapalayam',
                'aadhar_no' => '4850394580594',
                'report_in' => '2025-09-25 15:15:55',
                'reporting_time' => '2025-09-25 15:15:55',
                'status' => 'active',
                'line' => 'main area'
            ]
        ],
        'line_entries' => [
            [
                'id' => '1',
                'com_id' => '9',
                'area' => 'erode',
                'line' => 'main line',
                'created_on' => '0000-00-00 00:00:00'
            ],
            [
                'id' => '2',
                'com_id' => '9',
                'area' => 'salem',
                'line' => 'main line',
                'created_on' => '0000-00-00 00:00:00'
            ]
        ],
        'loan_entries' => [
            [
                'id' => '5',
                'loan_id' => 'DL2025002',
                'com_id' => '9',
                'cus_name' => 'Raja',
                'cust_mobile' => '1234567890',
                'area' => 'salem',
                'loan_type' => 'Daily',
                'loan_amt' => '10000',
                'next_due_date' => '2025-09-23',
                'per_due_amt' => '167'
            ]
        ],
        'query_info' => [
            'company_id' => '9',
            'line' => 'main line',
            'today' => '2025-09-23',
            'areas_found' => ['erode', 'salem'],
            'total_agents' => 1,
            'total_line_entries' => 2,
            'total_loan_entries' => 1
        ]
    ]
], JSON_PRETTY_PRINT);
echo "</pre>";

echo "<h3>4. How to Use</h3>";
echo "<ol>";
echo "<li>Get a valid JWT token by logging in through the authentication system</li>";
echo "<li>Make a GET request to either endpoint with the 'line' parameter</li>";
echo "<li>The API will return:</li>";
echo "<ul>";
echo "<li><strong>agents:</strong> Collection agents assigned to the specified line</li>";
echo "<li><strong>line_entries:</strong> Areas and lines for the company and specified line</li>";
echo "<li><strong>loan_entries:</strong> Loans due today in the areas covered by the line</li>";
echo "<li><strong>query_info:</strong> Summary information about the query results</li>";
echo "</ul>";
echo "</ol>";

echo "<h3>5. Example cURL Commands</h3>";
echo "<h4>Standalone API:</h4>";
echo "<pre>";
echo "curl -X GET 'http://your-domain.com/collection_agent_fetch.php?line=main%20line' \\\n";
echo "  -H 'Authorization: Bearer YOUR_JWT_TOKEN' \\\n";
echo "  -H 'Content-Type: application/json'";
echo "</pre>";

echo "<h4>Integrated Routes API:</h4>";
echo "<pre>";
echo "curl -X GET 'http://your-domain.com/collection_agent_auth.php?fetch_agent_data=1&line=main%20line' \\\n";
echo "  -H 'Authorization: Bearer YOUR_JWT_TOKEN' \\\n";
echo "  -H 'Content-Type: application/json'";
echo "</pre>";

echo "<h3>6. Database Query Logic</h3>";
echo "<p>The API performs the following queries:</p>";
echo "<ol>";
echo "<li><strong>Collection Agents:</strong> <code>SELECT * FROM collection_agent WHERE com_id = ? AND line = ?</code></li>";
echo "<li><strong>Line Entries:</strong> <code>SELECT * FROM line_entry WHERE com_id = ? AND line = ?</code></li>";
echo "<li><strong>Loan Entries:</strong> <code>SELECT * FROM loan_entry WHERE com_id = ? AND area IN (areas) AND next_due_date = today</code></li>";
echo "</ol>";

echo "<h3>7. Error Handling</h3>";
echo "<ul>";
echo "<li><strong>Missing Authorization:</strong> Returns 401 with 'Authorization header missing'</li>";
echo "<li><strong>Invalid Token:</strong> Returns error with JWT decode failure message</li>";
echo "<li><strong>Missing Line Parameter:</strong> Returns 400 with 'Line parameter is required'</li>";
echo "<li><strong>Database Errors:</strong> Returns 500 with database error message</li>";
echo "</ul>";

?>
