-- ============================================================
-- FCQC - Core Seed Data
-- Admin user, roles, permissions, test company, inspectors
-- ============================================================

-- -----------------------------------------------------------
-- 1. User Groups
-- -----------------------------------------------------------
INSERT IGNORE INTO `user_groups` (`name`, `label`) VALUES
('management', 'Management'),
('merchandising', 'Merchandising'),
('production', 'Production'),
('quality', 'Quality'),
('planning', 'Planning'),
('procurement', 'Procurement'),
('finance', 'Finance'),
('hr', 'HR');

-- -----------------------------------------------------------
-- 2. Permissions
-- -----------------------------------------------------------
INSERT IGNORE INTO `permissions` (`code`, `label`, `group_name`, `description`) VALUES
-- Core
('dashboard.view', 'View Dashboard', 'General', 'Access the main dashboard'),
('profile.view', 'View Profile', 'General', 'View and edit own profile'),
-- Quality
('quality.view', 'View Quality', 'Quality', 'Access quality module'),
('quality.inspect', 'Perform Inspections', 'Quality', 'Execute inspection sessions'),
('quality.approve', 'Approve Inspections', 'Quality', 'Approve or reject inspection results'),
('quality.manage', 'Manage Quality', 'Quality', 'Configure templates, rules, and plans'),
-- Reports
('reports.view', 'View Reports', 'Reports', 'Access report section'),
-- Notifications
('notifications.view', 'View Notifications', 'Notifications', 'View notifications'),
-- Admin
('roles.manage', 'Manage Roles & Permissions', 'Administration', 'Create, edit, and assign roles and permissions'),
('users.view', 'View Users', 'Administration', 'View user list'),
('users.create', 'Create Users', 'Administration', 'Create new users'),
('users.edit', 'Edit Users', 'Administration', 'Edit existing users'),
('users.delete', 'Delete Users', 'Administration', 'Delete users');

-- -----------------------------------------------------------
-- 3. Roles
-- -----------------------------------------------------------
INSERT IGNORE INTO `app_roles` (`name`, `label`, `description`) VALUES
('admin', 'Administrator', 'Full system access'),
('quality_manager', 'Quality Manager', 'Manages quality inspections and team'),
('quality_inspector', 'Quality Inspector', 'Executes inspections'),
('quality_viewer', 'Quality Viewer', 'Read-only access to quality data');

-- -----------------------------------------------------------
-- 4. Role-Permission assignments
-- -----------------------------------------------------------
-- Admin gets all permissions
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT r.id, p.id FROM app_roles r, permissions p WHERE r.name = 'admin';

-- Quality Manager
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 ('dashboard.view', 'profile.view', 'quality.view', 'quality.inspect', 'quality.approve', 'quality.manage', 'reports.view', 'notifications.view');

-- Quality Inspector
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 ('dashboard.view', 'profile.view', 'quality.view', 'quality.inspect', 'notifications.view');

-- Quality Viewer
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 ('dashboard.view', 'profile.view', 'quality.view', 'reports.view');

-- -----------------------------------------------------------
-- 5. Company
-- -----------------------------------------------------------
INSERT IGNORE INTO `companies` (`company_name`, `slug`, `contact_person`, `email`, `country`, `timezone`, `status`) VALUES
('FCQC Test Corp', 'fcqc-test', 'Admin User', 'admin@fcqc.com', 'Bangladesh', 'Asia/Dhaka', 'active');

-- -----------------------------------------------------------
-- 6. Admin User (password: "password")
-- -----------------------------------------------------------
INSERT IGNORE INTO `users` (`company_id`, `employee_code`, `name`, `email`, `password_hash`, `role_id`, `status`)
SELECT c.id, 'EMP-0000', 'Admin', 'admin@fcqc.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', r.id, 'active'
FROM companies c, app_roles r
WHERE c.slug = 'fcqc-test' AND r.name = 'admin';

-- -----------------------------------------------------------
-- 7. Sample Quality Inspectors
-- -----------------------------------------------------------
INSERT IGNORE INTO `quality_inspectors` (`name`, `employee_code`, `designation`, `certification`, `status`) VALUES
('Rafiq Hasan', 'QC-001', 'Senior Inspector', 'AQL Level II', 'active'),
('Fatima Begum', 'QC-002', 'Inspector', 'AQL Level I', 'active'),
('Kamal Hossain', 'QC-003', 'Junior Inspector', 'In Training', 'active'),
('Nasrin Akter', 'QC-004', 'Lead Inspector', 'AQL Level III', 'active');
