-- ============================================================
-- PurrCore v2 — Seeds: Roles, Permisos, Ubicación POS demo
-- ============================================================

-- ─── PERMISOS ─────────────────────────────────────────────────
INSERT INTO permissions (name, label, module) VALUES
-- Dashboard
('dashboard.view',          'Ver dashboard',              'dashboard'),
-- Pedidos
('orders.view',             'Ver pedidos',                'orders'),
('orders.create',           'Crear pedidos',              'orders'),
('orders.edit',             'Editar pedidos',             'orders'),
('orders.delete',           'Eliminar pedidos',           'orders'),
('orders.refund',           'Procesar reembolsos',        'orders'),
('orders.refund_partial',   'Reembolsos parciales',       'orders'),
('orders.change_status',    'Cambiar estado pedidos',     'orders'),
('orders.add_tracking',     'Añadir seguimiento',         'orders'),
('orders.export',           'Exportar pedidos',           'orders'),
-- Productos
('products.view',           'Ver productos',              'products'),
('products.create',         'Crear productos',            'products'),
('products.edit',           'Editar productos',           'products'),
('products.delete',         'Eliminar productos',         'products'),
('products.import',         'Importar productos',         'products'),
-- Stock
('stock.view',              'Ver stock',                  'stock'),
('stock.adjust',            'Ajustar stock',              'stock'),
('stock.receive',           'Recibir mercancía',          'stock'),
('stock.transfer',          'Transferir stock',           'stock'),
('purchase_orders.view',    'Ver órdenes de compra',      'stock'),
('purchase_orders.create',  'Crear órdenes de compra',    'stock'),
('purchase_orders.receive', 'Recepcionar compras',        'stock'),
-- Clientes
('customers.view',          'Ver clientes',               'customers'),
('customers.edit',          'Editar clientes',            'customers'),
('customers.delete',        'Eliminar clientes',          'customers'),
('customers.points',        'Gestionar puntos',           'customers'),
-- Facturas
('invoices.view',           'Ver facturas',               'invoices'),
('invoices.create',         'Crear facturas manuales',    'invoices'),
('invoices.download',       'Descargar facturas',         'invoices'),
('invoices.cancel',         'Anular facturas',            'invoices'),
-- Cupones
('coupons.view',            'Ver cupones',                'coupons'),
('coupons.create',          'Crear cupones',              'coupons'),
('coupons.edit',            'Editar cupones',             'coupons'),
('coupons.delete',          'Eliminar cupones',           'coupons'),
-- Newsletter
('newsletter.view',         'Ver newsletter',             'marketing'),
('newsletter.send',         'Enviar campañas',            'marketing'),
('newsletter.manage',       'Gestionar suscriptores',     'marketing'),
-- Backups
('backups.view',            'Ver backups',                'system'),
('backups.create',          'Crear backups',              'system'),
('backups.restore',         'Restaurar backups',          'system'),
('backups.delete',          'Eliminar backups',           'system'),
-- POS
('pos.access',              'Acceder al POS',             'pos'),
('pos.open_session',        'Abrir sesión de caja',       'pos'),
('pos.close_session',       'Cerrar sesión de caja',      'pos'),
('pos.apply_discount',      'Aplicar descuentos en POS',  'pos'),
('pos.void_transaction',    'Anular transacciones POS',   'pos'),
('pos.manage_locations',    'Gestionar ubicaciones POS',  'pos'),
-- Staff
('staff.view',              'Ver staff',                  'staff'),
('staff.create',            'Crear miembros de staff',    'staff'),
('staff.edit',              'Editar staff',               'staff'),
('staff.delete',            'Eliminar staff',             'staff'),
-- Configuración
('settings.view',           'Ver configuración',          'settings'),
('settings.edit',           'Editar configuración',       'settings'),
-- Importaciones externas
('external_orders.view',    'Ver pedidos externos',       'orders'),
('external_orders.import',  'Importar pedidos externos',  'orders'),
-- Finanzas/Informes
('reports.view',            'Ver informes',               'reports'),
('reports.export',          'Exportar informes',          'reports'),
-- Auditoría
('audit.view',              'Ver logs de auditoría',      'system')
ON DUPLICATE KEY UPDATE label = VALUES(label);

-- ─── ROLES ────────────────────────────────────────────────────
INSERT INTO roles (id, name, label, description, is_system) VALUES
(1, 'super_admin', 'Super Administrador', 'Acceso total al sistema. No puede ser eliminado.', 1),
(2, 'admin',       'Administrador',        'Acceso completo excepto gestión de super admins.', 1),
(3, 'manager',     'Gerente',             'Gestión de pedidos, productos, clientes e informes.', 0),
(4, 'cashier',     'Cajero/a',            'Acceso al POS y gestión básica de pedidos.', 0),
(5, 'warehouse',   'Almacén',             'Gestión de stock, recepciones y transferencias.', 0),
(6, 'marketing',   'Marketing',           'Newsletter, cupones y acceso a informes.', 0)
ON DUPLICATE KEY UPDATE label = VALUES(label);

-- ─── PERMISOS POR ROL ─────────────────────────────────────────

-- Super Admin: todos los permisos
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 1, id FROM permissions;

-- Admin: todo excepto audit y backups.restore
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 2, id FROM permissions
WHERE name NOT IN ('backups.restore', 'audit.view');

-- Gerente
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 3, id FROM permissions
WHERE name IN (
    'dashboard.view',
    'orders.view','orders.edit','orders.change_status','orders.add_tracking','orders.export',
    'orders.refund','orders.refund_partial',
    'products.view','products.create','products.edit',
    'stock.view','stock.adjust','stock.receive','stock.transfer',
    'purchase_orders.view','purchase_orders.create','purchase_orders.receive',
    'customers.view','customers.edit','customers.points',
    'invoices.view','invoices.create','invoices.download',
    'coupons.view','coupons.create','coupons.edit',
    'newsletter.view',
    'pos.access','pos.open_session','pos.close_session','pos.apply_discount','pos.void_transaction',
    'reports.view','reports.export',
    'external_orders.view','external_orders.import'
);

-- Cajero
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 4, id FROM permissions
WHERE name IN (
    'dashboard.view',
    'orders.view','orders.create','orders.change_status','orders.add_tracking',
    'products.view',
    'stock.view',
    'customers.view',
    'invoices.view','invoices.download',
    'pos.access','pos.open_session','pos.close_session','pos.apply_discount'
);

-- Almacén
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 5, id FROM permissions
WHERE name IN (
    'dashboard.view',
    'orders.view','orders.change_status','orders.add_tracking',
    'products.view',
    'stock.view','stock.adjust','stock.receive','stock.transfer',
    'purchase_orders.view','purchase_orders.create','purchase_orders.receive'
);

-- Marketing
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 6, id FROM permissions
WHERE name IN (
    'dashboard.view',
    'orders.view',
    'products.view',
    'customers.view',
    'coupons.view','coupons.create','coupons.edit',
    'newsletter.view','newsletter.send','newsletter.manage',
    'reports.view','reports.export'
);

-- ─── UBICACIÓN POS DEMO ───────────────────────────────────────
INSERT INTO pos_locations (id, name, slug, address, city, postcode, active) VALUES
(1, 'Tienda Online', 'online', NULL, NULL, NULL, 1),
(2, 'Tienda Física Málaga Centro', 'malaga-centro', 'Calle Larios 1', 'Málaga', '29001', 1)
ON DUPLICATE KEY UPDATE name = VALUES(name);
