-- Dashboard Vase operativo: tarjetas de stock, ecommerce, ventas, cajas, DTE e integraciones.
-- Úsalo solo si no puedes ejecutar: php artisan migrate

CREATE TABLE IF NOT EXISTS `dashboard_widgets` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `key` VARCHAR(80) NOT NULL,
  `title` VARCHAR(120) NOT NULL,
  `subtitle` VARCHAR(160) NULL,
  `icon` VARCHAR(100) NULL,
  `color` VARCHAR(30) NOT NULL DEFAULT '#20c58b',
  `route_name` VARCHAR(150) NULL,
  `url` VARCHAR(255) NULL,
  `permission_name` VARCHAR(255) NULL,
  `metric_type` VARCHAR(80) NOT NULL,
  `order` INT UNSIGNED NOT NULL DEFAULT 0,
  `is_active` TINYINT(1) NOT NULL DEFAULT 1,
  `created_at` TIMESTAMP NULL DEFAULT NULL,
  `updated_at` TIMESTAMP NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `dashboard_widgets_key_index` (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

UPDATE `dashboard_widgets`
SET `is_active` = 0, `updated_at` = NOW()
WHERE `key` IN ('usuarios', 'roles', 'permisos', 'modulos');

-- Productos bajo stock
UPDATE `dashboard_widgets` SET
  `title`='Productos bajo stock', `subtitle`='Revisar inventario', `icon`='bi bi-exclamation-triangle-fill', `color`='#f97316',
  `route_name`='vase.inventario.actual', `url`=NULL, `permission_name`=NULL, `metric_type`='productos_bajo_stock', `order`=1, `is_active`=1, `updated_at`=NOW()
WHERE `key`='productos_bajo_stock';
INSERT INTO `dashboard_widgets` (`key`,`title`,`subtitle`,`icon`,`color`,`route_name`,`url`,`permission_name`,`metric_type`,`order`,`is_active`,`created_at`,`updated_at`)
SELECT 'productos_bajo_stock','Productos bajo stock','Revisar inventario','bi bi-exclamation-triangle-fill','#f97316','vase.inventario.actual',NULL,NULL,'productos_bajo_stock',1,1,NOW(),NOW()
WHERE NOT EXISTS (SELECT 1 FROM `dashboard_widgets` WHERE `key`='productos_bajo_stock');

-- Pedidos ecommerce pendientes
UPDATE `dashboard_widgets` SET
  `title`='Pedidos web pendientes', `subtitle`='Ecommerce / delivery', `icon`='bi bi-bag-check-fill', `color`='#20c58b',
  `route_name`='vase.pedidos-externos', `url`=NULL, `permission_name`=NULL, `metric_type`='pedidos_ecommerce_pendientes', `order`=2, `is_active`=1, `updated_at`=NOW()
WHERE `key`='pedidos_ecommerce_pendientes';
INSERT INTO `dashboard_widgets` (`key`,`title`,`subtitle`,`icon`,`color`,`route_name`,`url`,`permission_name`,`metric_type`,`order`,`is_active`,`created_at`,`updated_at`)
SELECT 'pedidos_ecommerce_pendientes','Pedidos web pendientes','Ecommerce / delivery','bi bi-bag-check-fill','#20c58b','vase.pedidos-externos',NULL,NULL,'pedidos_ecommerce_pendientes',2,1,NOW(),NOW()
WHERE NOT EXISTS (SELECT 1 FROM `dashboard_widgets` WHERE `key`='pedidos_ecommerce_pendientes');

-- Ventas hoy
UPDATE `dashboard_widgets` SET
  `title`='Ventas de hoy', `subtitle`='Cantidad del día', `icon`='bi bi-receipt-cutoff', `color`='#0ea5e9',
  `route_name`='vase.reportes-ventas', `url`=NULL, `permission_name`=NULL, `metric_type`='ventas_hoy', `order`=3, `is_active`=1, `updated_at`=NOW()
WHERE `key`='ventas_hoy';
INSERT INTO `dashboard_widgets` (`key`,`title`,`subtitle`,`icon`,`color`,`route_name`,`url`,`permission_name`,`metric_type`,`order`,`is_active`,`created_at`,`updated_at`)
SELECT 'ventas_hoy','Ventas de hoy','Cantidad del día','bi bi-receipt-cutoff','#0ea5e9','vase.reportes-ventas',NULL,NULL,'ventas_hoy',3,1,NOW(),NOW()
WHERE NOT EXISTS (SELECT 1 FROM `dashboard_widgets` WHERE `key`='ventas_hoy');

-- Total vendido hoy
UPDATE `dashboard_widgets` SET
  `title`='Total vendido hoy', `subtitle`='Monto del día', `icon`='bi bi-cash-coin', `color`='#22c55e',
  `route_name`='vase.resumen-ventas-margen', `url`=NULL, `permission_name`=NULL, `metric_type`='ventas_hoy_total', `order`=4, `is_active`=1, `updated_at`=NOW()
WHERE `key`='ventas_hoy_total';
INSERT INTO `dashboard_widgets` (`key`,`title`,`subtitle`,`icon`,`color`,`route_name`,`url`,`permission_name`,`metric_type`,`order`,`is_active`,`created_at`,`updated_at`)
SELECT 'ventas_hoy_total','Total vendido hoy','Monto del día','bi bi-cash-coin','#22c55e','vase.resumen-ventas-margen',NULL,NULL,'ventas_hoy_total',4,1,NOW(),NOW()
WHERE NOT EXISTS (SELECT 1 FROM `dashboard_widgets` WHERE `key`='ventas_hoy_total');

-- Pedidos externos hoy
UPDATE `dashboard_widgets` SET
  `title`='Pedidos externos hoy', `subtitle`='Web, PedidosYa, Uber', `icon`='bi bi-globe-americas', `color`='#8b5cf6',
  `route_name`='vase.pedidos-externos', `url`=NULL, `permission_name`=NULL, `metric_type`='pedidos_externos_hoy', `order`=5, `is_active`=1, `updated_at`=NOW()
WHERE `key`='pedidos_externos_hoy';
INSERT INTO `dashboard_widgets` (`key`,`title`,`subtitle`,`icon`,`color`,`route_name`,`url`,`permission_name`,`metric_type`,`order`,`is_active`,`created_at`,`updated_at`)
SELECT 'pedidos_externos_hoy','Pedidos externos hoy','Web, PedidosYa, Uber','bi bi-globe-americas','#8b5cf6','vase.pedidos-externos',NULL,NULL,'pedidos_externos_hoy',5,1,NOW(),NOW()
WHERE NOT EXISTS (SELECT 1 FROM `dashboard_widgets` WHERE `key`='pedidos_externos_hoy');

-- Ticket promedio
UPDATE `dashboard_widgets` SET
  `title`='Ticket promedio', `subtitle`='Promedio de hoy', `icon`='bi bi-graph-up-arrow', `color`='#06b6d4',
  `route_name`='vase.resumen-ventas-margen', `url`=NULL, `permission_name`=NULL, `metric_type`='ticket_promedio_hoy', `order`=6, `is_active`=1, `updated_at`=NOW()
WHERE `key`='ticket_promedio_hoy';
INSERT INTO `dashboard_widgets` (`key`,`title`,`subtitle`,`icon`,`color`,`route_name`,`url`,`permission_name`,`metric_type`,`order`,`is_active`,`created_at`,`updated_at`)
SELECT 'ticket_promedio_hoy','Ticket promedio','Promedio de hoy','bi bi-graph-up-arrow','#06b6d4','vase.resumen-ventas-margen',NULL,NULL,'ticket_promedio_hoy',6,1,NOW(),NOW()
WHERE NOT EXISTS (SELECT 1 FROM `dashboard_widgets` WHERE `key`='ticket_promedio_hoy');

-- Cajas abiertas
UPDATE `dashboard_widgets` SET
  `title`='Cajas abiertas', `subtitle`='Operación activa', `icon`='bi bi-safe2-fill', `color`='#14b8a6',
  `route_name`='vase.caja', `url`=NULL, `permission_name`=NULL, `metric_type`='cajas_abiertas', `order`=7, `is_active`=1, `updated_at`=NOW()
WHERE `key`='cajas_abiertas';
INSERT INTO `dashboard_widgets` (`key`,`title`,`subtitle`,`icon`,`color`,`route_name`,`url`,`permission_name`,`metric_type`,`order`,`is_active`,`created_at`,`updated_at`)
SELECT 'cajas_abiertas','Cajas abiertas','Operación activa','bi bi-safe2-fill','#14b8a6','vase.caja',NULL,NULL,'cajas_abiertas',7,1,NOW(),NOW()
WHERE NOT EXISTS (SELECT 1 FROM `dashboard_widgets` WHERE `key`='cajas_abiertas');

-- Boletas pendientes SII
UPDATE `dashboard_widgets` SET
  `title`='Boletas pendientes SII', `subtitle`='DTE por revisar', `icon`='bi bi-file-earmark-text-fill', `color`='#eab308',
  `route_name`='vase.reportes-boletas-electronicas', `url`=NULL, `permission_name`=NULL, `metric_type`='boletas_pendientes_sii', `order`=8, `is_active`=1, `updated_at`=NOW()
WHERE `key`='boletas_pendientes_sii';
INSERT INTO `dashboard_widgets` (`key`,`title`,`subtitle`,`icon`,`color`,`route_name`,`url`,`permission_name`,`metric_type`,`order`,`is_active`,`created_at`,`updated_at`)
SELECT 'boletas_pendientes_sii','Boletas pendientes SII','DTE por revisar','bi bi-file-earmark-text-fill','#eab308','vase.reportes-boletas-electronicas',NULL,NULL,'boletas_pendientes_sii',8,1,NOW(),NOW()
WHERE NOT EXISTS (SELECT 1 FROM `dashboard_widgets` WHERE `key`='boletas_pendientes_sii');

-- Integraciones conectadas
UPDATE `dashboard_widgets` SET
  `title`='Integraciones conectadas', `subtitle`='Canales activos', `icon`='bi bi-plug-fill', `color`='#6366f1',
  `route_name`='vase.modulos.integraciones', `url`=NULL, `permission_name`=NULL, `metric_type`='integraciones_conectadas', `order`=9, `is_active`=1, `updated_at`=NOW()
WHERE `key`='integraciones_conectadas';
INSERT INTO `dashboard_widgets` (`key`,`title`,`subtitle`,`icon`,`color`,`route_name`,`url`,`permission_name`,`metric_type`,`order`,`is_active`,`created_at`,`updated_at`)
SELECT 'integraciones_conectadas','Integraciones conectadas','Canales activos','bi bi-plug-fill','#6366f1','vase.modulos.integraciones',NULL,NULL,'integraciones_conectadas',9,1,NOW(),NOW()
WHERE NOT EXISTS (SELECT 1 FROM `dashboard_widgets` WHERE `key`='integraciones_conectadas');
