/*
  Reforca a disciplina de prazo no score operacional das tarefas.
  Regras entregues:
  - bonus para tarefa concluida no prazo
  - penalidade para tarefa concluida em atraso
*/

UPDATE wpjy_perf_rules
SET
  nome = 'Tarefa concluida no prazo',
  evento = 'task_status_changed',
  pontos = 6,
  limite_dia = 40,
  ativo = 1,
  contexto_json = JSON_OBJECT(
    'all', JSON_ARRAY(
      JSON_OBJECT('field', 'status', 'op', 'eq', 'value', 'concluida'),
      JSON_OBJECT('field', 'on_time', 'op', 'eq', 'value', 1)
    ),
    'unique_by', JSON_ARRAY('source_key')
  )
WHERE slug = 'task-complete-on-time';

INSERT INTO wpjy_perf_rules (slug, nome, evento, pontos, limite_dia, ativo, contexto_json)
SELECT
  'task-complete-on-time',
  'Tarefa concluida no prazo',
  'task_status_changed',
  6,
  40,
  1,
  JSON_OBJECT(
    'all', JSON_ARRAY(
      JSON_OBJECT('field', 'status', 'op', 'eq', 'value', 'concluida'),
      JSON_OBJECT('field', 'on_time', 'op', 'eq', 'value', 1)
    ),
    'unique_by', JSON_ARRAY('source_key')
  )
WHERE NOT EXISTS (
  SELECT 1
  FROM wpjy_perf_rules
  WHERE slug = 'task-complete-on-time'
);

UPDATE wpjy_perf_rules
SET
  nome = 'Tarefa concluida em atraso',
  evento = 'task_status_changed',
  pontos = -6,
  limite_dia = 40,
  ativo = 1,
  contexto_json = JSON_OBJECT(
    'all', JSON_ARRAY(
      JSON_OBJECT('field', 'status', 'op', 'eq', 'value', 'concluida'),
      JSON_OBJECT('field', 'on_time', 'op', 'eq', 'value', 0)
    ),
    'unique_by', JSON_ARRAY('source_key')
  )
WHERE slug = 'task-complete-late';

INSERT INTO wpjy_perf_rules (slug, nome, evento, pontos, limite_dia, ativo, contexto_json)
SELECT
  'task-complete-late',
  'Tarefa concluida em atraso',
  'task_status_changed',
  -6,
  40,
  1,
  JSON_OBJECT(
    'all', JSON_ARRAY(
      JSON_OBJECT('field', 'status', 'op', 'eq', 'value', 'concluida'),
      JSON_OBJECT('field', 'on_time', 'op', 'eq', 'value', 0)
    ),
    'unique_by', JSON_ARRAY('source_key')
  )
WHERE NOT EXISTS (
  SELECT 1
  FROM wpjy_perf_rules
  WHERE slug = 'task-complete-late'
);
