-- +goose Up

CREATE TYPE posix_node_kind AS ENUM ('directory', 'file');

CREATE TABLE IF NOT EXISTS posix_nodes (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  path TEXT NOT NULL UNIQUE,
  parent_path TEXT,
  name TEXT NOT NULL,
  depth INTEGER NOT NULL,
  node_kind posix_node_kind NOT NULL,
  logical_type TEXT NOT NULL DEFAULT 'generic',
  file_role TEXT,
  resource_id TEXT,
  resource_name TEXT,
  resource_slug TEXT,
  installation_id TEXT,
  organization_id TEXT,
  organization_slug TEXT,
  department_slug TEXT,
  team_slug TEXT,
  project_slug TEXT,
  personal_slug TEXT,
  content_json JSONB,
  size_bytes BIGINT NOT NULL DEFAULT 0,
  checksum TEXT,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_posix_nodes_parent_path ON posix_nodes (parent_path);
CREATE INDEX IF NOT EXISTS idx_posix_nodes_logical_type ON posix_nodes (logical_type);
CREATE INDEX IF NOT EXISTS idx_posix_nodes_project_slug ON posix_nodes (project_slug);
CREATE INDEX IF NOT EXISTS idx_posix_nodes_department_slug ON posix_nodes (department_slug);
CREATE INDEX IF NOT EXISTS idx_posix_nodes_team_slug ON posix_nodes (team_slug);

-- +goose Down

DROP INDEX IF EXISTS idx_posix_nodes_team_slug;
DROP INDEX IF EXISTS idx_posix_nodes_department_slug;
DROP INDEX IF EXISTS idx_posix_nodes_project_slug;
DROP INDEX IF EXISTS idx_posix_nodes_logical_type;
DROP INDEX IF EXISTS idx_posix_nodes_parent_path;
DROP TABLE IF EXISTS posix_nodes;
DROP TYPE IF EXISTS posix_node_kind;
