-- Development schema bootstrap. Keep this in sync with backend/db/models.py.

CREATE TABLE IF NOT EXISTS interface_definitions (
  interface_type VARCHAR(64) PRIMARY KEY,
  name VARCHAR(128) NOT NULL,
  capability VARCHAR(16) NOT NULL,
  field_schema JSONB NOT NULL DEFAULT '{}'::jsonb,
  enabled BOOLEAN NOT NULL DEFAULT TRUE,
  version INTEGER NOT NULL DEFAULT 1,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS model_resources (
  id VARCHAR(40) PRIMARY KEY,
  name VARCHAR(128) NOT NULL DEFAULT '',
  capability VARCHAR(16) NOT NULL,
  interface_type VARCHAR(64) NOT NULL REFERENCES interface_definitions(interface_type) ON DELETE RESTRICT,
  values JSONB NOT NULL DEFAULT '{}'::jsonb,
  secrets JSONB NOT NULL DEFAULT '{}'::jsonb,
  support_image_input BOOLEAN NOT NULL DEFAULT FALSE,
  enabled BOOLEAN NOT NULL DEFAULT TRUE,
  is_default BOOLEAN NOT NULL DEFAULT FALSE,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS knowledge_bases (
  id VARCHAR(40) PRIMARY KEY,
  name VARCHAR(128) NOT NULL,
  description VARCHAR(2048) NOT NULL DEFAULT '',
  embedding_model_resource_id VARCHAR(40) REFERENCES model_resources(id) ON DELETE SET NULL,
  status VARCHAR(16) NOT NULL DEFAULT 'active',
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS assistants (
  id VARCHAR(40) PRIMARY KEY,
  name VARCHAR(128) NOT NULL,
  type VARCHAR(16) NOT NULL DEFAULT 'prompt',
  runtime_mode VARCHAR(16) NOT NULL DEFAULT 'pipeline',
  greeting VARCHAR(2048) NOT NULL DEFAULT '',
  enable_interrupt BOOLEAN NOT NULL DEFAULT TRUE,
  vision_enabled BOOLEAN NOT NULL DEFAULT FALSE,
  vision_model_resource_id VARCHAR(40) REFERENCES model_resources(id) ON DELETE SET NULL,
  knowledge_base_id VARCHAR(40) REFERENCES knowledge_bases(id) ON DELETE RESTRICT,
  prompt VARCHAR(8192) NOT NULL DEFAULT '',
  api_url VARCHAR(512) NOT NULL DEFAULT '',
  api_key VARCHAR(512) NOT NULL DEFAULT '',
  app_id VARCHAR(128) NOT NULL DEFAULT '',
  graph JSON NOT NULL DEFAULT '{}'::json,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS assistant_model_bindings (
  assistant_id VARCHAR(40) NOT NULL REFERENCES assistants(id) ON DELETE CASCADE,
  capability VARCHAR(16) NOT NULL,
  model_resource_id VARCHAR(40) NOT NULL REFERENCES model_resources(id) ON DELETE RESTRICT,
  config JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  PRIMARY KEY (assistant_id, capability)
);

-- Lightweight forward-compatible patches for existing dev databases.
ALTER TABLE interface_definitions
  ALTER COLUMN field_schema TYPE JSONB USING field_schema::jsonb;

ALTER TABLE assistants
  ADD COLUMN IF NOT EXISTS vision_enabled BOOLEAN NOT NULL DEFAULT FALSE,
  ADD COLUMN IF NOT EXISTS vision_model_resource_id VARCHAR(40);

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'assistants_vision_model_resource_id_fkey'
  ) THEN
    ALTER TABLE assistants
      ADD CONSTRAINT assistants_vision_model_resource_id_fkey
      FOREIGN KEY (vision_model_resource_id) REFERENCES model_resources(id) ON DELETE SET NULL;
  END IF;
END $$;

CREATE INDEX IF NOT EXISTS ix_interface_definitions_capability ON interface_definitions(capability);
CREATE INDEX IF NOT EXISTS ix_model_resources_capability ON model_resources(capability);
CREATE INDEX IF NOT EXISTS ix_model_resources_interface_type ON model_resources(interface_type);
CREATE INDEX IF NOT EXISTS ix_knowledge_bases_embedding_model_resource_id ON knowledge_bases(embedding_model_resource_id);
CREATE INDEX IF NOT EXISTS ix_assistants_type ON assistants(type);
CREATE INDEX IF NOT EXISTS ix_assistant_model_bindings_model_resource_id ON assistant_model_bindings(model_resource_id);
