-- ====================================================================
-- Storefront theme / "Theme Settings" module (2026-09-23)
--   Single-row table (id = 1) holding the storefront's 8 base colors, fonts
--   and corner radius. Everything else (hover shades, gray scale, status
--   palettes, footer color…) is derived in src/lib/theme/storefrontTheme.ts.
--   Separate from `skins`, which themes the admin panel per employee.
--   Defaults = the store's look before theming existed.
-- Safe to run more than once.
-- ====================================================================

SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `storefront_theme` (
  `id`               TINYINT      PRIMARY KEY DEFAULT 1,
  `preset`           VARCHAR(50)  NOT NULL DEFAULT 'desicart' COMMENT 'Preset key, or custom',
  `color_primary`    CHAR(7)      NOT NULL DEFAULT '#FF7A00',
  `color_secondary`  CHAR(7)      NOT NULL DEFAULT '#2E7D32',
  `color_background` CHAR(7)      NOT NULL DEFAULT '#FFFFFF',
  `color_text`       CHAR(7)      NOT NULL DEFAULT '#1E2A2E',
  `color_success`    CHAR(7)      NOT NULL DEFAULT '#00A544',
  `color_warning`    CHAR(7)      NOT NULL DEFAULT '#EDB200',
  `color_danger`     CHAR(7)      NOT NULL DEFAULT '#FB2C36',
  `color_info`       CHAR(7)      NOT NULL DEFAULT '#3080FF',
  `font_heading`     VARCHAR(40)  NOT NULL DEFAULT 'inter',
  `font_body`        VARCHAR(40)  NOT NULL DEFAULT 'inter',
  `radius`           VARCHAR(20)  NOT NULL DEFAULT 'medium',
  `updated_by`       VARCHAR(36)  NULL DEFAULT NULL,
  `created_at`       TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`       TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  CONSTRAINT `chk_storefront_theme_single_row` CHECK (`id` = 1),
  CONSTRAINT `fk_storefront_theme_updated_by` FOREIGN KEY (`updated_by`) REFERENCES `employees`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO `storefront_theme` (`id`) VALUES (1);

INSERT IGNORE INTO `permissions` (`id`, `name`, `module`, `action`) VALUES
  (UUID(), 'theme_settings:read',   'theme_settings', 'read'),
  (UUID(), 'theme_settings:update', 'theme_settings', 'update');

INSERT IGNORE INTO `role_permissions` (`id`, `role_id`, `permission_id`)
SELECT UUID(), r.id, p.id
FROM `roles` r
JOIN `permissions` p ON p.module = 'theme_settings'
WHERE r.name = 'super_admin';
