USE bulo;

CREATE TABLE IF NOT EXISTS motorista_sessoes_online (
  id INT AUTO_INCREMENT PRIMARY KEY,
  id_motorista INT NOT NULL,
  inicio_em DATETIME NOT NULL,
  fim_em DATETIME NULL,
  duracao_segundos INT NOT NULL DEFAULT 0,
  criado_em TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_sessoes_online_motorista FOREIGN KEY (id_motorista) REFERENCES usuarios(id),
  INDEX idx_sessoes_online_motorista_inicio (id_motorista, inicio_em),
  INDEX idx_sessoes_online_fim (fim_em)
);

INSERT INTO ganhos_diarios (id_motorista, data_ref, total_corridas, total_valor) VALUES
  (4, '2026-06-01', 3, 74.80),
  (4, '2026-06-03', 5, 132.45),
  (4, '2026-06-04', 4, 96.20),
  (4, '2026-06-06', 6, 158.90),
  (4, '2026-06-08', 2, 51.70),
  (4, '2026-06-10', 7, 189.35),
  (4, '2026-06-12', 5, 121.60),
  (4, '2026-06-14', 8, 216.40),
  (4, '2026-06-15', 4, 103.25),
  (4, '2026-06-16', 6, 147.90),
  (4, '2026-06-17', 3, 82.10),
  (4, '2026-06-19', 9, 238.75),
  (4, '2026-06-20', 5, 135.30),
  (4, '2026-06-22', 4, 109.50)
ON DUPLICATE KEY UPDATE
  total_corridas = VALUES(total_corridas),
  total_valor = VALUES(total_valor),
  atualizado_em = CURRENT_TIMESTAMP;

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-01 08:10:00', '2026-06-01 12:20:00', TIMESTAMPDIFF(SECOND, '2026-06-01 08:10:00', '2026-06-01 12:20:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-01 08:10:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-03 13:30:00', '2026-06-03 19:15:00', TIMESTAMPDIFF(SECOND, '2026-06-03 13:30:00', '2026-06-03 19:15:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-03 13:30:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-04 07:45:00', '2026-06-04 12:40:00', TIMESTAMPDIFF(SECOND, '2026-06-04 07:45:00', '2026-06-04 12:40:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-04 07:45:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-06 10:00:00', '2026-06-06 18:35:00', TIMESTAMPDIFF(SECOND, '2026-06-06 10:00:00', '2026-06-06 18:35:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-06 10:00:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-08 09:20:00', '2026-06-08 12:05:00', TIMESTAMPDIFF(SECOND, '2026-06-08 09:20:00', '2026-06-08 12:05:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-08 09:20:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-10 07:10:00', '2026-06-10 15:55:00', TIMESTAMPDIFF(SECOND, '2026-06-10 07:10:00', '2026-06-10 15:55:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-10 07:10:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-12 14:00:00', '2026-06-12 20:10:00', TIMESTAMPDIFF(SECOND, '2026-06-12 14:00:00', '2026-06-12 20:10:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-12 14:00:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-14 08:30:00', '2026-06-14 18:45:00', TIMESTAMPDIFF(SECOND, '2026-06-14 08:30:00', '2026-06-14 18:45:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-14 08:30:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-15 08:00:00', '2026-06-15 13:35:00', TIMESTAMPDIFF(SECOND, '2026-06-15 08:00:00', '2026-06-15 13:35:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-15 08:00:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-16 12:15:00', '2026-06-16 19:40:00', TIMESTAMPDIFF(SECOND, '2026-06-16 12:15:00', '2026-06-16 19:40:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-16 12:15:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-17 07:50:00', '2026-06-17 11:25:00', TIMESTAMPDIFF(SECOND, '2026-06-17 07:50:00', '2026-06-17 11:25:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-17 07:50:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-19 09:10:00', '2026-06-19 21:05:00', TIMESTAMPDIFF(SECOND, '2026-06-19 09:10:00', '2026-06-19 21:05:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-19 09:10:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-20 10:40:00', '2026-06-20 16:55:00', TIMESTAMPDIFF(SECOND, '2026-06-20 10:40:00', '2026-06-20 16:55:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-20 10:40:00');

INSERT INTO motorista_sessoes_online (id_motorista, inicio_em, fim_em, duracao_segundos)
SELECT 4, '2026-06-22 08:25:00', '2026-06-22 13:15:00', TIMESTAMPDIFF(SECOND, '2026-06-22 08:25:00', '2026-06-22 13:15:00')
WHERE NOT EXISTS (SELECT 1 FROM motorista_sessoes_online WHERE id_motorista = 4 AND inicio_em = '2026-06-22 08:25:00');
