#!/usr/bin/env python3
import requests
from bs4 import BeautifulSoup
import re

BASE = 'https://sistema.skeenmx.app'
USERNAME = 'JoseSkeen'
PASSWORD = 'SKr#902_88'

session = requests.Session()
r = session.get(f'{BASE}/login', timeout=30)
soup = BeautifulSoup(r.text, 'html.parser')
token = soup.find('input', {'name': '_token'})
token = token['value'] if token else ''

session.post(f'{BASE}/login', data={
    '_token': token,
    'username': USERNAME,
    'password': PASSWORD,
}, timeout=30, allow_redirects=True)

# Probar varios IDs directamente
for pid in [1, 2, 3, 5, 10, 20, 50, 100, 200, 500, 1000]:
    try:
        r = session.get(f'{BASE}/expediente/ver/{pid}', timeout=30)
        soup = BeautifulSoup(r.text, 'html.parser')
        text = soup.get_text(separator='\n')
        if any(k in text.lower() for k in ['riñón', 'rinon', 'embarazada', 'alergias', 'medicamentos', 'cirugías']):
            print(f'\n=== PACIENTE {pid} tiene historia clínica ===')
            lines = [l.strip() for l in text.split('\n') if l.strip()]
            for i, line in enumerate(lines):
                if any(k in line.lower() for k in ['riñón', 'rinon', 'embarazada', 'alergias', 'medicamentos', 'cirugías', 'antecedentes', 'patológicos', 'historia']):
                    print(f'  {i}: {line}')
                    for j in range(max(0, i-3), min(len(lines), i+6)):
                        print(f'    {j}: {lines[j]}')
                    print('---')
    except Exception as e:
        print(f'Error {pid}: {e}')
