START TRANSACTION;

DELETE FROM `wpjy_dashboards`
WHERE `id_contrato` = 57
  AND `slug` IN ('financeiro', 'comercial', 'diretoria', 'operacao');

INSERT INTO `wpjy_dashboards`
(`id_contrato`,`nome`,`slug`,`descricao`,`tipo`,`status`,`route_path`,`icone`,`cor`,`is_system`,`created_by`,`updated_by`)
VALUES
(57,'Dashboard Financeiro','financeiro','Receita, cobranca, assinaturas e aging financeiro em uma unica leitura.','financeiro','published','dashboard-financeiro','ti ti-cash-banknote','green',1,NULL,NULL),
(57,'Dashboard Comercial','comercial','Leads, conversao, funil e operacao comercial em leitura rapida.','comercial','published','dashboard-comercial','ti ti-chart-funnel','teal',1,NULL,NULL),
(57,'Dashboard Diretoria','diretoria','Leitura executiva de caixa, operacao, tickets, tarefas e documentos.','executivo','published','dashboard-diretoria','ti ti-briefcase','violet',1,NULL,NULL),
(57,'Dashboard Operacao','operacao','Tickets, execucao, backlog e documentos para acompanhamento operacional.','operacional','published','dashboard-operacao','ti ti-activity-heartbeat','warning',1,NULL,NULL);

INSERT INTO `wpjy_dashboard_versions`
(`id_dashboard`,`version_no`,`status`,`titulo_override`,`layout_json`,`filters_json`,`theme_json`,`notes`,`created_by`,`published_by`,`published_at`)
SELECT d.id, 1, 'published', 'Dashboard Financeiro',
'{
  "title":"Dashboard Financeiro",
  "filters":[
    {"key":"from","type":"date_range","default":"30d"},
    {"key":"id_contrato","type":"contract_select"}
  ],
  "widgets":[
    {"id":"fin_receita","type":"kpi","title":"Receita paga","data_source":"finance.summary","layout":{"x":0,"y":0,"w":3,"h":2},"config":{"width":3,"icon":"ti ti-cash-banknote","color":"success"}},
    {"id":"fin_recebiveis","type":"bar_chart","title":"Aging de recebiveis","data_source":"finance.receivables.aging","layout":{"x":3,"y":0,"w":4,"h":4},"config":{"width":4,"icon":"ti ti-report-money","color":"warning","limit":6}},
    {"id":"fin_revenue","type":"line_chart","title":"Receita no periodo","data_source":"finance.revenue.timeline","layout":{"x":7,"y":0,"w":5,"h":4},"config":{"width":5,"icon":"ti ti-chart-line","color":"success","group_by":"day","limit":12}},
    {"id":"fin_subs","type":"table","title":"Assinaturas","data_source":"finance.subscriptions.snapshot","layout":{"x":0,"y":1,"w":12,"h":4},"config":{"width":12,"icon":"ti ti-id-badge-2","color":"primary","limit":8}}
  ]
}',
'{"from":"-29 days","to":"today"}',
'{"hero":"finance"}',
'Seed inicial TIIX',
NULL,NULL,NOW()
FROM `wpjy_dashboards` d
WHERE d.`id_contrato` = 57 AND d.`slug` = 'financeiro';

INSERT INTO `wpjy_dashboard_versions`
(`id_dashboard`,`version_no`,`status`,`titulo_override`,`layout_json`,`filters_json`,`theme_json`,`notes`,`created_by`,`published_by`,`published_at`)
SELECT d.id, 1, 'published', 'Dashboard Comercial',
'{
  "title":"Dashboard Comercial",
  "filters":[
    {"key":"from","type":"date_range","default":"30d"},
    {"key":"id_contrato","type":"contract_select"}
  ],
  "widgets":[
    {"id":"com_leads","type":"kpi","title":"Leads no periodo","data_source":"leads.overview","layout":{"x":0,"y":0,"w":3,"h":2},"config":{"width":3,"icon":"ti ti-users","color":"teal"}},
    {"id":"com_funil","type":"donut_chart","title":"Funil por status","data_source":"leads.funnel.status","layout":{"x":3,"y":0,"w":4,"h":4},"config":{"width":4,"icon":"ti ti-chart-funnel","color":"teal","limit":8}},
    {"id":"com_tickets","type":"bar_chart","title":"Aging de tickets","data_source":"tickets.aging","layout":{"x":7,"y":0,"w":5,"h":4},"config":{"width":5,"icon":"ti ti-ticket","color":"info","limit":6}},
    {"id":"com_lista","type":"table","title":"Leitura geral de leads","data_source":"leads.overview","layout":{"x":0,"y":1,"w":12,"h":4},"config":{"width":12,"icon":"ti ti-list-details","color":"primary","limit":8}}
  ]
}',
'{"from":"-29 days","to":"today"}',
'{"hero":"commercial"}',
'Seed inicial TIIX',
NULL,NULL,NOW()
FROM `wpjy_dashboards` d
WHERE d.`id_contrato` = 57 AND d.`slug` = 'comercial';

