-- Table from dashboard_config.json
CREATE TABLE IF NOT EXISTS `dashboard_config` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `dashboard_name` VARCHAR(100) COMMENT '仪表板显示名称',
  `dashboard_type` VARCHAR(50) COMMENT 'sales/finance/customer/executive',
  `config_json` VARCHAR(2000) COMMENT '仪表板布局和组件配置',
  `is_default` VARCHAR(1) COMMENT 'Y/N',
  `org_id` VARCHAR(32) COMMENT '多租户组织隔离',
  `created_by` VARCHAR(32) COMMENT '创建用户ID',
  `created_at` TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='仪表板配置';

CREATE INDEX `idx_dashboard_org` ON `dashboard_config` (`org_id`);
CREATE INDEX `idx_dashboard_type` ON `dashboard_config` (`dashboard_type`);
CREATE UNIQUE INDEX `uk_dashboard_name_org` ON `dashboard_config` (`dashboard_name`, `org_id`);

-- Table from report_template.json
CREATE TABLE IF NOT EXISTS `report_template` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `template_name` VARCHAR(100) COMMENT '报表模板名称',
  `report_type` VARCHAR(50) COMMENT 'sales/finance/customer/contract',
  `sql_query` VARCHAR(2000) COMMENT '报表数据查询SQL',
  `columns_config` VARCHAR(1000) NOT NULL COMMENT 'JSON格式的列配置',
  `filters_config` VARCHAR(1000) NOT NULL COMMENT 'JSON格式的过滤器配置',
  `chart_config` VARCHAR(1000) NOT NULL COMMENT 'JSON格式的图表配置',
  `org_id` VARCHAR(32) COMMENT '多租户组织隔离',
  `created_by` VARCHAR(32) COMMENT '创建用户ID',
  `created_at` TIMESTAMP COMMENT '创建时间',
  `is_active` VARCHAR(1) COMMENT 'Y/N',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报表模板';

CREATE INDEX `idx_template_org` ON `report_template` (`org_id`);
CREATE INDEX `idx_template_type` ON `report_template` (`report_type`);

-- Table from user_dashboard.json
CREATE TABLE IF NOT EXISTS `user_dashboard` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `user_id` VARCHAR(32) COMMENT '关联用户',
  `dashboard_config_id` VARCHAR(32) COMMENT '关联的仪表板配置',
  `layout_json` VARCHAR(2000) NOT NULL COMMENT '用户自定义布局',
  `is_favorite` VARCHAR(1) COMMENT 'Y/N',
  `org_id` VARCHAR(32) COMMENT '多租户组织隔离',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户仪表板';

CREATE UNIQUE INDEX `idx_user_dashboard_user` ON `user_dashboard` (`user_id`, `dashboard_config_id`);
CREATE INDEX `idx_user_dashboard_org` ON `user_dashboard` (`org_id`);
