-- ============================================================
-- FCQC - TNA Module Permissions Seed Data
-- ============================================================

-- -----------------------------------------------------------
-- 1. TNA Permissions
-- -----------------------------------------------------------
INSERT IGNORE INTO `permissions` (`code`, `label`, `group_name`, `description`) VALUES
('tna.view', 'View TNA Projects', 'TNA', 'View TNA projects, calendar, kanban, and eagle eye'),
('tna.create', 'Create TNA Projects', 'TNA', 'Create new TNA projects'),
('tna.edit', 'Edit TNA Projects & Tasks', 'TNA', 'Edit TNA projects and tasks'),
('templates.manage', 'Manage TNA Templates', 'TNA', 'Create, edit, and delete TNA templates'),
('team.view', 'View TNA Team', 'TNA', 'View TNA team members and roles'),
('partners.view', 'View TNA Partners', 'TNA', 'View TNA partner access'),
('predictions.view', 'View AI Predictions', 'TNA', 'View AI delay predictions and recommendations'),
('email_report.view', 'View Email Reports', 'TNA', 'View and send email reports'),
('analytics.view', 'View TNA Analytics', 'TNA', 'View TNA analytics and dashboards'),
('task_report.view', 'View Task Reports', 'TNA', 'View task reports and summaries'),
('styles.view', 'View Styles (TNA)', 'TNA', 'View styles from TNA context'),
('purchase_orders.view', 'View Purchase Orders (TNA)', 'TNA', 'View purchase orders from TNA context'),
('factories.view', 'View Factories (TNA)', 'TNA', 'View factories from TNA context'),
('buyers.view', 'View Buyers (TNA)', 'TNA', 'View buyers from TNA context');

-- -----------------------------------------------------------
-- 2. Assign TNA permissions to admin role
-- -----------------------------------------------------------
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT r.id, p.id FROM app_roles r, permissions p
WHERE r.name = 'admin'
  AND p.code IN (
    'tna.view', 'tna.create', 'tna.edit',
    'templates.manage', 'team.view', 'partners.view',
    'predictions.view', 'email_report.view', 'analytics.view',
    'task_report.view', 'styles.view', 'purchase_orders.view',
    'factories.view', 'buyers.view'
  );

-- -----------------------------------------------------------
-- 3. Assign TNA permissions to quality_manager role
-- -----------------------------------------------------------
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT r.id, p.id FROM app_roles r, permissions p
WHERE r.name = 'quality_manager'
  AND p.code IN (
    'tna.view', 'tna.create', 'tna.edit',
    'templates.manage', 'team.view', 'partners.view',
    'predictions.view', 'email_report.view', 'analytics.view',
    'task_report.view', 'styles.view', 'purchase_orders.view',
    'factories.view', 'buyers.view'
  );

-- -----------------------------------------------------------
-- 4. Assign TNA permissions to quality_inspector role
-- -----------------------------------------------------------
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT r.id, p.id FROM app_roles r, permissions p
WHERE r.name = 'quality_inspector'
  AND p.code IN (
    'tna.view', 'task_report.view',
    'styles.view', 'purchase_orders.view',
    'factories.view', 'buyers.view'
  );

-- -----------------------------------------------------------
-- 5. Assign TNA permissions to quality_viewer role
-- -----------------------------------------------------------
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT r.id, p.id FROM app_roles r, permissions p
WHERE r.name = 'quality_viewer'
  AND p.code IN (
    'tna.view', 'task_report.view',
    'styles.view', 'purchase_orders.view',
    'factories.view', 'buyers.view'
  );
