-- GitLab Integration
-- Enables linking communities to GitLab projects for issue/MR-based governance

-- GitLab connections (per community)
CREATE TABLE gitlab_connections (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    community_id UUID NOT NULL REFERENCES communities(id) ON DELETE CASCADE UNIQUE,
    gitlab_url VARCHAR(500) NOT NULL, -- e.g., https://invent.kde.org
    project_path VARCHAR(500) NOT NULL, -- e.g., niccolove/likwid
    access_token_encrypted TEXT, -- Encrypted access token
    webhook_secret VARCHAR(100),
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    sync_issues BOOLEAN NOT NULL DEFAULT TRUE,
    sync_merge_requests BOOLEAN NOT NULL DEFAULT TRUE,
    auto_create_proposals BOOLEAN NOT NULL DEFAULT FALSE, -- Auto-create proposals from issues
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    last_synced_at TIMESTAMPTZ
);

-- Linked GitLab issues
CREATE TABLE gitlab_issues (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    connection_id UUID NOT NULL REFERENCES gitlab_connections(id) ON DELETE CASCADE,
    gitlab_iid INT NOT NULL, -- GitLab issue IID
    gitlab_id BIGINT NOT NULL, -- GitLab global issue ID
    title VARCHAR(500) NOT NULL,
    description TEXT,
    state VARCHAR(20) NOT NULL, -- opened, closed
    author_username VARCHAR(255),
    labels TEXT[], -- Array of label names
    proposal_id UUID REFERENCES proposals(id) ON DELETE SET NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    gitlab_created_at TIMESTAMPTZ,
    gitlab_updated_at TIMESTAMPTZ,
    UNIQUE(connection_id, gitlab_iid)
);

-- Linked GitLab merge requests
CREATE TABLE gitlab_merge_requests (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    connection_id UUID NOT NULL REFERENCES gitlab_connections(id) ON DELETE CASCADE,
    gitlab_iid INT NOT NULL,
    gitlab_id BIGINT NOT NULL,
    title VARCHAR(500) NOT NULL,
    description TEXT,
    state VARCHAR(20) NOT NULL, -- opened, merged, closed
    author_username VARCHAR(255),
    source_branch VARCHAR(255),
    target_branch VARCHAR(255),
    labels TEXT[],
    proposal_id UUID REFERENCES proposals(id) ON DELETE SET NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    gitlab_created_at TIMESTAMPTZ,
    gitlab_updated_at TIMESTAMPTZ,
    UNIQUE(connection_id, gitlab_iid)
);

-- GitLab webhook events log
CREATE TABLE gitlab_webhook_events (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    connection_id UUID NOT NULL REFERENCES gitlab_connections(id) ON DELETE CASCADE,
    event_type VARCHAR(50) NOT NULL, -- issue, merge_request, note, etc.
    object_kind VARCHAR(50),
    action VARCHAR(50),
    payload JSONB NOT NULL,
    processed BOOLEAN NOT NULL DEFAULT FALSE,
    processed_at TIMESTAMPTZ,
    error TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Indexes
CREATE INDEX idx_gitlab_connections_community ON gitlab_connections(community_id);
CREATE INDEX idx_gitlab_issues_connection ON gitlab_issues(connection_id);
CREATE INDEX idx_gitlab_issues_proposal ON gitlab_issues(proposal_id);
CREATE INDEX idx_gitlab_mrs_connection ON gitlab_merge_requests(connection_id);
CREATE INDEX idx_gitlab_mrs_proposal ON gitlab_merge_requests(proposal_id);
CREATE INDEX idx_gitlab_webhook_events_connection ON gitlab_webhook_events(connection_id);
CREATE INDEX idx_gitlab_webhook_events_processed ON gitlab_webhook_events(processed) WHERE processed = FALSE;

-- Triggers
CREATE TRIGGER gitlab_connections_updated_at BEFORE UPDATE ON gitlab_connections
    FOR EACH ROW EXECUTE FUNCTION update_updated_at();

CREATE TRIGGER gitlab_issues_updated_at BEFORE UPDATE ON gitlab_issues
    FOR EACH ROW EXECUTE FUNCTION update_updated_at();

CREATE TRIGGER gitlab_mrs_updated_at BEFORE UPDATE ON gitlab_merge_requests
    FOR EACH ROW EXECUTE FUNCTION update_updated_at();
