-- Migration 006: Store commit messages and per-file changes from push webhooks

-- Per-commit details linked to an attached repository
CREATE TABLE IF NOT EXISTS repository_commit_details (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  repository_id UUID REFERENCES github_repositories(id) ON DELETE CASCADE,
  commit_sha VARCHAR(64) NOT NULL,
  author_name VARCHAR(200),
  author_email VARCHAR(320),
  message TEXT,
  url TEXT,
  committed_at TIMESTAMP DEFAULT NOW(),
  created_at TIMESTAMP DEFAULT NOW()
);

CREATE UNIQUE INDEX IF NOT EXISTS uq_repo_commit_sha ON repository_commit_details(repository_id, commit_sha);
CREATE INDEX IF NOT EXISTS idx_repo_commit_created_at ON repository_commit_details(created_at);

-- Per-file changes for each commit
CREATE TABLE IF NOT EXISTS repository_commit_files (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  commit_id UUID REFERENCES repository_commit_details(id) ON DELETE CASCADE,
  change_type VARCHAR(20) NOT NULL, -- added | modified | removed
  file_path TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_commit_files_commit_id ON repository_commit_files(commit_id);
CREATE INDEX IF NOT EXISTS idx_commit_files_path ON repository_commit_files(file_path);


