<?php
/**
 * Swagger/OpenAPI Documentation Endpoint
 */

header('Content-Type: application/json; charset=utf-8');

$swagger = [
    'openapi' => '3.0.0',
    'info' => [
        'title' => 'Smart Lab LIMS API',
        'version' => '1.0.0',
        'description' => 'API documentation for Smart Lab LIMS system',
        'contact' => [
            'name' => 'API Support',
            'email' => 'support@lablims.com'
        ]
    ],
    'servers' => [
        [
            'url' => 'https://your-domain.com/api',
            'description' => 'Production server'
        ],
        [
            'url' => 'http://localhost/api',
            'description' => 'Development server'
        ]
    ],
    'paths' => [
        '/clinic_results.php' => [
            'get' => [
                'summary' => 'Get results by order number',
                'tags' => ['Results'],
                'parameters' => [
                    [
                        'name' => 'api_key',
                        'in' => 'query',
                        'required' => true,
                        'schema' => ['type' => 'string']
                    ],
                    [
                        'name' => 'order_number',
                        'in' => 'query',
                        'required' => true,
                        'schema' => ['type' => 'string']
                    ]
                ],
                'responses' => [
                    '200' => [
                        'description' => 'Success',
                        'content' => [
                            'application/json' => [
                                'schema' => [
                                    'type' => 'object',
                                    'properties' => [
                                        'success' => ['type' => 'boolean'],
                                        'data' => ['type' => 'object']
                                    ]
                                ]
                            ]
                        ]
                    ],
                    '401' => ['description' => 'Unauthorized'],
                    '404' => ['description' => 'Not found']
                ]
            ],
            'post' => [
                'summary' => 'Get results by patient ID',
                'tags' => ['Results'],
                'requestBody' => [
                    'required' => true,
                    'content' => [
                        'application/json' => [
                            'schema' => [
                                'type' => 'object',
                                'properties' => [
                                    'api_key' => ['type' => 'string'],
                                    'patient_id' => ['type' => 'integer']
                                ]
                            ]
                        ]
                    ]
                ],
                'responses' => [
                    '200' => ['description' => 'Success'],
                    '401' => ['description' => 'Unauthorized']
                ]
            ]
        ],
        '/doctor_notifications.php' => [
            'get' => [
                'summary' => 'Get doctor notifications',
                'tags' => ['Notifications'],
                'security' => [['sessionAuth' => []]],
                'responses' => [
                    '200' => ['description' => 'Success'],
                    '401' => ['description' => 'Unauthorized']
                ]
            ]
        ],
        '/global_search.php' => [
            'get' => [
                'summary' => 'Global search',
                'tags' => ['Search'],
                'parameters' => [
                    [
                        'name' => 'q',
                        'in' => 'query',
                        'required' => true,
                        'schema' => ['type' => 'string']
                    ]
                ],
                'responses' => [
                    '200' => ['description' => 'Success']
                ]
            ]
        ]
    ],
    'components' => [
        'securitySchemes' => [
            'sessionAuth' => [
                'type' => 'apiKey',
                'in' => 'cookie',
                'name' => 'PHPSESSID'
            ],
            'apiKeyAuth' => [
                'type' => 'apiKey',
                'in' => 'query',
                'name' => 'api_key'
            ]
        ],
        'schemas' => [
            'Error' => [
                'type' => 'object',
                'properties' => [
                    'success' => ['type' => 'boolean'],
                    'error' => ['type' => 'string'],
                    'code' => ['type' => 'string']
                ]
            ],
            'Result' => [
                'type' => 'object',
                'properties' => [
                    'test_name' => ['type' => 'string'],
                    'result_value' => ['type' => 'string'],
                    'unit' => ['type' => 'string'],
                    'reference_range' => ['type' => 'string'],
                    'flag' => ['type' => 'string', 'enum' => ['N', 'L', 'H', 'C']]
                ]
            ]
        ]
    ],
    'tags' => [
        ['name' => 'Results', 'description' => 'Laboratory results'],
        ['name' => 'Notifications', 'description' => 'Doctor notifications'],
        ['name' => 'Search', 'description' => 'Global search']
    ]
];

echo json_encode($swagger, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
