-- Migration 006: Add domain configuration table
--
-- Stores domain-specific settings and configurations:
-- - Government: departments, approval workflows
-- - Enterprise: departments, team structures
-- - Education: schools, faculties, semesters

------------------------------------------------------------------------
-- Domain configuration table
------------------------------------------------------------------------

CREATE TABLE IF NOT EXISTS domain_config (
    id                        INTEGER PRIMARY KEY CHECK (id = 1),
    -- Domain type (mirrors system_config.domain_type)
    domain_type               TEXT    NOT NULL DEFAULT 'enterprise'
                                CHECK(domain_type IN ('government', 'enterprise', 'education')),
    -- Domain-specific settings (JSON)
    settings                  TEXT    NOT NULL DEFAULT '{}',
    -- Government-specific
    government_settings       TEXT    NOT NULL DEFAULT '{}',
    -- Enterprise-specific
    enterprise_settings       TEXT    NOT NULL DEFAULT '{}',
    -- Education-specific
    education_settings        TEXT    NOT NULL DEFAULT '{}',
    -- Enabled features (JSON array)
    enabled_features          TEXT    NOT NULL DEFAULT '[]',
    -- Department/division list (JSON array)
    departments               TEXT    NOT NULL DEFAULT '[]',
    -- Custom domain parameters (JSON)
    custom_params             TEXT    NOT NULL DEFAULT '{}',
    -- Timestamps
    created_at                INTEGER NOT NULL,
    updated_at                INTEGER NOT NULL
);

-- Seed default domain configuration
INSERT OR IGNORE INTO domain_config (id, created_at, updated_at)
VALUES (1, unixepoch('now', 'subsec') * 1000, unixepoch('now', 'subsec') * 1000);

------------------------------------------------------------------------
-- Domain presets (reference data)
------------------------------------------------------------------------

CREATE TABLE IF NOT EXISTS domain_presets (
    id                TEXT PRIMARY KEY NOT NULL,
    name              TEXT NOT NULL,
    domain_type       TEXT NOT NULL
                        CHECK(domain_type IN ('government', 'enterprise', 'education')),
    description       TEXT,
    -- Default settings for this preset
    settings          TEXT NOT NULL DEFAULT '{}',
    -- Preset configurations
    config            TEXT NOT NULL DEFAULT '{}',
    -- Sort order
    sort_order        INTEGER NOT NULL DEFAULT 0,
    -- Timestamps
    created_at        INTEGER NOT NULL
);

-- Seed government presets
INSERT OR IGNORE INTO domain_presets (id, name, domain_type, description, settings, config, sort_order, created_at)
VALUES
    ('gov_office', '政务办公', 'government', '政务办公场景，适合政府机关日常办公',
     '{"approval_workflow": true, "document_classification": true, "multi_department": true}',
     '{"departments": ["办公室", "人事处", "财务处", "业务处"], "features": ["公文管理", "审批流程", "档案管理"]}',
     10, unixepoch('now', 'subsec') * 1000),
    ('gov_service', '政务服务', 'government', '政务服务场景，适合对外服务窗口',
     '{"queue_management": true, "service_rating": true, "id_verification": true}',
     '{"departments": ["综合窗口", "专项服务", "投诉受理"], "features": ["排队叫号", "服务评价", "身份核验"]}',
     20, unixepoch('now', 'subsec') * 1000);

-- Seed enterprise presets
INSERT OR IGNORE INTO domain_presets (id, name, domain_type, description, settings, config, sort_order, created_at)
VALUES
    ('ent_corporate', '企业办公', 'enterprise', '企业通用办公场景',
     '{"team_structure": true, "project_tracking": true, "knowledge_base": true}',
     '{"departments": ["技术部", "市场部", "运营部", "财务部"], "features": ["项目管理", "知识库", "团队协作"]}',
     10, unixepoch('now', 'subsec') * 1000),
    ('ent_sales', '销售团队', 'enterprise', '销售团队专用场景',
     '{"crm_integration": true, "lead_tracking": true, "sales_reporting": true}',
     '{"departments": ["销售一部", "销售二部", "客服部"], "features": ["客户管理", "销售报表", "线索追踪"]}',
     20, unixepoch('now', 'subsec') * 1000);

-- Seed education presets
INSERT OR IGNORE INTO domain_presets (id, name, domain_type, description, settings, config, sort_order, created_at)
VALUES
    ('edu_university', '高等院校', 'education', '高等院校教学科研场景',
     '{"semester_management": true, "course_management": true, "research_support": true}',
     '{"departments": ["计算机系", "数学系", "物理系", "外语系"], "features": ["课程管理", "科研助手", "论文写作"]}',
     10, unixepoch('now', 'subsec') * 1000),
    ('edu_school', '中小学', 'education', '中小学教育场景',
     '{"grade_management": true, "homework_tracking": true, "parent_communication": true}',
     '{"departments": ["语文组", "数学组", "英语组", "综合组"], "features": ["作业管理", "家长通知", "成绩分析"]}',
     20, unixepoch('now', 'subsec') * 1000);

------------------------------------------------------------------------
-- End of migration
------------------------------------------------------------------------