CREATE DATABASE IF NOT EXISTS `beeai_com` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `beeai_com`;

CREATE TABLE IF NOT EXISTS `admins` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(100) NOT NULL,
  `display_name` VARCHAR(100) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'enabled',
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_admin_username` (`username`),
  KEY `idx_admin_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `admin_operation_logs` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `operator_admin_id` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `operator_username` VARCHAR(100) NOT NULL DEFAULT '',
  `target_admin_id` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `target_username` VARCHAR(100) NOT NULL DEFAULT '',
  `action` VARCHAR(50) NOT NULL,
  `detail` VARCHAR(255) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_operator_admin_id` (`operator_admin_id`),
  KEY `idx_target_admin_id` (`target_admin_id`),
  KEY `idx_action` (`action`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `ai_models` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL,
  `code` VARCHAR(100) NOT NULL,
  `provider` VARCHAR(100) NOT NULL DEFAULT '',
  `model_name` VARCHAR(100) NOT NULL DEFAULT '',
  `api_url` VARCHAR(500) NOT NULL DEFAULT '',
  `api_method` VARCHAR(20) NOT NULL DEFAULT 'POST',
  `api_key` VARCHAR(255) NOT NULL DEFAULT '',
  `request_headers` TEXT NULL,
  `request_config` TEXT NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'enabled',
  `remark` VARCHAR(255) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_ai_model_code` (`code`),
  KEY `idx_ai_model_status` (`status`),
  KEY `idx_ai_model_provider` (`provider`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `admins` (`username`, `display_name`, `password`, `status`, `created_at`, `updated_at`)
SELECT 'admin', 'lex', 'Admin@123456', 'enabled', NOW(), NOW()
WHERE NOT EXISTS (
  SELECT 1 FROM `admins` WHERE `username` = 'admin'
);

CREATE TABLE IF NOT EXISTS `templates` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL,
  `code` VARCHAR(100) NOT NULL,
  `type` VARCHAR(20) NOT NULL DEFAULT 'homepage',
  `html_content` LONGTEXT NULL,
  `css_content` LONGTEXT NULL,
  `description` VARCHAR(255) NOT NULL DEFAULT '',
  `status` VARCHAR(20) NOT NULL DEFAULT 'enabled',
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_template_code` (`code`),
  KEY `idx_template_type` (`type`),
  KEY `idx_template_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `contents` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(255) NOT NULL,
  `slug` VARCHAR(150) NOT NULL,
  `path` VARCHAR(255) NOT NULL DEFAULT '',
  `domain_id` BIGINT UNSIGNED NOT NULL,
  `template_id` BIGINT UNSIGNED NOT NULL,
  `seo_title` VARCHAR(255) NOT NULL DEFAULT '',
  `seo_keywords` VARCHAR(255) NOT NULL DEFAULT '',
  `seo_description` VARCHAR(500) NOT NULL DEFAULT '',
  `content_body` LONGTEXT NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'draft',
  `remark` VARCHAR(255) NOT NULL DEFAULT '',
  `static_file_path` VARCHAR(500) NOT NULL DEFAULT '',
  `static_published_at` DATETIME NULL,
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_content_domain_path` (`domain_id`, `path`),
  KEY `idx_content_domain_id` (`domain_id`),
  KEY `idx_content_slug` (`slug`),
  KEY `idx_content_template_id` (`template_id`),
  KEY `idx_content_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `domains` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `domain` VARCHAR(255) NOT NULL,
  `site_name` VARCHAR(100) NOT NULL,
  `group_name` VARCHAR(100) NOT NULL,
  `template_name` VARCHAR(100) NOT NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'enabled',
  `remark` VARCHAR(255) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_domain` (`domain`),
  KEY `idx_group_name` (`group_name`),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
