-- Migration 014: Additional OAuth Provider Tables
-- This migration adds OAuth token tables for GitLab, Bitbucket, and Gitea

-- GitLab OAuth tokens
CREATE TABLE IF NOT EXISTS gitlab_user_tokens (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  access_token TEXT NOT NULL,
  gitlab_username TEXT,
  gitlab_user_id TEXT,
  scopes JSONB,
  expires_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Bitbucket OAuth tokens
CREATE TABLE IF NOT EXISTS bitbucket_user_tokens (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  access_token TEXT NOT NULL,
  bitbucket_username TEXT,
  bitbucket_user_id TEXT,
  scopes JSONB,
  expires_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Gitea OAuth tokens
CREATE TABLE IF NOT EXISTS gitea_user_tokens (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  access_token TEXT NOT NULL,
  gitea_username TEXT,
  gitea_user_id TEXT,
  scopes JSONB,
  expires_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Add triggers for updated_at columns
CREATE TRIGGER update_gitlab_user_tokens_updated_at BEFORE UPDATE ON gitlab_user_tokens
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER update_bitbucket_user_tokens_updated_at BEFORE UPDATE ON bitbucket_user_tokens
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER update_gitea_user_tokens_updated_at BEFORE UPDATE ON gitea_user_tokens
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
