CREATE TABLE IF NOT EXISTS slider_slides (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  title_ar VARCHAR(190) NOT NULL,
  title_en VARCHAR(190) NULL,
  subtitle_ar VARCHAR(190) NULL,
  subtitle_en VARCHAR(190) NULL,
  description_ar TEXT NULL,
  description_en TEXT NULL,
  image VARCHAR(255) NULL,
  button_text_ar VARCHAR(120) NULL,
  button_text_en VARCHAR(120) NULL,
  button_link VARCHAR(255) NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  sort_order INT NOT NULL DEFAULT 0,
  created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT IGNORE INTO permissions (permission_key, permission_name, module) VALUES
('view_slides','View slides','slides'),
('create_slides','Create slides','slides'),
('edit_slides','Edit slides','slides'),
('delete_slides','Delete slides','slides');

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 1, id FROM permissions WHERE permission_key IN ('view_slides','create_slides','edit_slides','delete_slides');

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 2, id FROM permissions WHERE permission_key IN ('view_slides','create_slides','edit_slides','delete_slides');

INSERT INTO slider_slides (title_ar, title_en, subtitle_ar, subtitle_en, description_ar, description_en, image, button_text_ar, button_text_en, button_link, is_active, sort_order, created_at, updated_at)
SELECT m.name, m.name, 'أفضل خدماتنا', 'Featured service', m.short_description, m.description, m.image, 'اختر هذه الخدمة', 'Choose this service', '/request.php', 1, m.sort_order, NOW(), NOW()
FROM materials m
WHERE m.status = 'active'
  AND m.slug IN ('solar-energy','networks-internet','surveillance-cameras','fire-alarm','audio-systems','integrated-security')
  AND NOT EXISTS (SELECT 1 FROM slider_slides LIMIT 1)
ORDER BY FIELD(m.slug, 'solar-energy','networks-internet','surveillance-cameras','fire-alarm','audio-systems','integrated-security');
