-- Dynamic Plugin System Migration
-- Creates tables for managing dynamic plugins

-- Plugin registry table
CREATE TABLE IF NOT EXISTS dynamic_plugins (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL UNIQUE,
  version TEXT NOT NULL,
  description TEXT,
  author_name TEXT,
  author_email TEXT,
  status TEXT DEFAULT 'active' CHECK (status IN ('active', 'inactive', 'error')),
  config TEXT, -- JSON configuration
  manifest TEXT, -- Full plugin manifest JSON
  installed_at INTEGER DEFAULT (unixepoch()),
  updated_at INTEGER DEFAULT (unixepoch()),
  last_error TEXT
);

-- Plugin admin pages registry
CREATE TABLE IF NOT EXISTS plugin_admin_pages (
  id TEXT PRIMARY KEY,
  plugin_id TEXT NOT NULL,
  path TEXT NOT NULL,
  title TEXT NOT NULL,
  icon TEXT,
  description TEXT,
  menu_label TEXT,
  menu_path TEXT,
  menu_icon TEXT,
  menu_order INTEGER DEFAULT 100,
  menu_parent TEXT,
  permissions TEXT, -- JSON array of required permissions
  is_active BOOLEAN DEFAULT 1,
  created_at INTEGER DEFAULT (unixepoch()),
  FOREIGN KEY (plugin_id) REFERENCES dynamic_plugins(id) ON DELETE CASCADE
);

-- Plugin routes registry
CREATE TABLE IF NOT EXISTS plugin_routes (
  id TEXT PRIMARY KEY,
  plugin_id TEXT NOT NULL,
  path TEXT NOT NULL,
  method TEXT DEFAULT 'GET',
  description TEXT,
  requires_auth BOOLEAN DEFAULT 0,
  permissions TEXT, -- JSON array of required permissions
  is_active BOOLEAN DEFAULT 1,
  created_at INTEGER DEFAULT (unixepoch()),
  FOREIGN KEY (plugin_id) REFERENCES dynamic_plugins(id) ON DELETE CASCADE
);

-- Plugin hooks registry
CREATE TABLE IF NOT EXISTS plugin_hooks (
  id TEXT PRIMARY KEY,
  plugin_id TEXT NOT NULL,
  hook_name TEXT NOT NULL,
  description TEXT,
  priority INTEGER DEFAULT 100,
  is_active BOOLEAN DEFAULT 1,
  created_at INTEGER DEFAULT (unixepoch()),
  FOREIGN KEY (plugin_id) REFERENCES dynamic_plugins(id) ON DELETE CASCADE
);

-- Plugin templates registry
CREATE TABLE IF NOT EXISTS plugin_templates (
  id TEXT PRIMARY KEY,
  plugin_id TEXT NOT NULL,
  template_name TEXT NOT NULL,
  template_type TEXT DEFAULT 'admin', -- 'admin', 'frontend', 'email', etc.
  template_content TEXT NOT NULL,
  is_active BOOLEAN DEFAULT 1,
  created_at INTEGER DEFAULT (unixepoch()),
  updated_at INTEGER DEFAULT (unixepoch()),
  FOREIGN KEY (plugin_id) REFERENCES dynamic_plugins(id) ON DELETE CASCADE
);

-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_dynamic_plugins_name ON dynamic_plugins(name);
CREATE INDEX IF NOT EXISTS idx_dynamic_plugins_status ON dynamic_plugins(status);
CREATE INDEX IF NOT EXISTS idx_plugin_admin_pages_plugin_id ON plugin_admin_pages(plugin_id);
CREATE INDEX IF NOT EXISTS idx_plugin_admin_pages_active ON plugin_admin_pages(is_active);
CREATE INDEX IF NOT EXISTS idx_plugin_routes_plugin_id ON plugin_routes(plugin_id);
CREATE INDEX IF NOT EXISTS idx_plugin_routes_path ON plugin_routes(path);
CREATE INDEX IF NOT EXISTS idx_plugin_hooks_plugin_id ON plugin_hooks(plugin_id);
CREATE INDEX IF NOT EXISTS idx_plugin_hooks_name ON plugin_hooks(hook_name);
CREATE INDEX IF NOT EXISTS idx_plugin_templates_plugin_id ON plugin_templates(plugin_id);
CREATE INDEX IF NOT EXISTS idx_plugin_templates_type ON plugin_templates(template_type);

-- Insert initial system plugin entries for existing core plugins
INSERT OR IGNORE INTO dynamic_plugins (id, name, version, description, author_name, status, installed_at)
VALUES 
  ('core-auth', 'core-auth', '3.0.4', 'Core authentication system', 'CF-CMS Team', 'active', unixepoch()),
  ('core-media', 'core-media', '3.0.4', 'Core media management', 'CF-CMS Team', 'active', unixepoch()),
  ('database-tools', 'database-tools', '3.0.4', 'Database management tools', 'CF-CMS Team', 'active', unixepoch());