-- ========================================
-- Quick Verification Queries
-- ========================================
-- Copy and paste these queries into MySQL
-- ========================================

-- 1. Latest OT (simple view)
SELECT id_ot, fecha, rut_cliente, rut_tecnico, empresa, estado_ot, motivo 
FROM OT 
ORDER BY id_ot DESC 
LIMIT 1;

-- 2. Latest OT (all fields - vertical format)
-- Run with: mysql -u root -p revisa -e "SELECT * FROM OT ORDER BY id_ot DESC LIMIT 1\G"

-- 3. Specific OT by ID
-- SELECT * FROM OT WHERE id_ot = 1655\G

-- 4. Check client exists
SELECT rut_cliente, nombre, direccion, comuna, telefono, email 
FROM Clientes 
WHERE rut_cliente = '12345678-5';

-- 5. Check vehicles in latest OT
SELECT ov.id_ot, ov.patente, v.marca, v.modelo, v.anio, v.color 
FROM OT_Vehiculos ov 
JOIN Vehiculos v ON ov.patente = v.patente 
WHERE ov.id_ot = (SELECT MAX(id_ot) FROM OT);

-- 6. Check specific vehicle
SELECT patente, marca, modelo, anio, color, vin, creado_en, actualizado_en 
FROM Vehiculos 
WHERE patente = 'TEST99';

-- 7. Count OTs created today
SELECT COUNT(*) as ots_today 
FROM OT 
WHERE DATE(creado_en) = CURDATE();

-- 8. List all OTs created today
SELECT id_ot, rut_cliente, rut_tecnico, empresa, estado_ot, creado_en 
FROM OT 
WHERE DATE(creado_en) = CURDATE()
ORDER BY id_ot DESC;

-- 9. Check technician exists
SELECT rut_tecnico, nombre, idgpsimple 
FROM Tecnicos 
WHERE rut_tecnico = '11111111-1';

-- 10. Check if stored procedure exists
SHOW PROCEDURE STATUS WHERE Name = 'prc_api_create_OT';

-- 11. Full OT with all relationships
SELECT 
    o.id_ot,
    o.fecha,
    o.empresa,
    o.estado_ot,
    c.nombre as cliente_nombre,
    t.nombre as tecnico_nombre,
    COUNT(ov.patente) as num_vehiculos
FROM OT o
LEFT JOIN Clientes c ON o.rut_cliente = c.rut_cliente
LEFT JOIN Tecnicos t ON o.rut_tecnico = t.rut_tecnico
LEFT JOIN OT_Vehiculos ov ON o.id_ot = ov.id_ot
WHERE o.id_ot = (SELECT MAX(id_ot) FROM OT)
GROUP BY o.id_ot;

-- 12. Check for NULL fields in latest OT
SELECT 
    id_ot,
    CASE WHEN fecha IS NULL THEN 'NULL' ELSE 'OK' END as fecha,
    CASE WHEN fecha_serv IS NULL THEN 'NULL' ELSE 'OK' END as fecha_serv,
    CASE WHEN empresa IS NULL THEN 'NULL' ELSE 'OK' END as empresa,
    CASE WHEN taller IS NULL THEN 'NULL' ELSE 'OK' END as taller,
    CASE WHEN motivo IS NULL THEN 'NULL' ELSE 'OK' END as motivo,
    CASE WHEN logo_taller IS NULL THEN 'NULL' ELSE 'OK' END as logo_taller,
    CASE WHEN logo_servicio IS NULL THEN 'NULL' ELSE 'OK' END as logo_servicio
FROM OT 
ORDER BY id_ot DESC 
LIMIT 1;

