CREATE TABLE IF NOT EXISTS project_modules (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    module_key TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT NOT NULL DEFAULT '',
    enabled INTEGER NOT NULL DEFAULT 1,
    config_json TEXT NOT NULL DEFAULT '{}',
    created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
    updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
    UNIQUE(project_id, module_key)
);

CREATE INDEX IF NOT EXISTS idx_project_modules_project_id ON project_modules(project_id);

CREATE TABLE IF NOT EXISTS project_live_sessions (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    agent_id TEXT NOT NULL REFERENCES agents(id),
    title TEXT NOT NULL,
    status TEXT NOT NULL DEFAULT 'active',
    created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
    updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
);

CREATE INDEX IF NOT EXISTS idx_live_sessions_project_id ON project_live_sessions(project_id);

CREATE TABLE IF NOT EXISTS project_live_messages (
    id TEXT PRIMARY KEY,
    session_id TEXT NOT NULL REFERENCES project_live_sessions(id) ON DELETE CASCADE,
    sender TEXT NOT NULL,
    content TEXT NOT NULL,
    created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
);

CREATE INDEX IF NOT EXISTS idx_live_messages_session_id ON project_live_messages(session_id);

CREATE TABLE IF NOT EXISTS project_agent_tasks (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    agent_id TEXT NOT NULL REFERENCES agents(id),
    title TEXT NOT NULL,
    description TEXT NOT NULL,
    status TEXT NOT NULL DEFAULT 'pending',
    result TEXT,
    error TEXT,
    created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
    started_at TEXT,
    finished_at TEXT
);

CREATE INDEX IF NOT EXISTS idx_agent_tasks_project_id ON project_agent_tasks(project_id);
CREATE INDEX IF NOT EXISTS idx_agent_tasks_status_created_at ON project_agent_tasks(status, created_at);

INSERT OR IGNORE INTO project_modules (
    id,
    project_id,
    module_key,
    name,
    description,
    enabled,
    config_json,
    created_at,
    updated_at
)
SELECT
    lower(hex(randomblob(16))),
    p.id,
    'tuleap_polling_auto_resolve',
    'Polling Tuleap + auto-resolve',
    'Surveille Tuleap et lance le pipeline analyste/developpeur.',
    1,
    '{}',
    strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
    strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
FROM projects p;

INSERT OR IGNORE INTO project_modules (
    id,
    project_id,
    module_key,
    name,
    description,
    enabled,
    config_json,
    created_at,
    updated_at
)
SELECT
    lower(hex(randomblob(16))),
    p.id,
    'ai_live_chat',
    'Live chat agent',
    'Discussion live avec un agent sur le contexte du projet.',
    1,
    '{}',
    strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
    strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
FROM projects p;

INSERT OR IGNORE INTO project_modules (
    id,
    project_id,
    module_key,
    name,
    description,
    enabled,
    config_json,
    created_at,
    updated_at
)
SELECT
    lower(hex(randomblob(16))),
    p.id,
    'agent_task_runner',
    'Agent task runner',
    'File de tâches asynchrones traitées par des agents pré-définis.',
    1,
    '{}',
    strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
    strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
FROM projects p;