INSERT INTO `wpjy_dashboard_versions`
(`id_dashboard`,`version_no`,`status`,`titulo_override`,`layout_json`,`filters_json`,`theme_json`,`notes`,`created_by`,`published_by`,`published_at`)
SELECT d.id, 1, 'published', 'Dashboard Diretoria',
'{
  "title":"Dashboard Diretoria",
  "filters":[
    {"key":"from","type":"date_range","default":"30d"},
    {"key":"id_contrato","type":"contract_select"}
  ],
  "widgets":[
    {"id":"dir_fin","type":"kpi","title":"Resumo financeiro","data_source":"finance.summary","layout":{"x":0,"y":0,"w":3,"h":2},"config":{"width":3,"icon":"ti ti-cash-banknote","color":"success"}},
    {"id":"dir_leads","type":"kpi","title":"Resumo de leads","data_source":"leads.overview","layout":{"x":3,"y":0,"w":3,"h":2},"config":{"width":3,"icon":"ti ti-users","color":"teal"}},
    {"id":"dir_tickets","type":"donut_chart","title":"Tickets por aging","data_source":"tickets.aging","layout":{"x":6,"y":0,"w":3,"h":4},"config":{"width":3,"icon":"ti ti-ticket","color":"info","limit":6}},
    {"id":"dir_docs","type":"bar_chart","title":"Documentos por entidade","data_source":"documents.by_entity","layout":{"x":9,"y":0,"w":3,"h":4},"config":{"width":3,"icon":"ti ti-file-text","color":"warning","limit":6}},
    {"id":"dir_exec","type":"list","title":"Execucao","data_source":"tasks.executive.summary","layout":{"x":0,"y":1,"w":12,"h":4},"config":{"width":12,"icon":"ti ti-activity-heartbeat","color":"violet","limit":6}}
  ]
}',
'{"from":"-29 days","to":"today"}',
'{"hero":"executive"}',
'Seed inicial TIIX',
NULL,NULL,NOW()
FROM `wpjy_dashboards` d
WHERE d.`id_contrato` = 57 AND d.`slug` = 'diretoria';

INSERT INTO `wpjy_dashboard_versions`
(`id_dashboard`,`version_no`,`status`,`titulo_override`,`layout_json`,`filters_json`,`theme_json`,`notes`,`created_by`,`published_by`,`published_at`)
SELECT d.id, 1, 'published', 'Dashboard Operacao',
'{
  "title":"Dashboard Operacao",
  "filters":[
    {"key":"from","type":"date_range","default":"30d"},
    {"key":"id_contrato","type":"contract_select"}
  ],
  "widgets":[
    {"id":"ops_tickets","type":"bar_chart","title":"Aging de tickets","data_source":"tickets.aging","layout":{"x":0,"y":0,"w":6,"h":4},"config":{"width":6,"icon":"ti ti-ticket","color":"danger","limit":6}},
    {"id":"ops_docs","type":"bar_chart","title":"Documentos por entidade","data_source":"documents.by_entity","layout":{"x":6,"y":0,"w":6,"h":4},"config":{"width":6,"icon":"ti ti-file-text","color":"warning","limit":6}},
    {"id":"ops_exec","type":"list","title":"Execucao","data_source":"tasks.executive.summary","layout":{"x":0,"y":1,"w":12,"h":4},"config":{"width":12,"icon":"ti ti-activity-heartbeat","color":"secondary","limit":6}}
  ]
}',
'{"from":"-29 days","to":"today"}',
'{"hero":"operations"}',
'Seed inicial TIIX',
NULL,NULL,NOW()
FROM `wpjy_dashboards` d
WHERE d.`id_contrato` = 57 AND d.`slug` = 'operacao';

UPDATE `wpjy_dashboards` d
INNER JOIN `wpjy_dashboard_versions` v
  ON v.`id_dashboard` = d.`id`
 AND v.`version_no` = 1
SET d.`published_version_id` = v.`id`,
    d.`status` = 'published'
WHERE d.`id_contrato` = 57
  AND d.`slug` IN ('financeiro', 'comercial', 'diretoria', 'operacao');

INSERT INTO `wpjy_dashboard_acl`
(`id_dashboard`,`target_kind`,`target_key`,`id_grupo`,`rule`,`inherit_mode`)
SELECT d.id, 'dashboard', NULL, g.id, 'allow', 'with_descendants'
FROM `wpjy_dashboards` d
JOIN `wpjy_grupos_permissoes` g ON g.id IN (2,3,6,7,55)
WHERE d.`id_contrato` = 57 AND d.`slug` = 'financeiro';

INSERT INTO `wpjy_dashboard_acl`
(`id_dashboard`,`target_kind`,`target_key`,`id_grupo`,`rule`,`inherit_mode`)
SELECT d.id, 'dashboard', NULL, g.id, 'allow', 'with_descendants'
FROM `wpjy_dashboards` d
JOIN `wpjy_grupos_permissoes` g ON g.id IN (2,3,10,57,58,83,84,85)
WHERE d.`id_contrato` = 57 AND d.`slug` = 'comercial';

INSERT INTO `wpjy_dashboard_acl`
(`id_dashboard`,`target_kind`,`target_key`,`id_grupo`,`rule`,`inherit_mode`)
SELECT d.id, 'dashboard', NULL, g.id, 'allow', 'with_descendants'
FROM `wpjy_dashboards` d
JOIN `wpjy_grupos_permissoes` g ON g.id IN (2,3,55,56,57,61,73,74,75,77,78)
WHERE d.`id_contrato` = 57 AND d.`slug` = 'diretoria';

INSERT INTO `wpjy_dashboard_acl`
(`id_dashboard`,`target_kind`,`target_key`,`id_grupo`,`rule`,`inherit_mode`)
SELECT d.id, 'dashboard', NULL, g.id, 'allow', 'with_descendants'
FROM `wpjy_dashboards` d
JOIN `wpjy_grupos_permissoes` g ON g.id IN (2,3,4,9,11,15,16,17,61,65,68)
WHERE d.`id_contrato` = 57 AND d.`slug` = 'operacao';

COMMIT;
